 <?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;
        }
    }
?>

