<?php

    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.
     */
    class Home_Page extends Page{

        /**
         * Player ID.
         */
        public $uid;

        /**
         * Player name.
         */
        public $name;

        /**
         * Player country (XX).
         */
        public $country;

        /**
         * Total experience.
         */
         public $experience;

        /**
         * Representative {@see Unit}.
         */
        public $rep;

        /**
         * {@see Unit}s in Arena defense.
         */
        public $defense = [];

        /**
         * 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,
        ];

        /**
         * List of {@see Building}s.
         */
        public $building = [];

        /**
         * List of {@see Decoration}s.
         */
        public $decoration = [];

        /**
         * List of Scrolls in {@see Inventory}s.
         */
        public $inventory_scroll = [];

        /**
         * List of rune crafting items in {@see Inventory}s.
         */
        public $inventory_craft_rune = [];

        /**
         * List of rune crafting items in {@see Inventory}s.
         */
        public $inventory_craft = [];

        /**
         * List of rune crafting items in {@see Inventory}s.
         */
        public $inventory_essence = [];

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param SQLite3 $db Connection to the database.
         * @param string $lang Lowercase, two-letter language code.
         */
        public function __construct($db){
            global $path;
            global $root;
            global $INVENTORY_TYPE;
            global $UID;
            parent::__construct($db);
            $this->view = $path["view"] . "home.php";
            $s = "
              SELECT
                uid,
                name,
                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->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 building
                FROM building
                WHERE uid = '$UID';
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->building, new Building($db, $r["building"]));
            }
            $s = "
                SELECT decoration
                FROM decoration
                WHERE uid = '$UID';
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->decoration, new Decoration($db, $r["decoration"]));
            }
            $s = "
                SELECT id
                FROM inventory
                WHERE
                  uid = '$UID' AND
                  type = " . $INVENTORY_TYPE["SCROLL"] . " AND
                  amount > 0;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->inventory_scroll, new Inventory($db, $r["id"]));
            }
            // Rune crafting items are marked as generic crafting.
            $s = "
                SELECT inventory.id
                FROM
                  inventory,
                  k_inventory
                WHERE
                  inventory.uid = '$UID' AND
                  inventory.id = k_inventory.id AND
                  k_inventory.type = " . $INVENTORY_TYPE["RUNE_CRAFT"] . " AND
                  amount > 0;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->inventory_craft_rune, new Inventory($db, $r["id"]));
            }
            $s = "
                SELECT inventory.id
                FROM
                  inventory,
                  k_inventory
                WHERE
                  inventory.uid = '$UID' AND
                  inventory.id = k_inventory.id AND
                  k_inventory.type = " . $INVENTORY_TYPE["CRAFT_STUFF"] . " AND
                  amount > 0;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->inventory_craft, new Inventory($db, $r["id"]));
            }
            $s = "
                SELECT inventory.id AS id
                FROM
                  inventory,
                  k_inventory
                WHERE
                  inventory.uid = '$UID' AND
                  k_inventory.id = inventory.id AND
                  k_inventory.type = " . $INVENTORY_TYPE["ESSENCES"] . "
                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"]));
            }
            $this->title = "SWDB";
            $this->description = "Summoners War DataBase";
            $this->canonical = $root;
        }
    }
?>
