 <?php
    /**
     * Monster list 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 . "Unit.php");

    /**
     * Monster list page model.
     *
     * @category Page
     */
    class Monsters_Page extends Page{

        /**
         * @var \Unit[] Units to show.
         */
        public $monsters = [];

        /**
         * @var mixed[] Stats gained by buildings.
         */
        public $building_stats = [
            "atk" => 0,
            "def" => 0,
            "hp" => 0,
            "spd" => 0,
            "crd" => 0,
            "elm" => [
                0 => 0,
                1 => 0,
                2 => 0,
                3 => 0,
                4 => 0
            ]
        ];

        /**
         * 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 . "monsters.php";
            $this->parse_filters();
            $this->calculate_buildings();
            $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 Unit($db, $r_mon["id"]));
            }
            $this->title = "Monsters - SWDB";
            $this->description = "Owned monsters";
            $this->canonical = URL::BASE . "monsters/";
        }

        /**
         * Parses the request looking for the selected filters, validates them
         * and adds them to the $filters array.
         */
        private function parse_filters(){
            $this->filters = FILTER::MONSTER;
            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"]) <= 6){
                $this->filters["min_stars"] = $_GET["min_stars"];
            }
            if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
                $this->filters["max_stars"] = $_GET["max_stars"];
            }
            if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
                $this->filters["in_storage"] = true;
            }
            if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
                $this->filters["without_runes"] = true;
            }
            return;
        }

        /**
         * Builds the query to the rune 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 unit.id AS id
                FROM
                  unit,
                  k_unit
                WHERE
                  unit.uid = '$UID' AND
                  unit.unit = k_unit.id AND 
                  stars >= " . $this->filters["min_stars"] . " AND 
                  stars <= " . $this->filters["max_stars"];
            if (property_exists(ELEMENT_ID, $this->filters["element"])){
                $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE element = " . $this->filters["element"] . ") ";
            }
            if ($this->filters["name"] != ""){
                $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
            }
            if ($this->filters["in_storage"]){
                $s = $s . " AND in_storage = 0 ";
            }
            if (!$this->filters["without_runes"]){
                $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
            }
            $s = $s . "
                ORDER BY 
                  stars DESC,
                  level DESC,
                  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
            ";
            return $s;
        }

        /**
         * Calculates the stats gained from bulding upgrades and populates the
         * $building_stats array.
         */
        private function calculate_buildings(){
            global $STAT;
            global $EFFECT_AREA;
            global $UID;
            $s = "
              SELECT
                affected_stat,
                bonus,
                element
              FROM
                k_decoration,
                k_decoration_level,
                decoration
              WHERE
                decoration.uid = '$UID' AND
                k_decoration.id = decoration.decoration AND
                decoration.decoration = k_decoration_level.decoration AND
                decoration.level = k_decoration_level.level AND
                area = " . EFFECT_AREA::GENERAL . " AND
                affected_stat IN (" . STAT::ATK . ", " . STAT::DEF . ", " . STAT::HP . ", " . STAT::SPD . ", " . STAT::CRIT_DMG . ");
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                switch($r["affected_stat"]){
                    case STAT::DEF:
                        $this->building_stats["def"] += $r["bonus"];
                        break;
                    case STAT::HP:
                        $this->building_stats["hp"] += $r["bonus"];
                        break;
                    case STAT::SPD:
                        $this->building_stats["spd"] += $r["bonus"];
                        break;
                    case STAT::CRIT_DMG:
                        $this->building_stats["crd"] += $r["bonus"];
                        break;
                    case STAT::ATK:
                        if(strlen($r["element"]) > 0){
                            $this->building_stats["elm"][$r["element"]] += $r["bonus"];
                        }
                        else{
                            $this->building_stats["atk"] += $r["bonus"];
                        }
                        break;
                }
            }
        }
    }
?>

