<?php
    error_reporting(-1);
    ini_set('display_errors', 1);
    ini_set('output_buffering', 0);
?>

<!doctype html>
<html>
<head>
    <meta charset='utf-8'>
    <title>Tärning</title>
    <link href="../style.css" type="text/css" rel="stylesheet">
</head>

<body>
    <h1>En tärning</h1>
    <?php
        if(isset($_GET["times"])){
            $times = $_GET["times"];
        }else{
            $times = 6;
        }
    ?>
    <form action="" method="get">
        <label for="times">Hur många gånger ska tärningen kastas?</label><br>
        <input type="number" name="times" id="times" value=<?="$times"?>><br>
        <button type="submit">Kasta!</button>
    </form>
    <p>Tärning med 6 sidor kastas <?=$times?> gånger, här är resultatet.</p>

    <?php
        include_once("dice.php");
        include_once("histogram.php");

        $dice = new DiceImage();
        $histogram = new Histogram();
        $dice->roll_dice($times);

        echo "<div id='dice_rolls'>";
        $dice->print_rolls();
        echo "</div>";

        echo "<p><strong>Summa:</strong> " . $dice->get_sum() . "</p>";
        echo "<p><strong>Medelvärde:</strong> " . $dice->get_median() . "</p>";

        echo $histogram->print_histogram($dice->getRolls(), $dice->getFaces());
    ?>
</body>
</html>