<?php

declare(strict_types=1);

require_once __DIR__ . '/../../vendor/autoload.php';

use App\Framework\QrCode\QrCodeGenerator;
use App\Framework\QrCode\Structure\AlignmentPattern;
use App\Framework\QrCode\Structure\FinderPattern;
use App\Framework\QrCode\Structure\FormatInformation;
use App\Framework\QrCode\Structure\TimingPattern;
use App\Framework\QrCode\ValueObjects\EncodingMode;
use App\Framework\QrCode\ValueObjects\ErrorCorrectionLevel;
use App\Framework\QrCode\ValueObjects\Module;
use App\Framework\QrCode\ValueObjects\QrCodeMatrix;
use App\Framework\QrCode\ValueObjects\QrCodeVersion;
use App\Framework\QrCode\ErrorCorrection\ReedSolomonEncoder;
use App\Framework\QrCode\Masking\MaskEvaluator;
use App\Framework\QrCode\Masking\MaskPattern;

echo "=== Testing All 8 Mask Patterns ===\n\n";

$data = "A";
$version = QrCodeVersion::fromNumber(1);
$size = 21;

// Data encoding for "A"
// Mode: 0100 (BYTE)
// Length: 00000001 (1 character)
// Data: 01000001 ('A' = 0x41)
// Terminator: 0000
$dataCodewords = [
    0x40,  // 01000000 - Mode indicator (BYTE) + first 4 bits of length
    0x10,  // 00010000 - Last 4 bits of length (1) + padding
    0x41,  // 01000001 - 'A'
    0x00,  // Terminator
    0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11,  // Padding pattern
    0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11
];

// Generate EC codewords
$reedSolomon = new ReedSolomonEncoder();
$ecInfo = ReedSolomonEncoder::getECInfo(1, 'M');
$ecCodewords = $reedSolomon->encode($dataCodewords, $ecInfo['ecCodewords']);

$allCodewords = array_merge($dataCodewords, $ecCodewords);

// Test all 8 mask patterns
for ($maskPatternValue = 0; $maskPatternValue < 8; $maskPatternValue++) {
    echo "Testing Mask Pattern {$maskPatternValue}...\n";

    // 1. Create empty matrix
    $matrix = QrCodeMatrix::create($version);

    // 2. Add finder patterns
    $matrix = FinderPattern::apply($matrix);
    $matrix = FinderPattern::applySeparators($matrix);

    // 3. Add alignment patterns (none for Version 1)
    $matrix = AlignmentPattern::apply($matrix);

    // 4. Add timing patterns
    $matrix = TimingPattern::apply($matrix);

    // 5. Add dark module
    $darkModuleRow = 4 * 1 + 9;
    $matrix = $matrix->setModuleAt($darkModuleRow, 8, Module::dark());

    // 6. Place data codewords
    $matrix = placeDataCodewords($matrix, $allCodewords);

    // 7. Apply specific mask pattern
    $maskEvaluator = new MaskEvaluator();
    $maskPattern = MaskPattern::from($maskPatternValue);
    $matrix = $maskEvaluator->applyMask($matrix, $maskPattern);

    // 8. Add format information
    $matrix = FormatInformation::apply($matrix, ErrorCorrectionLevel::M, $maskPatternValue);

    // 9. Generate PNG
    $scale = 20;
    $quietZone = 4;
    $totalSize = ($size + 2 * $quietZone) * $scale;

    $image = imagecreate($totalSize, $totalSize);
    $white = imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);

    imagefill($image, 0, 0, $white);

    for ($row = 0; $row < $size; $row++) {
        for ($col = 0; $col < $size; $col++) {
            if ($matrix->getModuleAt($row, $col)->isDark()) {
                $x = ($quietZone + $col) * $scale;
                $y = ($quietZone + $row) * $scale;

                for ($dy = 0; $dy < $scale; $dy++) {
                    for ($dx = 0; $dx < $scale; $dx++) {
                        imagesetpixel($image, $x + $dx, $y + $dy, $black);
                    }
                }
            }
        }
    }

    $filename = "qrcode-PATTERN{$maskPatternValue}-A.png";
    imagepng($image, "/var/www/html/public/{$filename}", 0);
    imagedestroy($image);

    echo "  ✓ Generated: {$filename}\n";
    echo "  URL: https://localhost/{$filename}\n\n";
}

echo "=== All 8 Patterns Generated ===\n\n";
echo "Bitte testen Sie jeden QR-Code:\n";
for ($i = 0; $i < 8; $i++) {
    echo "Pattern {$i}: https://localhost/qrcode-PATTERN{$i}-A.png\n";
}

// Helper function to check if position is function pattern
function isFunctionPattern(int $row, int $col, int $size): bool
{
    // Finder patterns + separators (top-left, top-right, bottom-left)
    if ($row < 9 && $col < 9) return true;  // Top-left
    if ($row < 9 && $col >= $size - 8) return true;  // Top-right
    if ($row >= $size - 8 && $col < 9) return true;  // Bottom-left

    // Timing patterns
    if ($row === 6 || $col === 6) return true;

    // Dark module
    if ($row === 13 && $col === 8) return true;

    // Format information (row 8 and column 8)
    if ($row === 8 && ($col < 9 || $col >= $size - 8)) return true;
    if ($col === 8 && ($row < 9 || $row >= $size - 7)) return true;

    return false;
}

// Helper function to place data codewords
function placeDataCodewords(QrCodeMatrix $matrix, array $codewords): QrCodeMatrix
{
    $size = $matrix->getSize();
    $bitIndex = 0;
    $totalBits = count($codewords) * 8;

    // Start from bottom-right, move up in columns of 2
    for ($col = $size - 1; $col > 0; $col -= 2) {
        if ($col === 6) {
            $col--; // Skip timing column
        }

        for ($row = $size - 1; $row >= 0; $row--) {
            for ($c = 0; $c < 2; $c++) {
                $currentCol = $col - $c;

                if (isFunctionPattern($row, $currentCol, $size)) {
                    continue;
                }

                if ($bitIndex >= $totalBits) {
                    break 3;
                }

                $byteIndex = (int)($bitIndex / 8);
                $bitPosition = 7 - ($bitIndex % 8);
                $bit = ($codewords[$byteIndex] >> $bitPosition) & 1;

                $module = $bit === 1 ? Module::dark() : Module::light();
                $matrix = $matrix->setModuleAt($row, $currentCol, $module);

                $bitIndex++;
            }
        }

        // Move up, then down
        for ($row = 0; $row < $size; $row++) {
            for ($c = 0; $c < 2; $c++) {
                $currentCol = $col - $c;

                if (isFunctionPattern($row, $currentCol, $size)) {
                    continue;
                }

                if ($bitIndex >= $totalBits) {
                    break 3;
                }

                $byteIndex = (int)($bitIndex / 8);
                $bitPosition = 7 - ($bitIndex % 8);
                $bit = ($codewords[$byteIndex] >> $bitPosition) & 1;

                $module = $bit === 1 ? Module::dark() : Module::light();
                $matrix = $matrix->setModuleAt($row, $currentCol, $module);

                $bitIndex++;
            }
        }
    }

    return $matrix;
}
