<?php

use GDTextPersian\Box;
use GDTextPersian\Color;
use Component\Db;

require_once __DIR__.'/../../../vendor/autoload.php';

try {

	// Getting url arguments
	$source = $_GET['source'] ?? null;
	$table_code = $_GET['tc'] ?? null;
	$id = $_GET['id'] ?? null;
	$title = $_GET['t'] ?? null;

	// Check arguments existence
	if ( !$source || !$table_code || !$id ||  !$title ){
		die('تعداد آرگومان های ناکافی');
	}

	// database connection
	require_once('../../config.php');
	require_once(INACCESSIBLE . "convert_code_table_eachother.php");
    require_once(INACCESSIBLE . 'function.php');
    $dbh_conn = Db::getInstance();


	if ( config('app_mode') == 'product' ){
		error_reporting(0);
	}

    // Get table name according to the passed table code
	$table_name = $code_tablename[$table_code];

	// Set database columns name
	if ($parent_of_table_code[$table_code] == 'dictionaries'){
		$cols = 'word as title, mean as text';
		$col_on_where_clause = 'word';
	} else {
		die('ستون های جدول شناسایی نشد.');
	}

	// Select the specified post
	$sth = $dbh_conn->prepare("SELECT $cols FROM $table_name WHERE id = :id AND $col_on_where_clause = :t");
	$sth->bindValue(":t", $title, PDO::PARAM_STR);
	$sth->bindValue(":id", $id, PDO::PARAM_INT);
	$sth->execute();

    // Post not found
    if (!$sth->rowCount()) {
        die('پست مورد نظر یافت نشد.');
    }

    $post = $sth->fetch(PDO::FETCH_ASSOC);
	$title = $post['title']; // overwriting just for sure
    $text = $post['text'];

} catch(PDOException $e) {
	die('خطایی رخ داده است.');
}

// Anonymous functions ----------------------------------
$cleanText = function($text){
	$text =  str_replace('  ', ' ', $text);
	$text =  preg_replace('~<br />~',"
", $text);
	$text =  preg_replace('/&.*?;/', '', $text);
	$text =  strip_tags($text);

	return trim($text);
};

// Constants --------------------------------------------
define('TITLE_FONT_SIZE', 36); // in px

// ------------------------------------------------------

$title = cut_string($title, 25, 20);
$text  = cut_string($cleanText($text), 500, 480);

$im                 = imagecreatetruecolor(400, 400);
$dimgrey            = imagecolorallocate($im,60,60,60);
$indigo             = imagecolorallocate($im,249,249,249);

@imagefilledrectangle($im, 0, 370, 400, 400, $dimgrey);
@imagefill($im, 0, 0, $indigo);

$box = new Box($im,true);

// Title
$box->setFontFace(__DIR__.'/../../fonts/iransans/IRANSansWeb_Bold.ttf');
$box->setFontSize(TITLE_FONT_SIZE);
$box->setFontColor(new Color(10, 10, 10));
$box->setBox(15, 18, 360, 360);
$box->setTextAlign('center', 'top');
$box->draw($title);

// Sub title
$box->setFontFace(__DIR__.'/../../fonts/iransans/IRANSansWeb.ttf');
$box->setFontSize(16);
$box->setFontColor(new Color(150, 150, 150));
$box->setBox(15, 77, 360, 360);
$box->setTextAlign('center', 'top');
$box->draw($source);

// Body
$box->setFontFace(__DIR__.'/../../fonts/iransans/IRANSansWeb.ttf');
$box->setFontSize(18);
$box->setFontColor(new Color(50, 50, 50));
$box->setBox(15, 117, 360, 360);
$box->setTextAlign('right', 'top');
$box->setLineHeight(1.6);
$box->draw($text);

// Footer
$box = new Box($im);
$box->setFontFace(__DIR__.'/../../fonts/iransans/IRANSansWeb.ttf');
$box->setFontSize(19);
$box->setFontColor(new Color(250, 250, 250));
$box->setBox(0, 0, 395, 395);
$box->setTextAlign('center', 'bottom');
$box->draw('l a m t a k a m . c o m', false);

ob_end_clean();
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);

// ob_start ();
// imagepng($im, null, 9, PNG_ALL_FILTERS);
	// $image_data = ob_get_contents ();
// ob_end_clean ();
// echo "<img src='data:image/png;base64," . base64_encode ($image_data) . "' />";
