<?php

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


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

        /**
         * Array of {@see K_Unit}.
         */
        public $monsters = [];

        /**
         * The 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"] . "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->monsters, new K_Unit($db, $r_mon["id"], false));
            }
            $this->title = "Catalog - SWDB";
            $this->description = "Catalog of all monsters";
            $this->canonical = $root . "catalog/";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            global $ELEMENT;
            global $FILTER_CATALOG;
            $this->filters = $FILTER_CATALOG;
            if (isset($_GET["element"]) && in_array($_GET["element"], $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 The query to be executed.
         */
        private function build_query(){
            global $ELEMENT;
            $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 (in_array($this->filters["element"], $ELEMENT)){
                $s = $s . " AND element = " . strtoupper($this->filters["element"]) . " ";
            }
            if ($this->filters["name"] != ""){
                $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%' ";
            }
            $s = $s . "
                ORDER BY
                  element = " . $ELEMENT["FIRE"] ." DESC,
                  element = " . $ELEMENT["WATER"] ." DESC,
                  element = " . $ELEMENT["WIND"] ." DESC,
                  element = " . $ELEMENT["LIGHT"] ." DESC,
                  element = " . $ELEMENT["DARK"] ." DESC,
                  natural_stars;
            ";
            return $s;
        }
    }
?>
