<?php

require_once dirname(__DIR__, 2).'/vendor/autoload.php';

use Rv\Data\Color;
use Rv\Data\MacrosDocument;
use Rv\Data\MacrosDocument\Macro;
use Rv\Data\MacrosDocument\MacroCollection;
use Rv\Data\MacrosDocument\MacroCollection\Item;
use Rv\Data\UUID;

function makeUuid(string $value): UUID
{
    $uuid = new UUID;
    $uuid->setString(strtoupper($value));

    return $uuid;
}

function makeColor(float $r, float $g, float $b, float $a = 1.0): Color
{
    $color = new Color;
    $color->setRed($r);
    $color->setGreen($g);
    $color->setBlue($b);
    $color->setAlpha($a);

    return $color;
}

$doc = new MacrosDocument;

$macros = [
    ['uuid' => 'AAAAAAAA-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Copyright Makro', 'r' => 1.0, 'g' => 0.5, 'b' => 0.0],
    ['uuid' => 'BBBBBBBB-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Vers Makro', 'r' => 0.0, 'g' => 0.8, 'b' => 0.2],
    ['uuid' => 'CCCCCCCC-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Refrain Makro', 'r' => 0.2, 'g' => 0.4, 'b' => 1.0],
];

$macroObjects = [];
foreach ($macros as $data) {
    $macro = new Macro;
    $macro->setUuid(makeUuid($data['uuid']));
    $macro->setName($data['name']);
    $macro->setColor(makeColor($data['r'], $data['g'], $data['b']));
    $macro->setTriggerOnStartup(false);
    $macro->setImageType(0);
    $macroObjects[] = $macro;
}
$doc->setMacros($macroObjects);

$collection = new MacroCollection;
$collection->setUuid(makeUuid('8D02FC57-83F8-4042-9B90-81C229728426'));
$collection->setName('--MAIN--');

$items = [];
foreach ($macros as $data) {
    $item = new Item;
    $item->setMacroId(makeUuid($data['uuid']));
    $items[] = $item;
}
$collection->setItems($items);
$doc->setMacroCollections([$collection]);

$output = $doc->serializeToString();
file_put_contents(__DIR__.'/macros-sample.bin', $output);
echo 'Written '.strlen($output).' bytes'.PHP_EOL;
