<?php
    /**
     * Home 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");
    require_once(PATH::ENTITY . "Building.php");
    require_once(PATH::ENTITY . "Decoration.php");
    require_once(PATH::ENTITY . "Inventory.php");


    /**
     * Home page model.
     *
     * @category Page
     */
    class Home_Page extends Page{

        /**
         * @var int Player ID.
         */
        public $uid;

        /**
         * @var int Player level.
         */
        public $level;

        /**
         * @var string Player name.
         */
        public $name;

        /**
         * @var string Player country (XX).
         */
        public $country;

        /**
         * @var int Total experience.
         */
        public $experience;

        /**
         * @var Unit Representative Unit.
         */
        public $rep;

        /**
         * @var \Unit[] Unitss in Arena defense.
         */
        public $defense = [];

        /**
         * @var int[] Currency.
         */
        public $currency = [
            "mana" => 0,
            "crystal" => 0,
            "energy" => 0,
            "energy_max" => 0,
            "arena_energy" => 0,
            "arena_energy_max" => 0,
            "darkportal_energy" => 0,
            "darkportal_energy_max" => 0,
            "dimension_energy" => 0,
            "dimension_energy_max" => 0,
            "honor_point" => 0,
            "guild_point" => 0,
            "honor_medal" => 0,
            "honor_mark" => 0,
            "event_coin" => 0,
            "social_point" => 0,
            "ancient_stone" => 0,
            "costume_point" => 0,
        ];

        /**
         * @var \Building[] List of Buildings.
         */
        public $building = [];

        /**
         * @var \Decoration[] List of Decorations.
         */
        public $decoration = [];

        /**
         * @var \Inventory[] List of Scrolls in Inventory.
         */
        public $inventory_scroll = [];

        /**
         * @var \Inventory[] List of rune crafting items in Inventory.
         */
        public $inventory_craft_rune = [];

        /**
         * @var \Inventory[] List of rune crafting items in Inventory.
         */
        public $inventory_craft = [];

        /**
         * @var \Inventory[] List of rune crafting items in Inventory.
         */
        public $inventory_essence = [];

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param resource $db Connection to the database.
         * @global int Player ID.
         */
        public function __construct($db){
            global $INVENTORY_TYPE;
            global $UID;
            parent::__construct($db);
            $this->view = PATH::VIEW . "home.php";
            $s = "
              SELECT
                uid,
                name,
                level,
                country,
                experience,
                rep,
                mana,
                crystal,
                energy,
                energy_max,
                arena_energy,
                arena_energy_max,
                darkportal_energy,
                darkportal_energy_max,
                dimension_energy,
                dimension_energy_max,
                honor_point,
                guild_point,
                honor_medal,
                honor_mark,
                event_coin,
                social_point,
                costume_point
              FROM player
              WHERE uid = '$UID';
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->uid = $r["uid"];
                $this->name = $r["name"];
                $this->level = $r["level"];
                $this->country = $r["country"];
                $this->experience = $r["experience"];
                if (strlen($r["rep"]) > 0){
                    $this->rep = new Unit($this->db, $r["rep"]);
                }
                $this->currency["mana"] = $r["mana"];
                $this->currency["crystal"] = $r["crystal"];
                $this->currency["energy"] = $r["energy"];
                $this->currency["energy_max"] = $r["energy_max"];
                $this->currency["arena_energy"] = $r["arena_energy"];
                $this->currency["arena_energy_max"] = $r["arena_energy_max"];
                $this->currency["darkportal_energy"] = $r["darkportal_energy"];
                $this->currency["darkportal_energy_max"] = $r["darkportal_energy_max"];
                $this->currency["dimension_energy"] = $r["dimension_energy"];
                $this->currency["dimension_energy_max"] = $r["dimension_energy_max"];
                $this->currency["honor_point"] = $r["honor_point"];
                $this->currency["guild_point"] = $r["guild_point"];
                $this->currency["honor_medal"] = $r["honor_medal"];
                $this->currency["honor_mark"] = $r["honor_mark"];
                $this->currency["event_coin"] = $r["event_coin"];
                $this->currency["social_point"] = $r["social_point"];
                $this->currency["costume_point"] = $r["costume_point"];
            }
            $s = "
                SELECT unit
                FROM defense
                WHERE uid = '$UID'
                ORDER BY position;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->defense, new Unit($db, $r["unit"], false));
            }
            $s = "
                SELECT DISTINCT id
                FROM building
                WHERE uid = '$UID';
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->building, new Building($db, $r["id"]));
            }
            $s = "
                SELECT id
                FROM decoration
                WHERE uid = '$UID';
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->decoration, new Decoration($db, $r["id"]));
            }
            $s = "
                SELECT
                  id,
                  type
                FROM inventory
                WHERE
                  uid = '$UID' AND
                  type = " . INVENTORY_TYPE_ID::SCROLL . " AND
                  amount > 0
                ORDER BY inventory.type;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->inventory_scroll, new Inventory($db, $r["id"], $r["type"]));
            }
            // Rune crafting items are marked as generic crafting.
            $s = "
                SELECT
                  inventory.id AS id,
                  inventory.type AS type
                FROM
                  inventory
                WHERE
                  inventory.uid = '$UID' AND
                  inventory.type = " . INVENTORY_TYPE_ID::RUNE_CRAFT . " AND
                  amount > 0
                ORDER BY inventory.id;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->inventory_craft_rune, new Inventory($db, $r["id"], $r["type"]));
            }
            $s = "
                SELECT
                  inventory.id AS id,
                  inventory.type AS type
                FROM
                  inventory
                WHERE
                  inventory.uid = '$UID' AND
                  inventory.type = " . INVENTORY_TYPE_ID::CRAFT_STUFF . " AND
                  amount > 0
                ORDER BY inventory.id;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->inventory_craft, new Inventory($db, $r["id"], $r["type"]));
            }
            $s = "
                SELECT
                  inventory.id AS id,
                  inventory.type AS type
                FROM
                  inventory,
                  k_inventory
                WHERE
                  inventory.id = k_inventory.id AND
                  inventory.type = k_inventory.type AND
                  inventory.uid = '$UID' AND
                  inventory.type = " . INVENTORY_TYPE_ID::ESSENCES . " AND
                  amount > 0
                ORDER BY
                  upper(name) NOT LIKE '%MAGIC%',
                  upper(name) NOT LIKE '%FIRE%',
                  upper(name) NOT LIKE '%WATER%',
                  upper(name) NOT LIKE '%WIND%',
                  upper(name) NOT LIKE '%LIGHT%',
                  upper(name) NOT LIKE '%DARK%',
                  upper(name) NOT LIKE '%LOW%',
                  upper(name) NOT LIKE '%MID%',
                  upper(name) NOT LIKE '%HIGH%'
                ;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->inventory_essence, new Inventory($db, $r["id"], $r["type"]));
            }
            $this->title = "SWDB";
            $this->description = "Summoners War DataBase";
            $this->canonical = URL::BASE;
        }
    }
?>

