<?php

// ---
// --- La config globale
// ---
chdir('/opt/TOPISTO/apps');
require_once '/opt/TOPISTO/apps/global/inc/config.php';

// ---
// --- External dependances
// ---
require TOPISTO_PATH.'/ressources/vendor/autoload.php';

// ---
// --- Internal dependances
// ---
require_once APP_PATH.'/blockchain/inc/block.php';

// ---
// --- Par défaut on cherche le dernier block
// ---
$block_hash = '*';
$methode='hasard';
$mode=9999;

// ---
// --- Le cas échéant, on cherche block passé en argument
// ---
if (isset($_REQUEST['hash']))    $block_hash = $_REQUEST['hash'];
if (isset($_REQUEST['methode'])) $methode    = $_REQUEST['methode'];
if (isset($_REQUEST['mode']))    $mode       = intval($_REQUEST['mode']);

//$methode = str_replace('full_', '', $methode);

$img = null;

$imagefilename = DATA_PATH.'/'.$methode.'/'.$block_hash.'-'.$mode.'.png';
if (file_exists($imagefilename))
{
  $img = imagecreatefrompng($imagefilename);
} 

//
// "strict" est un flag qui indique que l'on veut précisément
//             ce que les paramètres demandent
//
if (($img==null)&&(!isset($_REQUEST['strict'])))
{  
  //
  // On n'a pas passé de HASH
  // 
  if (($img==null)&&(!isset($_REQUEST['hash'])))
  {
    //
    // On prend le dernier connu pour la méthode et le mode
    // 
    $myarray = glob(DATA_PATH.'/'.$methode.'/*-'.$mode.'.png');
    if (isset($myarray[0]))
    {
      usort( $myarray, function( $a, $b ) { return filemtime($a) - filemtime($b); } );
      $img = imagecreatefrompng($myarray[0]);
    }
    //
    // On prend le dernier connu pour la méthode tout mode confondu
    // 
    if ($img==null)
    {
      $myarray = glob(DATA_PATH.'/'.$methode.'/*.png');
      if (isset($myarray[0]))
      {
        usort( $myarray, function( $a, $b ) { return filemtime($a) - filemtime($b); } );
        $img = imagecreatefrompng($myarray[0]);
      }
    }
  }

  //
  // On a passé un HASH
  //
  if (($img==null)&&(isset($_REQUEST['hash'])))
  {
    //
    // On cherche le bon HASH et la bonne méthode
    //
    $imagefilename = DATA_PATH.'/'.$methode.'/'.$block_hash.'.png';
    if (($img==null)&&(file_exists($imagefilename)))
    {
      $img = imagecreatefrompng($imagefilename);
    } 
    //
    // En fait on s'en fiche de la méthode tant qu'on a le bon HASH
    //
    $imagefilename = DATA_PATH.'/hasard/'.$block_hash.'.png';
    if (($img==null)&&(file_exists($imagefilename)))
    {
      $img = imagecreatefrompng($imagefilename);
    } 
  }  
}

//
// On n'a rien trouvé, donc on dessine ce qui est demandé
//
if ($img==null)
{
  if ($mode == 9999) $mode = 0;
  if ($block_hash == '*') $block_hash = blockchain::getLastCacheBlockHash();

  $imagefilename = DATA_PATH.'/'.$methode.'/'.$block_hash.'.png';

  switch($methode)
  {
    case 'full_treemap_fuzzy':
      require_once APP_PATH.'/methode/treemap_fuzzy/inc/treemap.php';
      $imagefilename = DATA_PATH.'/full_treemap_fuzzy/'.$block_hash.'.png';
      break;

    case 'full_spline':
      require_once APP_PATH.'/methode/spline/inc/splines.php';
      $imagefilename = DATA_PATH.'/full_spline/'.$block_hash.'.png';
      break;

    case 'full_treemap' :
      default:
      require_once APP_PATH.'/methode/treemap/inc/treemap.php';
      $imagefilename = DATA_PATH.'/full_treemap/'.$block_hash.'-'.$mode.'.png';
  }

  $the_block = blockchain::getBlockWithHash($block_hash);
  if ($the_block === FALSE) die();

  $the_name = blockchain::hash2SpecialName($the_block->hash);
  if ($the_name == $the_block->hash) $the_name ='';

  $bandeau     = 50;
  $marge       = 25;
  $text_border = 20;
  $width       = GRAPH_WIDTH;
  $height      = GRAPH_HEIGHT;

  $img_w = $marge + ($width*2)  + (2*$text_border);
  $img_h = $marge + ($height*2) + (2*$bandeau);

  // création d'une image plus haute pour inclure bandeaux haut et bas
  $img = imagecreatetruecolor( $img_w, $img_h);

  // Les parties du block : inputs, outputs, fees, reward
  switch($methode)
  {
    case 'full_treemap_fuzzy':
      topisto_treemap_fuzzy::DrawBlock($the_block, $img,     $text_border + 0, $bandeau, $width, $height, $mode, 2);
      topisto_treemap_fuzzy::DrawBlock($the_block, $img, $text_border+$marge+$width, $bandeau, $width, $height, $mode, 1);
      topisto_treemap_fuzzy::DrawBlock($the_block, $img,       $text_border+0, $marge+$height + $bandeau, $width, $height, $mode, 3);
      topisto_treemap_fuzzy::DrawBlock($the_block, $img, $text_border+$marge+$width, $marge+$height + $bandeau, $width, $height, $mode, 4);
      break;

    case 'full_spline':
      $type=2;
      $x0 = $text_border; $y0 = $bandeau;
      topisto_spline::DefaultDrawBlock($the_block, $img, $x0, $y0, $width, $height, $type);

      $type=1;
      $x0 = $text_border+$marge+$width; $y0 = $bandeau;
      topisto_spline::DefaultDrawBlock($the_block, $img, $x0, $y0, $width, $height, $type);

      $type=3;
      $x0 = $text_border; $y0 = $marge+$height+$bandeau;
      topisto_spline::DefaultDrawBlock($the_block, $img, $x0, $y0, $width, $height, $type);

      $type=4;
      $x0 = $text_border+$marge+$width; $y0 = $marge+$height+$bandeau;
      topisto_spline::DefaultDrawBlock($the_block, $img, $x0, $y0, $width, $height, $type);
      break;

    case 'full_treemap':
    default:
      topisto_treemap::DrawBlock($the_block, $img,     $text_border + 0, $bandeau, $width, $height, $mode, 2);
      topisto_treemap::DrawBlock($the_block, $img, $text_border+$marge+$width, $bandeau, $width, $height, $mode, 1);
      topisto_treemap::DrawBlock($the_block, $img,       $text_border+0, $marge+$height + $bandeau, $width, $height, $mode, 3);
      topisto_treemap::DrawBlock($the_block, $img, $text_border+$marge+$width, $marge+$height + $bandeau, $width, $height, $mode, 4);
      break;
  }

  blockchain::DrawBlockHeaderFooter($the_block, $img, $bandeau);

  $fond = imagecolorallocate($img, 40, 40, 40);
  imagefilledrectangle($img, 0, $bandeau, $text_border, $img_h-$bandeau, $fond);
  imagefilledrectangle($img, $img_w-$text_border, $bandeau, $img_w, $img_h-$bandeau, $fond);
  imagefilledrectangle($img, $text_border+$width, $bandeau, $text_border+$marge+$width, $img_h-$bandeau, $fond);
  imagefilledrectangle($img, 0, $bandeau+$height, $img_w - $text_border, $bandeau+$marge+$height, $fond);

  // Les textes
  putenv('GDFONTPATH='.RESS_PATH.'/fonts/');
  $font = 'DS-DIGIB.TTF';

  $fontColor = imagecolorallocate($img, 158,227,253);
  $fontColor = imagecolorallocate($img, 227,253,158);
  $fontColor = imagecolorallocate($img, 227,227,153);
  
  $the_texte = "Height : ".$the_block->height;
  imagettftext($img,18,  0, 5, $bandeau-5, $fontColor, $font, $the_texte);
  $the_texte = "Inputs : ".$the_block->topisto_inputs;
  imagettftext($img,15, 90, $text_border-3, $bandeau+$height, $fontColor, $font, $the_texte);
  $the_texte = "Outputs : ".$the_block->topisto_outputs;
  imagettftext($img,15, 90, $text_border+$width+$marge-3, $bandeau+$height, $fontColor, $font, $the_texte);
  $the_texte = "Fees : ".$the_block->topisto_fees;
  imagettftext($img,15, 90, $text_border-3, $bandeau+(2*$height)+$marge-3, $fontColor, $font, $the_texte);
  $the_texte = "Reward : ".$the_block->topisto_reward;
  imagettftext($img,15, 90, $text_border+$width+$marge-3, $bandeau+(2*$height)+$marge-3, $fontColor, $font, $the_texte);

  if ($the_name == '') $the_name = date('Ymd H:m:s', $the_block->time);
  $bbox = imagettfbbox(14, 0, $font, $the_name);
  imagettftext($img, 14, 0, ($img_w-3)-($bbox[2]-$bbox[0]), ($bandeau-5), $fontColor, $font, $the_name);

  // Sauvegarder l'image et ajouter 
  //   - un lien sur le mode
  //   - un lien dans le dossier "hasard"
  $imagefilename2 = str_replace(".png","-$mode.png", $imagefilename);
  imagepng($img,$imagefilename2);

  if (!file_exists($imagefilename)) link($imagefilename2, $imagefilename);

  $imagefilename2 = str_replace("methode/$methode/","methode/hasard/", $imagefilename);
  if (!file_exists($imagefilename2)) link($imagefilename, $imagefilename2);
}

$seconds_to_cache = 7200;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
header('Content-Type: image/png');

imagepng($img);
imagedestroy($img);

?>
