param(
  [int]$GatewayPort = 18889,
  [string]$GatewayToken = 'qjc-bundled-runtime-token',
  [int]$SmokePort = 4318,
  [string]$SmokeToken = 'smoke-token',
  [string]$BaseOutputDir,
  [int]$TimeoutSeconds = 180,
  [switch]$SkipMaterializeRuntime
)

$ErrorActionPreference = 'Stop'
$utf8NoBom = New-Object System.Text.UTF8Encoding $false

function New-SmokePdfFixture {
  param(
    [string]$FilePath
  )

  $directory = Split-Path -Parent $FilePath
  if ($directory) {
    New-Item -ItemType Directory -Force -Path $directory | Out-Null
  }

  $objects = @(
    '1 0 obj' + "`n" +
    '<< /Type /Catalog /Pages 2 0 R >>' + "`n" +
    'endobj' + "`n",

    '2 0 obj' + "`n" +
    '<< /Type /Pages /Kids [3 0 R] /Count 1 >>' + "`n" +
    'endobj' + "`n",

    '3 0 obj' + "`n" +
    '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 320 160] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>' + "`n" +
    'endobj' + "`n",

    '4 0 obj' + "`n" +
    '<< /Length 48 >>' + "`n" +
    'stream' + "`n" +
    'BT' + "`n" +
    '/F1 18 Tf' + "`n" +
    '36 96 Td' + "`n" +
    '(QJClaw PDF smoke fixture) Tj' + "`n" +
    'ET' + "`n" +
    'endstream' + "`n" +
    'endobj' + "`n",

    '5 0 obj' + "`n" +
    '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>' + "`n" +
    'endobj' + "`n"
  )

  $builder = New-Object System.Text.StringBuilder
  [void]$builder.Append("%PDF-1.4`n")
  $offsets = @()
  foreach ($objectText in $objects) {
    $offsets += [System.Text.Encoding]::ASCII.GetByteCount($builder.ToString())
    [void]$builder.Append($objectText)
  }

  $xrefOffset = [System.Text.Encoding]::ASCII.GetByteCount($builder.ToString())
  [void]$builder.Append("xref`n")
  [void]$builder.Append("0 6`n")
  [void]$builder.Append("0000000000 65535 f `n")
  foreach ($offset in $offsets) {
    [void]$builder.Append(([string]::Format('{0:0000000000} 00000 n `n', $offset)))
  }
  [void]$builder.Append("trailer`n")
  [void]$builder.Append("<< /Size 6 /Root 1 0 R >>`n")
  [void]$builder.Append("startxref`n")
  [void]$builder.Append("$xrefOffset`n")
  [void]$builder.Append("%%EOF`n")

  [System.IO.File]::WriteAllText($FilePath, $builder.ToString(), $utf8NoBom)
}

$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
if (-not $BaseOutputDir) {
  $BaseOutputDir = Join-Path $repoRoot '.tmp\douyin-expert-pdf-attachment-smoke'
}

$BaseOutputDir = [System.IO.Path]::GetFullPath($BaseOutputDir)
$pdfFixtureDir = Join-Path $repoRoot '.tmp\pdf-fixtures'
$pdfFixturePath = Join-Path $pdfFixtureDir 'douyin-smoke-attachment.pdf'
New-SmokePdfFixture -FilePath $pdfFixturePath

$argumentList = @(
  '-ExecutionPolicy', 'Bypass',
  '-File', (Join-Path $repoRoot 'build\scripts\douyin-expert-cloud-bundle-smoke.ps1'),
  '-GatewayPort', $GatewayPort,
  '-GatewayToken', $GatewayToken,
  '-SmokePort', $SmokePort,
  '-SmokeToken', $SmokeToken,
  '-BaseOutputDir', $BaseOutputDir,
  '-AttachmentFixturePath', $pdfFixturePath,
  '-TimeoutSeconds', $TimeoutSeconds
)

if ($SkipMaterializeRuntime) {
  $argumentList += '-SkipMaterializeRuntime'
}

powershell @argumentList
exit $LASTEXITCODE
