<?php
    /**
     * Catalog page 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 . "K_Unit.php");


    /**
     * Catalog page model.
     *
     * @category Page
     */
    class Catalog_Page extends Page{

        /**
         * @var \K_Unit[] List of units to show.
         */
        public $units = [];

        /**
         * 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 . "catalog.php";
            $this->parse_filters();
            $s_mon = $this->build_query();
            $q_mon = $this->db->query($s_mon);
            while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
                array_push($this->units, new K_Unit($db, $r_mon["id"], false));
            }
            error_log($s_mon);
            $this->title = "Catalog - SWDB";
            $this->description = "Catalog of all monsters";
            $this->canonical = URL::BASE . "catalog/";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            $this->filters = FILTER::CATALOG;
            if (isset($_GET["element"]) && property_exists(ELEMENT_ID, strtoupper($_GET["element"]))){
                $this->filters["ELEMENT"] = $_GET["element"];
            }
            if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
                $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
            }
            if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 5){
                $this->filters["MIN_STARS"] = $_GET["min_stars"];
            }
            if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){
                $this->filters["MAX_STARS"] = $_GET["max_stars"];
            }
            return;
        }

        /**
         * Builds the query to the rune table using the selected or default
         * filters.
         *
         * @return string The query to be executed.
         */
        private function build_query(){
            $s = "
                SELECT id 
                FROM k_unit 
                WHERE 
                  obtainable = 1 AND
                  awakens_from IS NULL AND
                  natural_stars >= " . $this->filters["MIN_STARS"] . " AND 
                  natural_stars <= " . $this->filters["MAX_STARS"];
            if(property_exists(ELEMENT_ID, $this->filters["ELEMENT"])){
                $s = $s . " AND element = " . $this->filters["ELEMENT"] . " ";
            }
            if ($this->filters["NAME"] != ""){
                $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%' ";
            }
            $s = $s . "
                ORDER BY
                  element = " . ELEMENT_ID::FIRE ." DESC,
                  element = " . ELEMENT_ID::WATER ." DESC,
                  element = " . ELEMENT_ID::WIND ." DESC,
                  element = " . ELEMENT_ID::LIGHT ." DESC,
                  element = " . ELEMENT_ID::DARK ." DESC,
                  natural_stars;
            ";
            return $s;
        }
    }
?>
