<?php

namespace monoformat {

enum SectionType : int {
    case AlwaysDrawn = 1;
    case TimeBasedDrawn = 2;
    case CustomFont = 32;
}

enum ElementType : int {
    case Image = 1;
    case Animation = 2;
    case HScrollImage = 3;
    case VScrollImage = 4;
    case Line = 5;
    case ClippedText = 16;
    case HScrollText = 17;
    case CurrentTime = 32;
}

enum LineStyle : int {
    case Solid = 0;
}

class Element {
    public function serialize() {
        return "";
    }
}

class HScrollTextElement extends Element {
    public $x = 0;
    public $y = 0;
    public $width = 0;
    public $height = 0;
    public $flags = 0;
    public $scrollSpeed = 0;
    public $fontIndex = 0;
    public $text = "";

    public function serialize() {
        $len = strlen($this->text);
        $result = pack("vvvvvCCvv",
                       17,
                       $this->x,
                       $this->y,
                       $this->width,
                       $this->height,
                       $this->flags,
                       $this->scrollSpeed,
                       $this->fontIndex,
                       $len);
        $result .= (string) $this->text;
        if ($len % 4 != 0) {
            $n = 4 - ($len % 4);
            for ($i = 0; $i < $n; ++$i) {
                $result .= chr(0);
            }
        }
        return $result;
    }
}

class CurrentTimeElement extends Element {
    public $x = 0;
    public $y = 0;
    public $width = 0;
    public $height = 0;
    public $fontIndex = 0;
    public $utcOffset = 0;
    public $flags = 0;

    public function serialize() {
        $result = pack("vvvvvvvv",
                       32,
                       $this->x,
                       $this->y,
                       $this->width,
                       $this->height,
                       $this->fontIndex,
                       $this->utcOffset,
                       $this->flags);
        return $result;
    }
}

class Section {
    public function serialize() {
        return "";
    }
}

class AlwaysDrawnSection extends Section {
    public $drawOnFront = false;
    public $drawOnBack = false;
    public $clearBeforeDrawing = false;
    public $elements = [];

    public function serialize() {
        $flags = 0;
        if ($this->drawOnFront) {
            $flags |= 0x01;
        }
        if ($this->drawOnBack) {
            $flags |= 0x02;
        }
        if ($this->clearBeforeDrawing) {
            $flags |= 0x04;
        }
        $inner = pack("vv", $flags, count($this->elements));
        foreach ($this->elements as $element) {
            $inner .= $element->serialize();
        }
        $len = strlen($inner) + 4;
        $len = substr(pack("V", $len), 0, 3);
        return pack("C", 1) . $len . $inner;
    }
}

class File {
    public $sections = [];

    public function serialize() {
        $nSections = count($this->sections);
        $result = "\xAF\x7E\x2B\x63";
        $result .= pack("Vvv", 1, $nSections, 0);
        foreach ($this->sections as $section) {
            $result .= $section->serialize();
        }
        return $result;
    }
}

} // namespace monoformat

namespace {

$roomName= "BOOL";

$data = json_decode(file_get_contents("https://cfp.cttue.de/tdf5/schedule/export/schedule.json"), true);
$talks = [];
$now = new DateTimeImmutable("now");
foreach ($data["schedule"]["conference"]["days"] as $day) {
    foreach ($day["rooms"][$roomName] as $t) {
        [$h, $m] = explode(":", $t["duration"]);
        $duration = $h * 3600 + $m * 60;
        $duration = DateInterval::createFromDateString("$duration sec");
        $talk = [
            "date" => new DateTimeImmutable($t["date"]),
            "duration" => $duration,
            "title" => $t["title"],
        ];
        $talkEnd = $talk["date"]->add($talk["duration"]);
        if ($talkEnd < $now) {
            continue;
        }
        if ($talk["date"] > $now && count($talks) > 2) {
            break;
        }
        array_push($talks, $talk);
    }
}

$elements = [];

function putText($x, $y, $width, $height, $text) {
    global $elements;

    $e = new monoformat\HScrollTextElement();
    $e->x = $x;
    $e->y = $y;
    $e->width = $width;
    $e->height = $height;
    $e->flags = 0;
    $e->scrollSpeed = 15;
    $e->fontIndex = 0;
    $e->text = $text;
    array_push($elements, $e);
}

$i = 0;
foreach ($talks as $talk) {
    putText(10, $i * 20, 25, 20, $talk["date"]->format("H:i"));
    putText(35, $i * 20, 85, 20, $talk["title"]);
    ++$i;
}

$section = new monoformat\AlwaysDrawnSection();
$section->drawOnFront = true;
$section->drawOnBack = true;
$section->clearBeforeDrawing = true;
$section->elements = $elements;

$file = new monoformat\File();
array_push($file->sections, $section);

Header("Content-Type: application/octet-stream");

print($file->serialize());
} // global

?>
