<?php
// admin/test_barcode.php
// Drop-in diagnostic for BCG barcode generation.
// Saves a test PNG to admin/test_output.png on success and prints detailed diagnostics.

error_reporting(E_ALL);
ini_set('display_errors', 1);

// Config: expected locations (adjust only if your BCG folder is elsewhere)
$bcg_base = __DIR__ . DIRECTORY_SEPARATOR . 'BCG' . DIRECTORY_SEPARATOR;
$class_dir = $bcg_base . 'class' . DIRECTORY_SEPARATOR;
$config_dir = $bcg_base . 'html' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR; // not required
$font_dir = $bcg_base . 'html' . DIRECTORY_SEPARATOR . 'font' . DIRECTORY_SEPARATOR;

// Files we expect (adjust if your filenames differ)
$required_files = [
    'BCGColor' => $class_dir . 'BCGColor.php',
    'BCGDrawing' => $class_dir . 'BCGDrawing.php',
    'BCGFontFile' => $class_dir . 'BCGFontFile.php',
    'BCGcode128' => $class_dir . 'BCGcode128.barcode.php'
];

echo "<h2>BCG Diagnostic</h2>";

echo "<h3>Runtime</h3>";
echo "<pre>PHP SAPI: " . php_sapi_name() . "\nPHP Version: " . PHP_VERSION . "\n</pre>";

// Check GD
echo "<h3>Extensions</h3><pre>";
echo "GD: " . (extension_loaded('gd') ? 'loaded' : 'MISSING') . "\n";
echo "Imagick: " . (extension_loaded('imagick') ? 'loaded' : 'not loaded') . "\n";
echo "MBString: " . (extension_loaded('mbstring') ? 'loaded' : 'not loaded') . "\n";
echo "</pre>";

// Check files
echo "<h3>Expected files</h3><pre>";
foreach ($required_files as $k => $f) {
    echo str_pad($k, 12) . " => " . $f . " : " . (file_exists($f) ? "FOUND" : "MISSING") . "\n";
}
echo "</pre>";

// If any missing, stop and show suggestion
$missing = array_filter($required_files, function($f){ return !file_exists($f); });
if (!empty($missing)) {
    echo "<p style='color:darkred;font-weight:bold'>One or more required class files are missing. Please upload them to the path above or edit the test to point to the correct path.</p>";
    exit;
}

// Attempt to include files and report any warnings
echo "<h3>Include test</h3><pre>";
try {
    require_once $required_files['BCGColor'];
    require_once $required_files['BCGDrawing'];
    require_once $required_files['BCGFontFile'];
    require_once $required_files['BCGcode128'];
    echo "Includes succeeded\n";
} catch (Throwable $t) {
    echo "Include failed: " . $t->getMessage() . "\n";
    exit;
}
echo "</pre>";

// Attempt to create barcode and save to file
echo "<h3>Generate test barcode</h3><pre>";
$testCode = "TEST-".time();
try {
    // Setup colors and font (font optional)
    $color_black = new BCGColor(0, 0, 0);
    $color_white = new BCGColor(255, 255, 255);
    $fontFile = false;
    // try common font locations
    $candidates = [
        $font_dir . 'Arial.ttf',
        __DIR__ . '/tcpdf/fonts/freeserif.ttf',
        __DIR__ . '/BCG/html/font/Arial.ttf',
    ];
    foreach ($candidates as $c) {
        if ($c && file_exists($c)) { $fontFile = $c; break; }
    }
    echo "Font used: " . ($fontFile ? $fontFile : 'NONE (no font file found)') . "\n";

    if ($fontFile) {
        $font = new BCGFontFile($fontFile, 10);
    } else {
        $font = null;
    }

    // instantiate barcode class (code128)
    $codebar = new BCGcode128();
    if ($font) $codebar->setFont($font);
    $codebar->setScale(2);
    $codebar->setThickness(30);
    $codebar->setForegroundColor($color_black);
    $codebar->setBackgroundColor($color_white);
    $codebar->parse($testCode);

    $drawing = new BCGDrawing('', $color_white);
    $drawing->setBarcode($codebar);
    $drawing->setDPI(72);
    $drawing->draw();

    // Save output to a file instead of sending headers so we can inspect permissions
    $outFile = __DIR__ . DIRECTORY_SEPARATOR . 'test_output.png';
    $ok = $drawing->finish(BCGDrawing::IMG_FORMAT_PNG, $outFile);

    if ($ok || file_exists($outFile)) {
        echo "WROTE PNG: " . $outFile . "\n";
        echo "File size: " . filesize($outFile) . " bytes\n";
        echo "</pre>";
        echo "<p>Open this generated image directly: <a href='test_output.png' target='_blank'>test_output.png</a></p>";
        exit;
    } else {
        echo "Finish returned false and file was not created.\n";
    }
} catch (Exception $e) {
    echo "Exception during draw: " . $e->getMessage() . "\n";
    echo $e;
}
echo "</pre>";
?>
