<?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 in screen
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

// Repair output with Tidy
ini_set('tidy.clean_output', '1');
// Empty web browser cache
header('Cache-Control: no-cache');
header('Pragma: no-cache');

// Load classes
spl_autoload_register(function ($ClassName) {
    require './classes/' . $ClassName . '.php';
});

// Load variables
require './variables.php';

// Main functionality
if ((isset($_GET) && count($_GET) === 0) || !isset($_GET) || isset($_GET['XDEBUG_SESSION_START'])) {
    $Type = 0;
} else if (isset($_GET) && count($_GET) > 1) {
    header('Location:.');
} else {
    if (isset($_GET['search'])) {
        $Search = $_GET['search'];
        $Data = $Search;
        $Type = 1;
    } else if (isset($_GET['movie'])) {
        $Data = $_GET['movie'];
        $Type = 2;
    } else if (isset($_GET['person'])) {
        $Data = $_GET['person'];
        $Type = 3;
    } else {
        $Type = -1;
    }
}

$PagePrinter = PagePrinter::GetInstance($Data, $Type, $BaseURL, $APIKey,
                $ImageURL, $DefaultPoster, $DefaultProfile);

$Page = $PagePrinter->Render();

// Load view
require './view.php';
