<?php

require_once "header.php";

// Imagick::IMGTYPE_BILEVEL

$weather = new Weather();
$tasks = Task::getList();

$deg = html_entity_decode("&deg;");
$image = imagecreate(480, 800);
$white = imagecolorallocate($image, 0, 0, 0);
$black = imagecolorallocate($image, 255, 255, 255);
//$red = imagecolorallocate($image, 255, 0, 0);
$red = $black;
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
$font = __DIR__ . "/fonts/ARIALBOLD.TTF";
$icon = imagecreatefrompng(__DIR__ . "/icons/" . substr($weather->conditions["icon"], 0, 2) . "_solid.png");
imagefilter($icon, IMG_FILTER_NEGATE);
$icon_x = imagesx($icon);
$icon_y = imagesy($icon);
imageantialias($image, true);

$background = $white;
$line_color = $black;
$highlight_color = $red;

imagefill($image, 0, 0, $white);

/*
imagesetthickness($image, 3);
imageline($image, 0, 1, 479, 1, $line_color);
imageline($image, 478, 0, 478, 799, $line_color);
imageline($image, 1, 0, 1, 799, $line_color);
imageline($image, 0, 798, 479, 798, $line_color);
 */

imagesetthickness($image, 1);
$taskStartY = 3;
$taskCurrentY = $taskStartY;
$taskHeightY = 80;
for ( $i=0; $i<8; $i++ ) {
    if ( !isset($tasks[$i]) ) break;
    if ( $tasks[$i]->isOverdue() ) {
        $color = $red;
    } else {
        $color = $black;
    }
    imagerectangle($image, 3, $taskCurrentY, 476, ($taskCurrentY + $taskHeightY), $color);
    imagettftext($image, 20, 0, 7, ($taskCurrentY + 18 + 6), $color, $font, $tasks[$i]->getTitle());
    $daysLeftX = 420;
    if ( $tasks[$i]->getDaysLeft() > 9 ) $daysLeftX -= 54;
    $lines = array("", "");
    $words = explode(" ", $tasks[$i]->getDescription());
    $currentWord = 0;
    $currentLine = 0;
    while ( ($currentWord < count($words)) && ($currentLine < 2) ) {
        $testLine = $lines[$currentLine] . $words[$currentWord] . " ";
        $box = imagettfbbox(14, 0, $font, $testLine);
        if ( $box[2] >= $daysLeftX ) {
            $currentLine++;
        } else {
            $lines[$currentLine] = $testLine;
            $currentWord++;
        }
    }
    imagettftext($image, 14, 0, 7, ($taskCurrentY + 18 + 6 + 23), $color, $font, $lines[0]);
    imagettftext($image, 14, 0, 7, ($taskCurrentY + 18 + 6 + 23 + 21), $color, $font, $lines[1]);
    imagettftext($image, 70, 0, $daysLeftX, ($taskCurrentY + $taskHeightY - 5), $color, $font, $tasks[$i]->getDaysLeft());
    $taskCurrentY += $taskHeightY + $taskStartY + 1;
}
//imagestring($image, 1, 20, 20, "This is some text", $line_color);
//imagestring($image, 2, 20, 50, "This is some text", $line_color);
//imagestring($image, 3, 20, 80, "This is some text", $line_color);
//imagestring($image, 4, 20, 110, "This is some text", $line_color);
//imagestring($image, 5, 20, 140, "This is some text", $line_color);
//imagettftext($image, 20, -45, 20, 270, $line_color, $font, "This is some text");
imagesetthickness($image, 3);
imagerectangle($image, 250, 675, 472, 792, $line_color);
imagecopymerge($image, $icon, 400, 680, 0, 0, $icon_x, $icon_y, 100);
imagettftext($image, 55, 0, 260, 735, $line_color, $font, intval($weather->temps["current"]) . html_entity_decode("&deg;"));
imagettftext($image, 18, 0, 345, 735, $line_color, $font, "/ " . intval($weather->stats["feels_like"]) . $deg);
imagettftext($image, 15, 0, 256, 762, $line_color, $font, "High: " . intval($weather->temps["max"]) . $deg);
imagettftext($image, 15, 0, 261, 782, $line_color, $font, "Low: " . intval($weather->temps["min"]) . $deg);
imagettftext($image, 15, 0, 350, 762, $line_color, $font, "Humid: " . $weather->stats["humidity"] . "%");
imagettftext($image, 15, 0, 364, 782, $line_color, $font, "Wind: " . intval($weather->stats["wind_speed"]));

//imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_CONTRAST, -1000);

//header("Content-type: image/bmp");
//imagebmp($image);
imagebmp($image, __DIR__ . '/render/image.bmp');
$b = new Imagick(__DIR__ . '/render/image.bmp');
$b->setImageType(Imagick::IMGTYPE_BILEVEL);
$b->setFormat('bmp');
$b->writeImage(__DIR__ . '/render/image_1.bmp');

exit();
