# $global:apiToken = "APP-TOKEN-GENERATED-BY-YOUR-APPCENTER-ACCOUNT"
# $global:appName = "THE NAME OF THE APP IN APPCENTER (You can find this in the URL of the app. eg: account/app-name)"
# $global:branchName = "THE BRANCH YOU HAVE CONFIGURED TO BUILD (eg feature/addAuth)"

function Login(){
    appcenter login --token $global:apiToken
}

try{

    ## Login to AppCenter
    Login

    ## Get configured branches
    $configuredBranches = appcenter build branches list -a $global:appName --output json | ConvertFrom-Json

    ## Only branches configured on AppCenter can be built
    ## If branch is not configured, exit with a failure
    if(($configuredBranches | Where-Object { $_.sourceBranch -eq "$global:branchName" } | Measure-Object).Count -eq 0){
        Write-Host "The branch $global:branchName has not been configured yet. Please do so on Appcenter." -ForegroundColor Red
        Write-Host "The branch $global:branchName has not been configured yet. If the branch has been configured, manually start the first build for the CLI to pick it up." -ForegroundColor Red

        $Host.SetShouldExit(1)
        exit 1
    }
    else{

        ## Kick off a new build
        appcenter build queue --app $global:appName -b $global:branchName
        Write-Host "Build has been queued on AppCenter"

        $buildComplete = $false;

        ## WHILE BUILD IS IN PROGRESS
        while ($buildComplete -eq $false) {

            ## Get the latest status for the build that was just queued
            $buildStatus = appcenter build branches show -a $global:appName -b $global:branchName --output json | ConvertFrom-Json

            ## If the build status is completed
            if($buildStatus.status -eq "completed"){

                ## Build has completed
                if($buildStatus.result -eq "succeeded"){
                    Write-Host "Build has complete and the app is being processed on the Playstore"

                    ## If this is a UAT build, notify collaborators of new build
                    if($global:appName -eq "geradebbd.co.za/rtc-mobile-uat"){

                        ## Get all releases
                        $releases = appcenter distribute releases list -a $global:appName --output json | ConvertFrom-Json

                        ## Get the release for this build
                        $builRelease = $releases | Where-Object  { $_.version -eq "$($buildStatus.buildNumber)" }

                        ## If there was a release for this build, notify collaborators
                        if(!($null -eq $builRelease)){

                            Write-Host "Distributing and notifying collaborators of new build available for testing"
                            appcenter distribute releases add-destination -d "Collaborators" -t "group" -r $builRelease.id -a $global:appName
                        }
                        else{
                            Write-Host "No release for build. Collaborators will not be notified."
                        }
                    }
                }
                elseif($buildStatus.result -eq "failed"){

                    ## If the build failed on AppCenter, exit CI job with a failure
                    Write-Host "Build failed on AppCenter. Please login to AppCenter to view the logs."
                    $Host.SetShouldExit(1)
                    exit 1
                }
                elseif($buildStatus.result -eq "canceled"){

                    ##Build was canceled on AppCenter
                    Write-Host "Build was canceled on AppCenter."
                }
                
                ## Break out of while loop
                $buildComplete = $true
            }
            else{

                ## Notify CI logs that build is still in progress
                Write-Host "Build is still in progress"

                ## sleep for 15 seconds before checking for status again
                Start-Sleep -Seconds 15
            }
        }
    }
    


}catch {
    Write-Host $_.Exception
    $Host.SetShouldExit(1)
    exit 1
}