<?php
  header("Content-Type: image/png");

  $carnet = $_GET["carnet"] ?? "";
  if($carnet == ""){
    echo "No se especifico carnet";
    exit;
  }

  if(!file_exists("estudiantes.txt")){
    echo "No hay registros";
    exit;
  }

  $encontrado = false;
  $recurso = fopen("estudiantes.txt","r");
  while(!feof($recurso)){
    $linea = fgets($recurso);
    if(trim($linea) != ""){
      $reg = explode("|", $linea);
      if(count($reg) == 7 && trim($reg[0]) == $carnet){
        $encontrado = true;
        break;
      }
    }
  }
  fclose($recurso);

  if(!$encontrado){
    echo "Estudiante no encontrado";
    exit;
  }

  $nombre = $reg[1];
  $notas = array(floatval($reg[2]), floatval($reg[3]), floatval($reg[4]), floatval($reg[5]), floatval($reg[6]));
  $promedio = array_sum($notas) / count($notas);

  $materias = array("Mat1", "Mat2", "Mat3", "Mat4", "Mat5", "Promedio");
  $valores = array_merge($notas, array(round($promedio, 2)));

  $ancho = 800;
  $alto = 500;
  $img = imagecreatetruecolor($ancho, $alto);

  $fondo = imagecolorallocate($img, 255, 255, 255);
  $negro = imagecolorallocate($img, 0, 0, 0);
  $gris = imagecolorallocate($img, 150, 150, 150);
  $rojo = imagecolorallocate($img, 220, 50, 50);
  $verde = imagecolorallocate($img, 50, 180, 50);

  imagefill($img, 0, 0, $fondo);

  $titulo = "Notas de: " . $nombre . " (Carnet: " . $carnet . ")";
  imagestring($img, 5, ($ancho - strlen($titulo) * imagefontwidth(5)) / 2, 10, $titulo, $negro);

  $inicioX = 80;
  $inicioY = 60;
  $anchoBarra = 70;
  $espacio = 25;
  $maxAltura = 350;
  $maxNota = 100;

  for($i = 0; $i < count($valores); $i++){
    $x = $inicioX + $i * ($anchoBarra + $espacio);
    $altura = ($valores[$i] / $maxNota) * $maxAltura;
    $y = $inicioY + ($maxAltura - $altura);

    if($valores[$i] >= 0 && $valores[$i] <= 39){
      $color = $gris;
    } elseif($valores[$i] >= 40 && $valores[$i] <= 60){
      $color = $rojo;
    } else {
      $color = $verde;
    }

    imagefilledrectangle($img, $x, $y, $x + $anchoBarra, $inicioY + $maxAltura, $color);
    imagerectangle($img, $x, $y, $x + $anchoBarra, $inicioY + $maxAltura, $negro);

    $textoNota = strval($valores[$i]);
    imagestring($img, 4, $x + ($anchoBarra - strlen($textoNota) * imagefontwidth(4)) / 2, $y - 15, $textoNota, $negro);

    $textoMateria = $materias[$i];
    imagestring($img, 4, $x + ($anchoBarra - strlen($textoMateria) * imagefontwidth(4)) / 2, $inicioY + $maxAltura + 8, $textoMateria, $negro);
  }

  imagestring($img, 3, 10, $inicioY + $maxAltura + 30, "Leyenda:", $negro);
  imagefilledrectangle($img, 10, $inicioY + $maxAltura + 50, 25, $inicioY + $maxAltura + 60, $gris);
  imagestring($img, 3, 30, $inicioY + $maxAltura + 50, "[0-39] Abandono", $negro);
  imagefilledrectangle($img, 160, $inicioY + $maxAltura + 50, 175, $inicioY + $maxAltura + 60, $rojo);
  imagestring($img, 3, 180, $inicioY + $maxAltura + 50, "[40-60] Reprobado", $negro);
  imagefilledrectangle($img, 320, $inicioY + $maxAltura + 50, 335, $inicioY + $maxAltura + 60, $verde);
  imagestring($img, 3, 340, $inicioY + $maxAltura + 50, "[61-100] Aprobado", $negro);

  imagepng($img);
  imagedestroy($img);
?>
