<?php
const GET_PARAM_MIN_STARS = 'search_min_stars';
const GET_PARAM_SEARCH_TEXT = 'search_text';
const GET_PARAM_SHOW_DESCRIPTION = 'show_description';
const GET_PARAM_SPRACHE = 'sprache';
const GET_PARAM_TOP_FLOPP = 'top_flopp';
/**
 * List of all allergens.
 */
$allergens = [
    11 => 'Gluten',
    12 => 'Krebstiere',
    13 => 'Eier',
    14 => 'Fisch',
    17 => 'Milch'
];

$meal = [
    'name' => 'Süßkartoffeltaschen mit Frischkäse und Kräutern gefüllt',
    'description' => 'Die Süßkartoffeln werden vorsichtig aufgeschnitten und der Frischkäse eingefüllt.',
    'price_intern' => 2.90,
    'price_extern' => 3.90,
    'allergens' => [11, 13],
    'amount' => 42             // Number of available meals
];

$ratings = [
    [   'text' => 'Die Kartoffel ist einfach klasse. Nur die Fischstäbchen schmecken nach Käse. ',
        'author' => 'Ute U.',
        'stars' => 2 ],
    [   'text' => 'Sehr gut. Immer wieder gerne',
        'author' => 'Gustav G.',
        'stars' => 4 ],
    [   'text' => 'Der Klassiker für den Wochenstart. Frisch wie immer',
        'author' => 'Renate R.',
        'stars' => 4 ],
    [   'text' => 'Kartoffel ist gut. Das Grüne ist mir suspekt.',
        'author' => 'Marta M.',
        'stars' => 3 ]
];

$dic = [
        [   "Gericht" => "Gericht",
            "Allergien" => "Allergien",
            "Bewertungen" => "Bewertungen (Insgesamt: ",
            "Text" => "Text",
            "Sterne" => "Sterne",
            "Author" => "Author",
            "Preis" => "Preis",
            "Gaeste" => "Gäste"
        ],
        [   "Gericht" => "Meal",
            "Allergien" => "Allergies",
            "Bewertungen" => "Review (overall: ",
            "Text" => "Text",
            "Sterne" => "Stars",
            "Author" => "Author",
            "Preis" => "Price",
            "Gaeste" => "Guests"
            ]
];

$translation = $dic[0];
$language = "de";
if (!empty($_GET[GET_PARAM_SPRACHE])){
        $language = $_GET[GET_PARAM_SPRACHE];
}
if($language == "en"){$translation = $dic[1];}

if (empty($_GET[GET_PARAM_SHOW_DESCRIPTION])){
        $meal['description'] = "";
}

$searchTerm = "";
$showRatings = [];
if (!empty($_GET[GET_PARAM_SEARCH_TEXT])) {
    $searchTerm = $_GET[GET_PARAM_SEARCH_TEXT];
    foreach ($ratings as $rating) {
        if (strpos(strtolower($rating['text']), strtolower($searchTerm) )!== false) {
            $showRatings[] = $rating;
        }
    }
} else if (!empty($_GET[GET_PARAM_MIN_STARS])) {
    $minStars = $_GET[GET_PARAM_MIN_STARS];
    foreach ($ratings as $rating) {
        if ($rating['stars'] >= $minStars) {
            $showRatings[] = $rating;
        }
    }
} else {
    $showRatings = $ratings;
}

if (!empty($_GET[GET_PARAM_TOP_FLOPP])){
    $flopp = $_GET[GET_PARAM_TOP_FLOPP];
    if($flopp == "flopp"){
        unset($showRatings);
        $showRatings=[];
        $stars=[];
        foreach ($ratings as $rating) {
            $stars[] = $rating["stars"];
        }
        $min = min($stars);
        foreach ($ratings as $rating) {
            if ($rating['stars'] == $min) {
                $showRatings[] = $rating;
            }
        }
    }
    elseif ($flopp == "top"){
        unset($showRatings);
        $showRatings=[];
        $stars=[];
        foreach ($ratings as $rating) {
            $stars[] = $rating["stars"];
        }
        $min = max($stars);
        foreach ($ratings as $rating) {
            if ($rating['stars'] == $min) {
                $showRatings[] = $rating;
            }
        }
    }
}

/**
 *:float gibt den return value der Funktion an
 */
function calcMeanStars (array $ratings) : float {
    $sum = 0;
    foreach ($ratings as $rating) {
        $sum += $rating['stars'] / count($ratings);
    }
    return $sum;
}

/**
 *
 */

?>
<!DOCTYPE html>
<html lang="de">
    <head>
        <meta charset="UTF-8"/>
        <title><?php echo $translation["Gericht"].": ". $meal['name']; ?></title>
        <style>
            * {
                font-family: Arial, serif;
            }
            .rating {
                color: darkgray;
            }
        </style>
    </head>
    <body>
        <h1><?php echo $translation["Gericht"].": ". $meal['name']; ?></h1>
        <p><?php echo $meal['description']; ?>
        </p>

        <h4><?php echo $translation["Preis"].":"?></h4>
        <p>
            <?php echo $translation["Preis"]." intern: ".$meal["price_intern"]."€";?> <br>
            <?php echo $translation["Preis"]." ".$translation["Gaeste"].": ".$meal["price_extern"]."€";?>
        </p>

        <h4><?php echo $translation["Allergien"].":"?> </h4>
        <ul>
            <?php
            foreach ($meal['allergens'] as $all) {
                echo "<li>$allergens[$all]</li>";
            }
            ?>
        </ul>
        <h1><?php echo $translation["Bewertungen"].calcMeanStars($ratings); ?>)</h1>
        <!--Such form in den Bewertungen für den User -->
        <form method="get">
            <label for="search_text">Filter:</label>
            <input id="search_text" type="text" name="search_text" value=<?php echo $searchTerm?>>
            <input type="submit" value="Suchen">
        </form>
        <table class="rating">
            <thead>
            <tr>
                <td><?php echo $translation["Text"]?></td>
                <td><?php echo $translation["Sterne"]?></td>
                <td><?php echo $translation["Author"]?></td>
            </tr>
            </thead>
            <tbody>
            <?php
        foreach ($showRatings as $rating) {
            echo "<tr><td class='rating_text'>{$rating['text']}</td>
                      <td class='rating_stars'>{$rating['stars']}</td>
                      <td class='rating_author'>{$rating['author']}</td>
                  </tr>";
        }
        ?>
            </tbody>
        </table>

        <br>
        <br>
        <a href="?sprache=de">Deutsch</a> oder <a href="?sprache=en">English</a>
    </body>
</html>
