<?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/>.
 */

// Show errors on screen
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

// Turn on the output repairing by Tidy
ini_set('tidy.clean_output', '1');

// Load variables
include './variables.php';

// Load functions
include './functions.php';

// Main operation thread

// Check that the operation is done
if (isset($_REQUEST['operation'])) {
    $Operation = $_REQUEST['operation'];

    // Validate the type of the operation
    if ($Operation == 'consult' || $Operation == 'all') {
        if ($Operation == 'consult') {
            // Check that the operators are sent
            if (!isset($_REQUEST['month']) || !isset($_REQUEST['day'])) {
                $Result = '<span class="d-block text-center">Must send a month '
                        . 'and a day.</span>' . PHP_EOL;
            } else {
                // Set the operators' value
                $Month = $_REQUEST['month'];
                $Day = $_REQUEST['day'];

                // Validate the type of the operators' value
                if ($Month == '' || $Day == '') {
                    $Result = '<span class="d-block text-center">Must set a month '
                            . 'and a day.</span>' . PHP_EOL;
                } else if (is_string($Month) && is_numeric($Day)) {
                    // Validate day per month
                    if ($Day >= 1 && $Day <= $Months[$Month]) {
                        $Result = Consult($Months, $Month, $Day, $Horoscope);
                    } else {
                        $Result = '<span class="d-block text-center">Must '
                                . 'set corresponding values of month and day.'
                                . '</span>' . PHP_EOL;
                    }
                } else {
                    $Result = '<span class="d-block text-center">Must set correct '
                            . 'values for the month and the day.</span>'
                            . PHP_EOL;
                }
            }
        } else {
            $Result = ConsultAll($Horoscope);
        }
    } else {
        $Result = '<span class="d-block text-center">The operation must be '
                . '«consult» o «all».</span>' . PHP_EOL;
    }
}

// Load view
include './view.php';
