<?php

require("./lib//config.php");
require("./lib//helpers.php");
require("./lib//validation.php");

session_start();

setup_locale();

validate_get_parameter();

$id = $_GET["id"];
$tempo = $_GET["tempo"];
$time = $_GET["time"];
$bars = intval($_GET["bars"]);
$shortest_note = intval($_GET["shortest_note"]);

$dynamic_beat = $_GET["dynamic_beat"];
$dynamic_rhythm = $_GET["dynamic_rhythm"];

$notes_final = [];
$notes_modifiers = [];
$bars_count = 0;
$rhythm_length = 0;

$time_explode = explode("/", $time, 2);
$rhythm_length_max = $shortest_note * (intval($time_explode[0]) / intval($time_explode[1]));

$id_hashed = md5($id);
do {
    $notes_input = [];
    for($i = 0; $i < strlen($id_hashed); $i+=2)
    {
        $notes_input[] = strval(pow(2, (ord(substr($id_hashed, $i)) % intval(log($shortest_note, 2))) + 1));
        $notes_modifiers[] = ord(substr($id_hashed, $i + 1));
    }
    for($i = 0; $i < count($notes_input); $i++)
    {
        if($bars_count == $bars)
        {
            continue;
        }
        $note_val = intval($notes_input[$i]);
        if(($note_val == 0) || ($note_val > $shortest_note) || (!is_valid_note($note_val)))
        {
            continue;
        }
        if($rhythm_length + ($shortest_note / $note_val) > $rhythm_length_max)
        {
            continue;
        }
        $rhythm_length += $shortest_note / $note_val;

        if($notes_modifiers[$i] & 0b0111)
        {
            $notes_final[] = PERCUSSION_CLAP . $note_val;
        }
        else
        {
            $notes_final[] = PERCUSSION_REST . $note_val;
        }

        if($rhythm_length == $rhythm_length_max)
        {
            $notes_final[] = "|";
            $rhythm_length = 0;
            $bars_count++;
        }
    }
    $id_hashed = md5($id_hashed);
} while($bars_count < $bars);

$notes_final = implode(" ", $notes_final);

$rest_padding = array_fill(0, $time_explode[0], PERCUSSION_REST . $time_explode[1]);
$rest_padding[0] = $rest_padding[0] . "\\$dynamic_rhythm";
$rest_padding = implode(" ", $rest_padding);

$beats_one_bar = array_fill(1, $time_explode[0] - 1, PERCUSSION_BEAT . $time_explode[1]);
array_unshift($beats_one_bar, PERCUSSION_BEAT_ACCENT . $time_explode[1] . "\\$dynamic_beat");
array_push($beats_one_bar, "|");
$beats = implode(" ", array_fill(0, $bars_count + 2, implode(" ", $beats_one_bar)));

$file_content = sprintf(
    LILYPOND_FORMAT,
    $time,
    $tempo,
    $notes_final,
    $rest_padding,
    $beats
);
$file_name = md5($file_content);

file_put_contents( OUTPUT_FOLDER . $file_name . ".ly", $file_content);
exec(sprintf("lilypond --png -o %1\$s %1\$s$file_name.ly && convert %1\$s$file_name.png -trim %1\$s$file_name.png", OUTPUT_FOLDER));

echo(render(
    "./templates/index.tpl.php",
    $locale,
    array(
        "id" => $id,
        "time" => $time,
        "tempo" => $tempo,
        "bars" => $bars,
        "shortest_note" => $shortest_note,
        "dynamic_beat" => $dynamic_beat,
        "dynamic_rhythm" => $dynamic_rhythm,
        "file_name" => $file_name,
    )
));

?>