<?php

// ---
// --- Listening to blockchain.info to get the last block
// --- Drawing the block as a Treemap
// --- Tweet it
// ---

// ---
// --- 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 'inc/twitter.php';
require '../generique/inc/twitter.php';
require '../config.php';

$methode='spline';
if (isset($argv[3])) $methode=$argv[3];

$block_hash = $argv[1];
$the_block = blockchain::getBlockWithHash($block_hash);
if ($the_block === FALSE) die();

// ---
// --- Remercier les followers
// ---
// twitter::thanksFollowers();

$image_file=DATA_PATH."/$methode/".$argv[1].'.png';

if (file_exists($image_file))
{
  $image_origine = imagecreatefrompng($image_file);

  if (image_origine) {
  	$image_twitter = imagecreatetruecolor(TWITTER_WIDTH, TWITTER_HEIGHT);
	if ($image_twitter) {

  	    $fond = imagecolorallocate($image_twitter, 0, 0, 0);
  	    imagefilledrectangle($image_twitter, 0, 0, TWITTER_WIDTH, TWITTER_HEIGHT, $fond);

  	    $ratio = TWITTER_HEIGHT / imagesy($image_origine); 
  	    $new_width = $ratio * imagesx($image_origine);

  	    $xpos = floor((TWITTER_WIDTH - $new_width) / 2);
  	    $ypos = 0;

  	    imagecopyresized($image_twitter, $image_origine, $xpos, $ypos, 0, 0, $new_width, TWITTER_HEIGHT, imagesx($image_origine), imagesy($image_origine));

  	    $image_file = DATA_PATH."/twitterbot/".$argv[1].'.png';

  	    if (!imagepng($image_twitter,$image_file)) $image_file='';

       } else echo("Pbme avec image TWITTER ".PHP_EOL);
   } else echo("Pbme avec $image_file ".PHP_EOL);

  // ---
  // --- Un petit peu de stéganographie ...
  // ---
/* Ca fait des fichiers trop gros !
  $processor = new KzykHys\Steganography\Processor();
  $image = $processor->encode($image_file, 'TOPISTO is making art with block '.$the_block->height);
  $image->write($image_file);
*/

  // ---
  // --- Tweet
  // ---
  $tweet  = "#computerart #creativecoding #generativeart :\\n#bitcoin #blockchain \\n";
  $tweet .= $argv[2]." Block, Height : ".$the_block->height;
  $tweet .= "\\n[ See more on www.topisto.net ]";
  $success = twitter::tweet($tweet, $image_file);
}

?>
