 <?php
    /**
     * Run list file.
     *
     * Provides a class with all the properties and methods to display the page.
     *
     * @category Page
     */

    /**
     * Require dependent files if not present.
     */
    require_once(PATH::PAGE . "Page.php");
    require_once(PATH::ENTITY . "Run.php");

    /**
     * Run list page model.
     *
     * @category Page
     */
    class Runs_Page extends Page{

        /**
         * @var \Run[] Runs to show.
         */
        public $runs = [];

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param resource $db Connection to the database.
         */
        public function __construct($db){
            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 = URL::BASE . "runs/";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            $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_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::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 string The query to be executed.
         * @global int Player ID.
         */
        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;
        }
    }
?>

