param(
    [int]$maxCount = 5
)

$root = "h:\Applications\Aycode\Source\AyCode.Core"
$output = Join-Path $root "AllBenchmarksDropdown.html"

if (Test-Path $output) { Remove-Item $output }

$htmlFiles = Get-ChildItem -Path $root -Recurse -Filter "*-report.html" | Sort-Object LastWriteTime -Descending | Select-Object -First $maxCount

@"
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <title>BenchmarkDotNet Riportok (Dropdown)</title>
    <style>
        body { font-family: Segoe UI, Arial, sans-serif; margin: 2em; background: #f8f9fa; }
        select { font-size: 1.1em; padding: 0.2em; }
        .report-content { background: #fff; border: 1px solid #ccc; padding: 1em; margin-top: 1em; border-radius: 6px; box-shadow: 0 2px 8px #0001; min-height: 200px; }
        h1 { font-size: 1.5em; }
        .filename { color: #888; font-size: 0.95em; }
        .compare { color: #007700; font-size: 1.1em; margin-bottom: 1em; }
        .compare.negative { color: #bb2222; }
        iframe { width: 100%; min-height: 600px; border: none; background: #fff; }
    </style>
</head>
<body>
    <h1>BenchmarkDotNet Riportok</h1>
    <label for='reportSelect'>Válassz riportot:</label>
    <select id='reportSelect'>
"@ | Set-Content -Encoding UTF8 -Path $output

$reports = @()
foreach ($file in $htmlFiles) {
    $id = ([System.IO.Path]::GetFileNameWithoutExtension($file.Name) + "_" + [Math]::Abs($file.FullName.GetHashCode()))
    $runName = ($file.FullName -replace "^.*SwitcherRun_([\w\dT]+).*", 'SwitcherRun_$1')
    if ($runName -eq $file.FullName) { $runName = $file.BaseName }
    $relPath = $file.FullName.Substring($root.Length + 1).Replace("\", "/")
    $reports += @{ Id = $id; Title = $runName; Path = $relPath }
    Add-Content -Encoding UTF8 -Path $output -Value "        <option value='$id'>$runName</option>"
}

@"
    </select>
    <div class='filename' id='filename'></div>
    <div class='compare' id='compare'></div>
    <div class='report-content'><iframe id='reportFrame'></iframe></div>
    <script>
        // Relatív riport fájlok
        const reports = {
"@ | Add-Content -Encoding UTF8 -Path $output

$means = @{}
foreach ($r in $reports) {
    Add-Content -Encoding UTF8 -Path $output -Value "            '$($r.Id)': '$($r.Path)',"
    $html = Get-Content (Join-Path $root $r.Path) -Raw
    $mean = [regex]::Match($html, '(?<=<td>Mean</td><td>)[0-9.,]+').Value
    $means[$r.Id] = $mean
}

@"
        };
        const means = {
"@ | Add-Content -Encoding UTF8 -Path $output

foreach ($r in $reports) {
    $mean = $means[$r.Id]
    Add-Content -Encoding UTF8 -Path $output -Value "            '$($r.Id)': '$mean',"
}

@"
        };
        const select = document.getElementById('reportSelect');
        const frame = document.getElementById('reportFrame');
        const filename = document.getElementById('filename');
        const compare = document.getElementById('compare');
        function showReport() {
            const id = select.value;
            if (reports[id]) {
                frame.src = reports[id];
            } else {
                frame.srcdoc = '<i>Nincs tartalom.</i>';
            }
            const opt = select.options[select.selectedIndex];
            filename.textContent = opt ? opt.text : '';
            // Összehasonlítás az eggyel korábbival
            const idx = select.selectedIndex;
            if (idx < select.options.length - 1) {
                const prevId = select.options[idx + 1].value;
                const currMean = parseFloat(means[id].replace(',','.'));
                const prevMean = parseFloat(means[prevId].replace(',','.'));
                if (!isNaN(currMean) && !isNaN(prevMean) && prevMean > 0) {
                    const diff = currMean - prevMean;
                    const percent = (diff / prevMean * 100).toFixed(2);
                    const sign = percent > 0 ? '+' : '';
                    compare.textContent = `Eltérés az előzőhöz képest: ${sign}${percent}% (${currMean} vs ${prevMean})`;
                    compare.className = 'compare' + (percent > 0 ? ' negative' : '');
                } else {
                    compare.textContent = '';
                }
            } else {
                compare.textContent = '';
            }
        }
        select.addEventListener('change', showReport);
        window.onload = showReport;
    </script>
</body>
</html>
"@ | Add-Content -Encoding UTF8 -Path $output

Write-Host "Összefűzve: $output"