param(
    [string]$ProjectRoot = 'C:\HyperTwist',
    [string]$ArchiveDirectory = 'C:\HyperTwist\packaged\desktop',
    [ValidateSet('Development', 'Shipping')]
    [string]$Configuration = 'Development',
    [string]$CookMap = '/Game/HyperTwistTraining/Maps/L_HyperTwist_ClassicTraining',
    [string[]]$AdditionalCookMaps = @(
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_FollowAlongTraining',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_2x2x2x2Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_3x3x3x3Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_4x4x4x4Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_5x5x5x5Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_6x6x6x6Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_Magic120CellTraining',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube5DTraining'
    ),
    [string[]]$SmokeMaps = @(
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_ClassicTraining',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_FollowAlongTraining',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_2x2x2x2Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_3x3x3x3Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_4x4x4x4Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_5x5x5x5Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube4D_6x6x6x6Training',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_Magic120CellTraining',
        '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube5DTraining'
    ),
    [string[]]$AdditionalCookerOptions = @(
        '-DisablePlugins=MovieRenderPipeline',
        '-SkipCookingEditorContent'
    ),
    [bool]$HeadlessSmoke = $true,
    [string]$ValidationReportPath = '',
    [string]$LaunchSurfaceReportPath = '',
    [string]$SolverTablePath = '',
    [Int64]$ExpectedSolverTableSizeBytes = 676207080,
    [string]$ExpectedSolverTableSha256 = 'dc6de3d909a37afe2ba7f1f978543a13eae215bd20d954d66813524a8ca20034',
    [string]$BrowserShellSourceDirectory = '',
    [string]$ZipOutputPath = '',
    [string]$ZipExportReportPath = '',
    [switch]$CleanArchive,
    [switch]$SkipBuild,
    [switch]$SkipBrowserShellBuild,
    [switch]$SkipLaunch,
    [switch]$CreateZip
)

$ErrorActionPreference = 'Stop'

$ClassicPackageScriptPath = Join-Path $PSScriptRoot 'Invoke-HyperTwistClassicCubePackage.ps1'
$DesktopLaunchScriptPath = Join-Path $PSScriptRoot 'Launch-HyperTwistDesktopPackage.ps1'
$ZipExportScriptPath = Join-Path $PSScriptRoot 'Export-HyperTwistPackagedBuildZip.ps1'
$ValidationDirectory = Join-Path $ArchiveDirectory 'validation'

if (-not (Test-Path -LiteralPath $ClassicPackageScriptPath))
{
    throw "Classic-cube package helper was not found at '$ClassicPackageScriptPath'."
}

if (-not (Test-Path -LiteralPath $DesktopLaunchScriptPath))
{
    throw "Desktop launch helper was not found at '$DesktopLaunchScriptPath'."
}

if (-not (Test-Path -LiteralPath $ZipExportScriptPath))
{
    throw "Packaged-zip export helper was not found at '$ZipExportScriptPath'."
}

if ([string]::IsNullOrWhiteSpace($ValidationReportPath))
{
    $ValidationReportPath = Join-Path $ValidationDirectory 'desktop-package-validation-report.json'
}

if ([string]::IsNullOrWhiteSpace($LaunchSurfaceReportPath))
{
    $LaunchSurfaceReportPath = Join-Path $ValidationDirectory 'desktop-launch-surface-report.json'
}

if ([string]::IsNullOrWhiteSpace($ZipExportReportPath))
{
    $ZipExportReportPath = Join-Path $ValidationDirectory 'desktop-zip-export-report.json'
}

$InvokeParameters = @{
    ProjectRoot = $ProjectRoot
    ArchiveDirectory = $ArchiveDirectory
    Configuration = $Configuration
    CookMap = $CookMap
    AdditionalCookMaps = $AdditionalCookMaps
    SmokeMaps = $SmokeMaps
    AdditionalCookerOptions = $AdditionalCookerOptions
    HeadlessSmoke = $HeadlessSmoke
    ValidationReportPath = $ValidationReportPath
    SolverTablePath = $SolverTablePath
    ExpectedSolverTableSizeBytes = $ExpectedSolverTableSizeBytes
    ExpectedSolverTableSha256 = $ExpectedSolverTableSha256
}

if (-not [string]::IsNullOrWhiteSpace($BrowserShellSourceDirectory))
{
    $InvokeParameters.BrowserShellSourceDirectory = $BrowserShellSourceDirectory
}

if ($CleanArchive)
{
    $InvokeParameters.CleanArchive = $true
}

if ($SkipBuild)
{
    $InvokeParameters.SkipBuild = $true
}

if ($SkipBrowserShellBuild)
{
    $InvokeParameters.SkipBrowserShellBuild = $true
}

if ($SkipLaunch)
{
    $InvokeParameters.SkipLaunch = $true
}

& $ClassicPackageScriptPath @InvokeParameters

if (-not $SkipLaunch)
{
    $DesktopLaunchParameters = @{
        PackageRoot = $ArchiveDirectory
        ReportPath = $LaunchSurfaceReportPath
        UseNullRHI = $HeadlessSmoke
        NoSound = $HeadlessSmoke
        RequireStartupDiagnostics = $true
    }
    if (-not $HeadlessSmoke)
    {
        $DesktopLaunchParameters.ExpectedStartupDiagnosticsResult = 'launch-menu-renderable'
    }

    & $DesktopLaunchScriptPath @DesktopLaunchParameters
}

if ($CreateZip)
{
    & $ZipExportScriptPath `
        -PackageRoot $ArchiveDirectory `
        -DestinationZipPath $ZipOutputPath `
        -ReportPath $ZipExportReportPath `
        -ExpectedSolverTableSizeBytes $ExpectedSolverTableSizeBytes `
        -ExpectedSolverTableSha256 $ExpectedSolverTableSha256
}
