 <?php

    require_once($path["page"] . "Page.php");
    require_once($path["entity"] . "Run.php");

    /**
     * Run list page.
     */
    class Runs_Page extends Page{

        /**
         * Array of @{see Run}s.
         */
        public $runs = [];

        /**
         * Filters the page can handle.
         */
        public $filters;

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param SQLite3 $db Connection to the database.
         */
        public function __construct($db){
            global $path;
            global $root;
            parent::__construct($db);
            $this->view = $path["view"] . "runs.php";
            $this->parse_filters();
            $s = $this->build_query();
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->runs, new Run($db, $r["id"]));
            }
            $this->title = "Runs - SWDB";
            $this->description = "Run logs";
            $this->canonical = $root . "runs/";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            global $FILTER_RUN;
            global $DIFFICULTY;
            $this->filters = $FILTER_RUN;
            if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
                $this->filters["area_type"] = $_GET["area_type"];
            }
            if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
                $this->filters["area"] = $_GET["area"];
            }
            if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
                $this->filters["stage"] = $_GET["stage"];
            }
            if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= $DIFFICULTY["NORMAL"] && intval($_GET["difficulty"]) <= $DIFFICULTY["HELL"]){
                $this->filters["difficulty"] = $_GET["difficulty"];
            }
            if (isset($_GET["date_min"]) && strlen($_GET["date_min"]) > 0){
                $this->filters["date_min"] = DateTime::createFromFormat('Y-m-d', $_GET["date_min"])->format('Y-m-d');
            }
            if (isset($_GET["date_max"]) && strlen($_GET["date_max"]) > 0){
                $this->filters["date_max"] = DateTime::createFromFormat('Y-m-d', $_GET["date_max"])->format('Y-m-d');
            }
            if (isset($_GET["defeat"]) && $_GET["defeat"] == "on"){
                $this->filters["defeat"] = true;
            }
            else{
                $this->filters["helper"] = false;
            }
            if (isset($_GET["helper"]) && $_GET["helper"] == "on"){
                $this->filters["helper"] = true;
            }
            else{
                $this->filters["helper"] = false;
            }
            if (isset($_GET["number"]) && intval($_GET["number"]) > 0){
                $this->filters["number"] = $_GET["number"];
            }
            return;
        }

        /**
         * Builds the query to the run table using the selected or default
         * filters.
         * 
         * @return The query to be executed.
         */
        private function build_query(){
            global $UID;
            $s = "
                SELECT id
                FROM run
                WHERE uid = '$UID'
             ";
            if ($this->filters["area"] > 0){
                $s = $s . " AND area =  " . $this->filters["area"] . " ";
            }
            if ($this->filters["stage"] > 0){
                $s = $s . " AND stage =  " . $this->filters["stage"] . " ";
            }
            if ($this->filters["difficulty"] > 0){
                $s = $s . " AND difficulty =  " . $this->filters["difficulty"] . " ";
            }
            if (!$this->filters["defeat"]){
                $s = $s . " AND win = 1 ";
            }
            if (!$this->filters["helper"]){
                $s = $s . " AND helper = 0 ";
            }
            if ($this->filters["date_min"] != null){
                $s = $s . " AND date(dtime) >= date('" . $this->filters["date_min"] . "') ";
            }
            if ($this->filters["date_max"] != null){
                $s = $s . " AND date(dtime) >= date('" . $this->filters["date_max"] . "') ";
            }
            $s = $s . " ORDER BY dtime DESC ";
            $s = $s . " LIMIT " . $this->filters["number"] . ";";
            return $s;
        }
    }
?>
