<?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.'/methode/treemap/inc/treemap.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=rand(0,5);
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 ='';

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

$param_header = blockchain::DrawBlockHeaderFooter($the_block, $img, $bandeau);
$tr_color = $param_header[0];
$fond = $param_header[1];
$font = $param_header[3];
$fontColor = $param_header[2];

// Les parties du block : outputs, fees, reward
topisto_treemap::DrawBlock($the_block, $img, $text_border+0, $bandeau,$marge+($width*2), $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);

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 + $height, $text_border+$marge+$width, $img_h-$bandeau, $fond);
imagefilledrectangle($img, 0, $bandeau+$height, $img_w - $text_border, $bandeau+$marge+$height, $fond);

// Les textes
$the_texte = "Outputs : ".$the_block->topisto_inputs;
imagettftext($img,15, 90, $text_border-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);

imagepng($img, DATA_PATH.'/full_treemap/'.$the_block->hash.'.png');

imagedestroy($img);

?>
