<?php

// ---
// --- Listening to blockchain.info to get the last block
// ---

// ---
// --- La config globale
// ---
require_once '../global/inc/config.php';

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

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

// ---
// --- Par défaut on cherche le dernier block
// ---
$block_hash = 'LAST';

// ---
// --- Le cas échéant, on cherche block passé en argument
// ---
if (isset($argv[1])) $block_hash = $argv[1];

$mode=8;
if (isset($argv[2])) $mode=intval($argv[2]);

$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 ='';

$mode        = 0;
$iterations  = 1;

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

$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 4 parties du block : inputs, outputs, fees, reward
//
$mode=5;
$iterations=50;

$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);

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);
$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);

imagepng($img, BIG_PATH.'/full_spline/'.$the_block->hash.'.png');

imagedestroy($img);

?>
