<?php

    require_once(__DIR__ . "/config.php");
    require_once($path["helper"] . "db.php");
    require_once($path["helper"] . "text.php");
    require_once($path["helper"] . "net.php");
    require_once($path["helper"] . "html.php");
    require_once(__DIR__ . "/Constant.php");
    require_once(__DIR__ . "/Filter.php");

    $UID = 0;

    /**
     * Application controller.
     *
     * Handles every request, creating the required models and selecting the
     * view.
     */
    class Controller {

        /**
         * Constructor.
         *
         * Handles every request, creating the required models and selecting the
         * view.
         *
         * @param Array $params POST parameters of the request.
         */
        public function __construct($uid, $params){

            global $path;
            global $UID;
            $page;

            session_start();
            $db = start_db();

            // Parse parameters, get lang and build the route.
            $pars = [];
            foreach($params as $p){
                if (strlen($p) > 0){
                    array_push($pars, $p);
                }
            }

            // Special case: Actions.
            // Not redirecting to page, just execute a function
            if (count($pars) > 1 && strtoupper($pars[0]) == "ACTION"){
                switch (strtoupper($pars[1])){
                    case "LOGIN":
                        require_once($path["action"] . "login.php");
                        action($db);
                        return;
                        break;
                    case "NEW_TEAM":
                        require_once($path["action"] . "new_team.php");
                        action($db);
                        return;
                        break;
                    case "SAVE_RUN_TEAM":
                        require_once($path["action"] . "save_run_team.php");
                        action($db);
                        return;
                        break;
                    default:
                        require_once($path["page"] . "Error_Page.php");
                        $page = new Error_Page($db, 404);
                        http_response_code(404);
                }
            }

            // TODO: Validate private/public user with cookie and redirect
            $UID = $uid;
            if (preg_match("/\d{7}/", $UID)){
                $q = $db->query("SELECT count(uid) AS c FROM player WHERE uid = $UID;");
                $r = $q->fetchArray(SQLITE3_ASSOC);
                if ($r["c"] == 0){
                    require_once($path["page"] . "Landing_Page.php");
                    $page = new Landing_Page($db);
                    require_once($page->view);
                    return;
                }
            }
            else{
                if ($_SESSION['session'] == true && isset($_SESSION['uid'])){
                    $UID = $_SESSION['uid'];
                    header("Location: /$UID" . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));
                    return;
                }
                else{
                    require_once($path["page"] . "Landing_Page.php");
                    $page = new Landing_Page($db);
                    require_once($page->view);
                    return;
                }
            }

            // Select the model to load.
            if (sizeof($pars) == 0){
                require_once($path["page"] . "Home_Page.php");
                $page = new Home_Page($db);
            }
            else{
                if (strtoupper($pars[0]) == "FUSION"){
                    require_once($path["page"] . "Fusion_Page.php");
                    $page = new Fusion_Page($db);
                }
                elseif (strtoupper($pars[0]) == "RUNES"){
                    require_once($path["page"] . "Runes_Page.php");
                    $page = new Runes_Page($db);
                }
                elseif (strtoupper($pars[0]) == "GUILD"){
                    require_once($path["page"] . "Guild_Page.php");
                    $page = new Guild_Page($db);
                }
                elseif (strtoupper($pars[0]) == "RUNS"){
                    require_once($path["page"] . "Runs_Page.php");
                    $page = new Runs_Page($db);
                }
                elseif (strtoupper($pars[0]) == "TEAMS"){
                    require_once($path["page"] . "Teams_Page.php");
                    $page = new Teams_Page($db);
                }
                elseif (strtoupper($pars[0]) == "MONSTERS"){
                    if (count($pars) == 1){
                        require_once($path["page"] . "Monsters_Page.php");
                        $page = new Monsters_Page($db);
                    }
                    else{
                        require_once($path["page"] . "Monster_Page.php");
                        $page = new Monster_Page($db, $pars[1]);
                    }
                }
                elseif (strtoupper($pars[0]) == "CATALOG"){
                    if (count($pars) == 1){
                        require_once($path["page"] . "Catalog_Page.php");
                        $page = new Catalog_Page($db);
                    }
                    else{
                        require_once($path["page"] . "Monster_Info_Page.php");
                        $page = new Monster_Info_Page($db, $pars[1]);
                    }
                }
                elseif (strtoupper($pars[0]) == "REPORT"){
                    if (count($pars) == 1){
                        require_once($path["page"] . "Report_Page.php");
                        $page = new Report_Page($db);
                    }
                    elseif (strtoupper($pars[1]) == "SKILLUP"){
                        require_once($path["page"] . "Report_Skillup_Page.php");
                        $page = new Report_Skillup_Page($db, $pars[1]);
                    }
                    elseif (strtoupper($pars[1]) == "RUNEUPGRADE"){
                    require_once($path["page"] . "Report_Runeupgrade_Page.php");
                        $page = new Report_Runeupgrade_Page($db, $pars[1]);
                    }
                    elseif (strtoupper($pars[1]) == "RUNECRAFT"){
                    require_once($path["page"] . "Report_Runecraft_Page.php");
                        $page = new Report_Runecraft_Page($db, $pars[1]);
                    }

                }
            }
            // Load the view, or set an error code.
            if (isset($page)){
                require_once($page->view);
            }
            else{
                require_once($path["page"] . "Error_Page.php");
                $page = new Error_Page($db, 404);
                http_response_code(404);
            }
        }
    }
?>
