<?php
/**
 * histogrammenotes.php
 *
 * Defines histo() which draws a bar chart and echoes it directly into the page
 * as an inline base64 <img> tag.
 *
 * Why base64 instead of saving a file?
 *   - No writable ../graph/ directory required
 *   - No stale image from a previous request
 *   - Works correctly wherever this file is include()'d from
 */

function histo($x, $y, $donn, $texte, $titre, $moyencl)
{
    if (empty($donn)) return;

    // ── Font (TTF optional — falls back to built-in GD fonts) ────────────────
    // __DIR__ resolves to THIS file's directory even when include()'d from elsewhere
    $font   = __DIR__ . '/font/OpenSans-Regular.ttf';
    $useTTF = file_exists($font) && function_exists('imagettftext');

    // ── Helper: draw text ─────────────────────────────────────────────────────
    // Tries TTF first; if font missing or FreeType not compiled, uses built-in font
    function drawText($image, $size, $angle, $px, $py, $colour, $font, $useTTF, $text)
    {
        $text = (string)$text;
        if ($useTTF && @imagettftext($image, $size, $angle, $px, $py, $colour, $font, $text) !== false) {
            return;
        }
        // Built-in GD fallback — no external file needed
        $gdf = $size >= 14 ? 4 : ($size >= 10 ? 3 : 2);
        if ($angle != 0) {
            imagestringup($image, $gdf, $px, $py, $text, $colour);
        } else {
            imagestring($image, $gdf, $px, $py - imagefontheight($gdf) + 1, $text, $colour);
        }
    }

    // ── Canvas ────────────────────────────────────────────────────────────────
    $image = imagecreatetruecolor($x, $y);   // truecolor = better anti-aliasing

    // Background
    $bg    = imagecolorallocate($image, 235, 240, 250);
    imagefilledrectangle($image, 0, 0, $x, $y, $bg);

    // Colours
    $blanc  = imagecolorallocate($image, 255, 255, 255);
    $navy   = imagecolorallocate($image,  26,  41,  66);
    $rouge  = imagecolorallocate($image, 200,  50,  50);
    $noir   = imagecolorallocate($image,   0,   0,   0);
    $pink   = imagecolorallocate($image, 243, 184, 250);
    $green  = imagecolorallocate($image,  26, 122,  74);
    $fillG  = imagecolorallocate($image, 180, 230, 200);
    $fillB  = imagecolorallocate($image, 190, 210, 245);

    // ── Layout ───────────────────────────────────────────────────────────────
    $nbcol     = count($donn);
    $maxi      = max($donn);
    if ($maxi == 0) $maxi = 20;

    $padLeft   = 15;
    $padRight  = 15;
    $padTop    = 30;   // space above tallest bar for value label
    $padBottom = 85;   // space below baseline for rotated names

    $YO    = $y - $padBottom;                          // baseline Y
    $coeff = ($y - $padBottom - $padTop) / $maxi;
    $larg  = ($x - $padLeft - $padRight) / $nbcol;

    // ── Horizontal grid lines ─────────────────────────────────────────────────
    $gridCol = imagecolorallocate($image, 200, 210, 225);
    for ($g = 0; $g <= 20; $g += 5) {
        $gy = (int)($YO - $coeff * $g);
        imageline($image, $padLeft, $gy, $x - $padRight, $gy, $gridCol);
        drawText($image, 8, 0, 2, $gy - 4, $navy, $font, $useTTF, $g);
    }

    // ── Bars + labels ─────────────────────────────────────────────────────────
    for ($i = 0; $i < $nbcol; $i++) {
        $bx1 = (int)($padLeft + $larg * $i + 2);
        $bx2 = (int)($padLeft + $larg * ($i + 1) - 2);
        $by1 = (int)($YO - $coeff * $donn[$i]);
        $by2 = $YO;

        // Fill colour by grade band
        if ($donn[$i] > 0 && $donn[$i] < 10) {
            imagefilledrectangle($image, $bx1, $by1, $bx2, $by2, $pink);
            imagerectangle($image,       $bx1, $by1, $bx2, $by2, $rouge);
        } elseif ($donn[$i] >= 14) {
            imagefilledrectangle($image, $bx1, $by1, $bx2, $by2, $fillG);
            imagerectangle($image,       $bx1, $by1, $bx2, $by2, $green);
        } else {
            imagefilledrectangle($image, $bx1, $by1, $bx2, $by2, $fillB);
            imagerectangle($image,       $bx1, $by1, $bx2, $by2, $navy);
        }

        // Value above bar
        $valX = (int)($bx1 + ($bx2 - $bx1) / 2 - 8);
        drawText($image, 9, 0, $valX, $by1 - 4, $noir, $font, $useTTF, $donn[$i]);

        // Name below baseline, rotated 90°
        $nameX = (int)($bx1 + ($bx2 - $bx1) / 2 + 5);
        drawText($image, 9, 90, $nameX, $YO + $padBottom - 5, $navy, $font, $useTTF, $texte[$i]);
    }

    // ── Baseline ─────────────────────────────────────────────────────────────
    imagesetthickness($image, 2);
    imageline($image, $padLeft, $YO, $x - $padRight, $YO, $noir);
    imagesetthickness($image, 1);

    // ── Class-average line ────────────────────────────────────────────────────
    $yd = (int)($YO - $coeff * $moyencl);
    imagesetthickness($image, 2);
    imageline($image, $padLeft, $yd, $x - $padRight, $yd, $rouge);
    imagesetthickness($image, 1);
    drawText($image, 9, 0, $padLeft + 4, $yd - 12, $rouge, $font, $useTTF, 'Moy. classe = ' . $moyencl);

    // ── Title ─────────────────────────────────────────────────────────────────
    drawText($image, 13, 0, (int)($x / 2 - strlen($titre) * 4), $y - 8, $navy, $font, $useTTF, $titre);

    // ── Output as inline base64 (no file needed, no broken-image risk) ────────
    ob_start();
    imagepng($image);
    $raw = ob_get_clean();
    imagedestroy($image);

    echo '<img src="data:image/png;base64,' . base64_encode($raw) . '" '
       . 'style="max-width:100%;height:auto;display:block;" alt="Histogramme des notes" />';
}
