<?php

/*
 * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Declare functions
// Create option for a select based on a name, a value and selected value
// with optional additional text
function CreateOption($Selected, $Value, $Name, $AddText) {
    $HTML = '';

    if ($Name == $Selected) {
        if ($Value == '') {
            $HTML .= '<option class="font-weight-bold text-center" '
                    . "value=\"$Value\" selected=\"\">$AddText" .
                    "$Name</option>" . PHP_EOL;
        } else {
            $HTML .= '<option class="text-center" '
                    . "value=\"$Value\" selected=\"\">$AddText"
                    . "$Name</option>" . PHP_EOL;
        }
    } else {
        if ($Value == '') {
            $HTML .= '<option class="font-weight-bold text-center" '
                    . "value=\"$Value\">$AddText" . "$Name</option>" . PHP_EOL;
        } else {
            $HTML .= '<option class="text-center" '
                    . "value=\"$Value\">$AddText" . "$Name</option>" . PHP_EOL;
        }
    }

    return $HTML;
}

// Create a select based on a dataset, a name and a selected value
function CreateSelect($DataSet, $Name, $Selected) {
    $HTML = '<select class="custom-select custom-select-lg text-center" '
            . "name=\"$Name\" id=\"$Name\">" . PHP_EOL;

    $AddText = 'Select a ';
    $HTML .= CreateOption($Selected, '', $Name, $AddText);

    foreach ($DataSet as $Key => $Value) {
        if (is_string($Key)) {
            $HTML .= CreateOption($Selected, $Key, $Key, '');
        } else {
            $HTML .= CreateOption($Selected, $Value, $Value, '');
        }
    }
    $HTML .= '</select>' . PHP_EOL;

    return $HTML;
}

// Return a reply based on a sign, a symbol, a message and the horoscope
function ResultOut($Sign, $Symbol, $Message, &$Horoscope) {
    $RangeMonths = array_keys($Horoscope[$Sign]['range']); 
    $StartDate = $RangeMonths[0] . ' ' 
            . $Horoscope[$Sign]['range'][$RangeMonths[0]];
    $EndDate = $RangeMonths[1] . ' ' 
            . $Horoscope[$Sign]['range'][$RangeMonths[1]];
    
    $HTML = '<div class="row my-1 border border-secondary rounded p-2">'
            . PHP_EOL;
    $HTML .= '<div class="container">' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<h2 class="m-auto text-center">' . $Sign . "</h2>" . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<span class="d-block m-auto text-center t-icon">'
            . $Symbol . "</span>" . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row mb-2">' . PHP_EOL;
    $HTML .= '<span class="d-block m-auto text-center font-weight-bold">'
            . $StartDate . ' - ' . $EndDate . "</span>" . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '<div class="row">' . PHP_EOL;
    $HTML .= '<p class="m-auto text-center">' . $Message . '</p>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;
    $HTML .= '</div>' . PHP_EOL;

    return $HTML;
}

// Return a reply with all the signs
function ConsultAll(&$Horoscope) {
    $HTML = '';

    foreach ($Horoscope as $Sign => $Represent) {
        $Symbol = '';
        $Message = '';

        foreach ($Represent as $Name => $Content) {
            if ($Name == array_keys($Represent)[0]) {
                $Symbol = $Content;
            } else if($Name == array_keys($Represent)[1]) {
                $Message = $Content;
            }
        }

        $HTML .= ResultOut($Sign, $Symbol, $Message, $Horoscope);
    }

    return $HTML;
}

// Consult horoscope based on month and day
function Consult(&$Months, $Month, $Day, &$Horoscope) {
    $HoroscopeNames = array_keys($Horoscope);
    $Sign = '';

    $Pos = 0;
    $Exit = false;

    while ($Pos < count($HoroscopeNames) && $Exit == false) {
        $HoroscopeName = $HoroscopeNames[$Pos];
        $MonthRange = $Horoscope[$HoroscopeNames[$Pos]]['range'];

        if (isset($MonthRange[$Month])) {

            if ((array_keys($MonthRange)[0] == $Month && $Day >= $MonthRange[$Month] 
                    || (array_keys($MonthRange)[1] == $Month && $Day <= $MonthRange[$Month]))) {
                $Sign = $HoroscopeName;
                $Exit = true;
            }
        }

        $Pos++;
    }

    $Symbol = $Horoscope[$Sign]['symbol'];
    $Message = $Horoscope[$Sign]['message'];

    return ResultOut($Sign, $Symbol, $Message, $Horoscope);
}
