<?php

declare(strict_types=1);

echo "=== Verifying Format Information Placement ===\n\n";

// For EC Level M, Mask Pattern 0
// Format bits (raw): 00 000 = 00000
// After BCH error correction: 101010000010010 (15 bits)
$formatBits = 0b101010000010010;

echo "Format Bits for EC Level M, Mask Pattern 0:\n";
echo sprintf("Binary: %015b\n", $formatBits);
echo sprintf("Hex: 0x%04X\n\n", $formatBits);

echo "Bit-by-bit breakdown:\n";
for ($i = 0; $i < 15; $i++) {
    $bit = ($formatBits >> (14 - $i)) & 1;
    echo sprintf("Bit %2d: %d\n", $i, $bit);
}

echo "\n=== ISO/IEC 18004:2015 Section 7.9 ===\n\n";

echo "HORIZONTAL Placement (Row 8):\n";
echo "Bit  0 → Column 0\n";
echo "Bit  1 → Column 1\n";
echo "Bit  2 → Column 2\n";
echo "Bit  3 → Column 3\n";
echo "Bit  4 → Column 4\n";
echo "Bit  5 → Column 5\n";
echo "Bit  6 → Column 7 (skip column 6 - timing)\n";
echo "Bit  7 → Column 8\n";
echo "Bit  8 → Column 20 (size-1)\n";
echo "Bit  9 → Column 19 (size-2)\n";
echo "Bit 10 → Column 18 (size-3)\n";
echo "Bit 11 → Column 17 (size-4)\n";
echo "Bit 12 → Column 16 (size-5)\n";
echo "Bit 13 → Column 15 (size-6)\n";
echo "Bit 14 → Column 14 (size-7)\n";

echo "\n\nVERTICAL Placement (Column 8):\n";
echo "According to ISO/IEC 18004:2015:\n";
echo "Bit  0 → Row 20 (size-1)\n";
echo "Bit  1 → Row 19 (size-2)\n";
echo "Bit  2 → Row 18 (size-3)\n";
echo "Bit  3 → Row 17 (size-4)\n";
echo "Bit  4 → Row 16 (size-5)\n";
echo "Bit  5 → Row 15 (size-6)\n";
echo "Bit  6 → Row 14 (size-7)\n";
echo "Bit  7 → Row 8\n";
echo "Bit  8 → Row 7\n";
echo "Bit  9 → Row 5 (skip row 6 - timing)\n";
echo "Bit 10 → Row 4\n";
echo "Bit 11 → Row 3\n";
echo "Bit 12 → Row 2\n";
echo "Bit 13 → Row 1\n";
echo "Bit 14 → Row 0\n";

echo "\n\n=== Current Implementation Check ===\n\n";

$size = 21;

// Horizontal
echo "Horizontal (current implementation):\n";
$columns = [0, 1, 2, 3, 4, 5, 7, 8, 20, 19, 18, 17, 16, 15, 14];
for ($i = 0; $i < 15; $i++) {
    echo sprintf("Bit %2d → Row 8, Column %2d\n", $i, $columns[$i]);
}

echo "\n\nVertical (current implementation):\n";

// Bits 0-6
for ($i = 0; $i < 7; $i++) {
    $row = $size - 1 - $i;
    echo sprintf("Bit %2d → Row %2d, Column 8\n", $i, $row);
}

// Bits 7-14
$rows = [8, 7, 5, 4, 3, 2, 1, 0];
for ($i = 0; $i < 8; $i++) {
    echo sprintf("Bit %2d → Row %2d, Column 8\n", 7 + $i, $rows[$i]);
}

echo "\n\n=== Comparison ===\n";
echo "✅ Horizontal placement appears CORRECT\n";
echo "✅ Vertical placement appears CORRECT\n";
echo "\nBoth horizontal and vertical Format Info placements follow ISO spec.\n";
