parameters:
  - name: condition
    default: true

  - name: artifactName
    type: string
    default: nuget

  - name: artifactItemPattern
    type: string
    default: '**/*.nupkg'

  - name: uploadPrefix
    type: string
    default: ''

  - name: checkoutDirectory
    type: string
    default: $(System.DefaultWorkingDirectory)

  - name: BuildPlatforms
    type: object
    default:
    - name: Windows
      poolName: $(windowsNet6VmPool)
      vmImage: $(windowsNet6VmImage)
      artifact: templates-windows
    - name: macOS
      poolName: $(macOSXNet6VmPool)
      vmImage: $(macOSXNet6VmImage)
      artifact: templates-macos

  - name: RunPlatforms
    type: object
    default:
    - name: $(androidTestsVmPool)
      vmImage: $(androidTestsVmImage)
      demands:
        - macOS.Name -equals Ventura
        - macOS.Architecture -equals x64
      testName: RunOnAndroid
      artifact: templates-run-android
    - name: $(iosTestsVmPool)
      vmImage: $(iosTestsVmImage)
      testName: RunOniOS
      artifact: templates-run-ios

  - name: prepareSteps
    type: stepList
    default: []

jobs:
- job: build_maui_templates
  workspace:
    clean: all
  displayName: 'Build platform:'
  timeoutInMinutes: 240
  condition: ${{ parameters.condition}}
  strategy:
    matrix:
      ${{ each BuildPlatform in parameters.BuildPlatforms }}:
        ${{ BuildPlatform.name }}:
          POOL_NAME: ${{ BuildPlatform.poolName }}
          POOL_VIMAGE: ${{ BuildPlatform.vmImage }}
          PLATFORM_NAME: ${{ lower(BuildPlatform.name) }}

  pool:
    name: $(POOL_NAME)
    vmImage: $(POOL_VIMAGE)
    demands:
      - macOS.Name -equals Ventura
      - macOS.Architecture -equals x64
      - Agent.OSVersion -equals 13.5
  steps:

  - ${{ each step in parameters.prepareSteps }}:
    - ${{ each pair in step }}:
        ${{ pair.key }}: ${{ pair.value }}

  - template: provision.yml
    parameters:
      checkoutDirectory: ${{ parameters.checkoutDirectory }}

  - task: DownloadBuildArtifacts@0
    displayName: 'Download Packages'
    inputs:
      artifactName: ${{ parameters.artifactName }}
      itemPattern: ${{ parameters.artifactItemPattern }}
      downloadPath: $(System.DefaultWorkingDirectory)/artifacts

  - pwsh: Move-Item -Path artifacts\${{ parameters.artifactName }}\*.nupkg -Destination artifacts -Force
    displayName: Move the downloaded artifacts

  - pwsh: ./build.ps1 --target=dotnet-local-workloads --verbosity=diagnostic
    displayName: 'Install .NET (Local Workloads)'
    retryCountOnTaskFailure: 3
    workingDirectory: ${{ parameters.checkoutDirectory }}
    env:
      DOTNET_TOKEN: $(dotnetbuilds-internal-container-read-token)
      PRIVATE_BUILD: $(PrivateBuild)

  - pwsh: ./build.ps1 --target=dotnet-integration-build --verbosity=diagnostic
    displayName: Build Microsoft.Maui.IntegrationTests

  - pwsh: ./build.ps1 --target=dotnet-integration-test --filter="FullyQualifiedName=Microsoft.Maui.IntegrationTests.TemplateTests" --resultsfilename="integration-tests" --verbosity=diagnostic
    displayName: Run $(PLATFORM_NAME) templates build tests

  - task: PublishTestResults@2
    displayName: Publish the $(PLATFORM_NAME) templates build tests
    condition: always()
    inputs:
      testRunner: VSTest
      testResultsFiles: '$(build.artifactstagingdirectory)/**/*.trx'
      testRunTitle: $(PLATFORM_NAME) templates build tests

  - pwsh: |
      Write-Host "Current job status is: $env:AGENT_JOBSTATUS"
      if ($env:AGENT_JOBSTATUS -eq "SucceededWithIssues") {
          Write-Host "##vso[task.complete result=Failed;]DONE"
      }
    displayName: Fail if any issues occurred

- ${{ each RunPlatform in parameters.RunPlatforms }}:
  - job: run_${{ RunPlatform.testName }}
    workspace:
      clean: all
    displayName: ${{ RunPlatform.testName }}
    timeoutInMinutes: 240
    condition: ${{ parameters.condition}}
    pool: ${{ RunPlatform }}
    steps:

    - ${{ each step in parameters.prepareSteps }}:
      - ${{ each pair in step }}:
          ${{ pair.key }}: ${{ pair.value }}

    - template: provision.yml
      parameters:
        skipXcode: ${{ eq(RunPlatform.testName, 'RunOnAndroid') }}
        checkoutDirectory: ${{ parameters.checkoutDirectory }}

    - task: DownloadBuildArtifacts@0
      displayName: 'Download Packages'
      inputs:
        artifactName: ${{ parameters.artifactName }}
        itemPattern: ${{ parameters.artifactItemPattern }}
        downloadPath: $(System.DefaultWorkingDirectory)/artifacts

    - pwsh: Move-Item -Path artifacts\${{ parameters.artifactName }}\*.nupkg -Destination artifacts -Force
      displayName: Move the downloaded artifacts

    - pwsh: ./build.ps1 --target=dotnet-local-workloads --verbosity=diagnostic
      displayName: 'Install .NET (Local Workloads)'
      retryCountOnTaskFailure: 3
      workingDirectory: ${{ parameters.checkoutDirectory }}
      env:
        DOTNET_TOKEN: $(dotnetbuilds-internal-container-read-token)
        PRIVATE_BUILD: $(PrivateBuild)

    # - script: dotnet tool update Microsoft.DotNet.XHarness.CLI --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json --version "9.0.0-prerelease*" -g
    #   displayName: install xharness

    - pwsh: ./build.ps1 --target=dotnet-integration-build --verbosity=diagnostic
      displayName: Build Microsoft.Maui.IntegrationTests

    - pwsh: ./build.ps1 --target=dotnet-integration-test --filter="Name=${{ RunPlatform.testName }}" --resultsfilename="integration-run-${{ RunPlatform.testName }}" --verbosity=diagnostic
      displayName: Run $(PLATFORM_NAME) templates run tests
      continueOnError: true

    - task: PublishTestResults@2
      displayName: Publish the $(PLATFORM_NAME) templates run tests
      condition: always()
      inputs:
        testRunner: VSTest
        testResultsFiles: '$(build.artifactstagingdirectory)/**/*.trx'
        testRunTitle: $(PLATFORM_NAME) templates run tests

    - pwsh: |
        Write-Host "Current job status is: $env:AGENT_JOBSTATUS"
        if ($env:AGENT_JOBSTATUS -eq "SucceededWithIssues") {
            Write-Host "##vso[task.complete result=Failed;]DONE"
        }
      displayName: Fail if any issues occurred
