<?php
require_once __DIR__ . '/assets/vendor/phpqrcode/qrlib.php';

// Test format encoding directly
$ref = new ReflectionClass('QRcode');

// Test encodeFormat
$encodeFormat = $ref->getMethod('encodeFormat');
$encodeFormat->setAccessible(true);

// For EC level M, mask 6: formatBits = (0 << 3) | 6 = 6
$formatBits = (0 << 3) | 6;
echo "Input format bits: $formatBits = " . str_pad(decbin($formatBits), 5, '0', STR_PAD_LEFT) . "\n";

$result = $encodeFormat->invoke(null, $formatBits);
echo "Encoded format: $result = " . str_pad(decbin($result), 15, '0', STR_PAD_LEFT) . "\n";
echo "Format hex: 0x" . dechex($result) . "\n";

// The expected format info for EC=M mask=6 should be:
// 100111110010111
echo "Expected:       " . "100111110010111" . "\n";
echo "Got:            " . str_pad(decbin($result), 15, '0', STR_PAD_LEFT) . "\n";
echo "Match: " . (str_pad(decbin($result), 15, '0', STR_PAD_LEFT) === "100111110010111" ? "YES" : "NO") . "\n\n";

// Now test the full encode and try to extract data
$ref2 = new ReflectionClass('QRcode');
$encodeMethod = $ref2->getMethod('encode');
$encodeMethod->setAccessible(true);
$url = 'http://localhost/tap2order/menu.php?r=big-mama';
$matrix = $encodeMethod->invoke(null, $url);
$size = count($matrix);

echo "Matrix size: {$size}x{$size} (version " . (($size - 17) / 4) . ")\n\n";

// Read format info from matrix properly
// Top-left horizontal: row 8, cols 0-8 (skip col 6 which is timing)
$bits = '';
// Read from two places and compare
echo "=== Reading format info ===\n";

// Location 1: bottom-left (column 8, rows size-1 down to size-8)
echo "Bottom-left vertical (col 8): ";
$fmt = '';
for ($i = 0; $i < 6; $i++) $fmt .= $matrix[$size - 1 - $i][8] ? '1' : '0';
$fmt .= $matrix[$size - 8][8] ? '1' : '0';
$fmt .= $matrix[$size - 7][8] ? '1' : '0';
echo " $fmt\n";

// Location 2: top-left (row 8, cols 0-7)
echo "Top-left horizontal (row 8):  ";
$fmt2 = '';
for ($i = 0; $i < 6; $i++) $fmt2 .= $matrix[8][$i] ? '1' : '0';
$fmt2 .= $matrix[8][7] ? '1' : '0';
$fmt2 .= $matrix[8][8] ? '1' : '0';
echo " $fmt2\n";

// Location 3: top-right horizontal (row 8, cols size-7 to size-1)  
echo "Top-right horizontal:         ";
$fmt3 = '';
for ($i = 0; $i < 8; $i++) $fmt3 .= $matrix[8][$size - 8 + $i] ? '1' : '0';
echo " $fmt3\n";

// Location 4: top-left vertical (col 8, rows 0-7)
echo "Top-left vertical (col 8):    ";
$fmt4 = '';
for ($i = 0; $i < 7; $i++) $fmt4 .= $matrix[$i][8] ? '1' : '0';
$fmt4 .= $matrix[8][8] ? '1' : '0';
echo " $fmt4\n";

// Expected format strings for EC level M
$expected = [
    'M0' => '101010000010010',
    'M1' => '101000100100101',
    'M2' => '101111001111100',
    'M3' => '101101101001011',
    'M4' => '100010111111001',
    'M5' => '100000011001110',
    'M6' => '100111110010111',
    'M7' => '100101010100000',
];

echo "\n=== Expected format strings for EC=M ===\n";
foreach ($expected as $k => $v) echo "$k: $v\n";

// Also try to place format info and re-read
$placeFormat = $ref2->getMethod('placeFormatInfo');
$placeFormat->setAccessible(true);
$testMatrix = $matrix;
$placeFormat->invoke(null, $testMatrix, 6, $size);

echo "\n=== After placeFormatInfo with mask=6 ===\n";
echo "Row 8: ";
for ($c = 0; $c < $size; $c++) echo $testMatrix[8][$c] ? '1' : '0';
echo "\nCol 8: ";
for ($r = 0; $r < $size; $r++) echo $testMatrix[$r][8] ? '1' : '0';
echo "\n";
