[CmdletBinding(SupportsShouldProcess)]param(                                                                                                                                                                                                 
    [ArgumentCompleter({ param (                                                                                                                                                                                                             
        $commandName,                                                                                                                                                                                                                        
        $parameterName,
        $wordToComplete,
        $commandAst,
        $fakeBoundParameters
      )
      [DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
    })]
    [string[]]$ProjectPath=@($PWD),
    [switch]$Recurse,
    [int]$Depth=1,
    [switch]$Force,
    [switch]$OneLine,
    [Parameter(Position = 0, ValueFromRemainingArguments = $true, mandatory)]
    [array]$CliParams
)

$script:JobQueue = @()
$script:CleanupJobQueue = @()
function CompleteJobs{
    if( -not $JobQueue ) { return }
    $local:currentJobQueue = $JobQueue
    $JobQueue = @()
    $CleanupJobQueue += $currentJobQueue

    Wait-Job $currentJobQueue | ForEach-Object {
        $_ | Receive-Job | ForEach-Object {
            Write-Host "---: $($_.Path) | $($_.Params)" -ForegroundColor Cyan
            $_.Results
        }
    }
    Write-Host '--- Background jobs completed' -ForegroundColor Green
}

if( $CliParams[0] -is [string] ) {
    $CliParams = @($CliParams,@($null))
}

$local:dcPathList = $ProjectPath | ForEach-Object {
        if( (Test-Path (Join-Path $_ 'docker-compose.yml')) -and -not $Recurse ) {
            Resolve-Path $_
        } elseif( $Recurse ) {
            $local:gciParams = [ordered]@{
                Recurse = $true
            }
            if( $Depth ) {
                $gciParams.Depth = $Depth
            }
            Get-ChildItem -Path $_ -Include 'docker-compose.yml' @gciParams |
                Select-Object -ExpandProperty Directory |
                Where-Object { $Force -or -not (Test-Path $(Join-Path $_ .noauto.all)) }
        }
    }
if( -not $dcPathList ) { return }

$local:SingleProject = $dcPathList.Length -eq 1

foreach( $local:p in $CliParams ) {
    if( -not $p ) { continue }

    $local:AsJob = $false;
    $local:Wait  = $false;

    if( $p -isnot [String] -or $p.Length -eq 1 ) {
        $local:skipFirst = $true
        switch( $p[0] ) {
            '&' { $AsJob = -not $SingleProject }
            '!' { $Wait  = -not $SingleProject }
            default { $skipFirst = $false }
        }
        if( $skipFirst ) { $p = $p | Select-Object -Skip 1 }
    }

    $local:dcParams=@()
    $local:dcParams = $p | Where-Object { $_ } | ForEach-Object {
        if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ }
        else { "'$($_.Replace( "'", "''" ))'" }
    }
 
    $dcPathList | ForEach-Object {
        $local:dcPath = Join-Path $_ 'docker-compose.yml'
        if( -not (Test-Path $dcPath) ) { if( $Recurse ) { return } else { throw "ERROR: dcPath ($dcPath) does not exists" } }
       
        if( $Wait ) {
            if( $WhatIfPreference ) {
                Write-Host -ForegroundColor DarkCyan "Wait for bg jobs to complete !"
            } else {
                CompleteJobs
            }
        } 

        if( -not $dcParams ) { continue }

        $local:nextActionDescription = "In $dcPath Run $(if($AsJob){"(bg) "})with: $dcParams"
        if( $WhatIfPreference ) {
            Write-Host -ForegroundColor DarkYellow $nextActionDescription
        } else {
            Write-Verbose $nextActionDescription

            if( $AsJob ) {
                $JobQueue += Start-Job -ArgumentList @($dcPath,$dcParams) -ScriptBlock {
                    param($dcPath,$dcParams)
                    [PSCustomObject] $([ordered]@{
                        Path = $dcPath
                        Params = $dcParams
                        Results = $(& docker-compose --file $dcPath $dcParams)
                    })
                }
            } else {
                & docker-compose --file $dcPath $dcParams
            }
        }
    }
}

# if( $WhatIfPreference ) { return }

CompleteJobs
if( $CleanupJobQueue ) {
    $CleanupJobQueue | Remove-Job
}