<?php

    require_once($path["entity"] . "Entity.php");
    require_once($path["entity"] . "K_Skill.php");
    require_once($path["entity"] . "K_Leader_Skill.php");
    require_once($path["entity"] . "K_Source.php");


    /**
     * Monster.
     *
     * Represents an object from the table 'monster'.
     */
    class K_Unit extends Entity{

        /**
         * Monster identifier.
         */
        public $id;

        /**
         * Monster family id.
         */
        public $family;

        /**
         * Monster name.
         */
        public $name;

        /**
         * Monster element.
         */
        public $element;

        /**
         * Element name (title case)
         */
        public $element_name;

        /**
         * Monster archetype.
         */
        public $archetype;

        /**
         * Archetype name (title case).
         */
        public $archetype_name;

        /**
         * Default star level.
         */
        public $base_stars;

        /**
         * Default star level.
         */
        public $natural_stars;

        /**
         * Indicates if the monster is obtainable.
         */
        public $obtainable;

        /**
         * Indicates if the monster can be awakened.
         */
        public $can_awaken;

        /**
         * Bonus when awakening.
         */
        public $awaken_bonus;

        /**
         * Required skill-ups.
         */
        public $skill_ups_to_max;

        /**
         * @{see K_Leader_Skill} of the monster.
         */
        public $leader_skill;

        /**
         * Base HP.
         */
        public $hp;

        /**
         * Base ATK.
         */
        public $atk;

        /**
         * Base DEF.
         */
        public $defense;

        /**
         * SPD.
         */
        public $speed;

        /**
         * CRR.
         */
        public $crit_rate;

        /**
         * CRD.
         */
        public $crit_damage;

        /**
         * RES
         */
        public $resistance;

        /**
         * ACC.
         */
        public $accuracy;

        /**
         * Raw HP.
         */
        public $raw_hp;

        /**
         * Raw ATK.
         */
        public $raw_atk;

        /**
         * Raw DEF.
         */
        public $raw_defense;

        /**
         * HP at max_level.
         */
        public $max_lvl_hp;

        /**
         * ATK at max_level.
         */
        public $max_lvl_atk;

        /**
         * DEF at max_level.
         */
        public $max_lvl_defense;

        /**
         * @{see Monster} it awakens from.
         */
        public $awakens_from;

        /**
         * @{see Monster} it awakens to.
         */
        public $awakens_to;

        /**
         * Indicates if the monster is a fusion ingredient.
         */
        public $fusion_food;

        /**
         * Indicates if the monster is homunculus.
         */
        public $homunculus;

        /**
         * The crafting cost (homunculus only).
         */
        public $craft_cost;

        /**
         * All {@see K_Monster_Skill}s.
         */
        public $skill = [];

        /**
         * Required essences to awaken the monster. Formed by arrays of:
         * [
         *   "element" => "",
         *   "grade" => "",
         *   "amount" => 
         * ]
         */
        public $essences = [];

        /**
         * {@see K_Source}s the monster can be get from.
         */
        public $sources = [];

        /**
         * Unit title.
         * If awakened, it will be the awakened from name.
         * If unawakened, it will be <element> <name>.
         */
        public $title;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * monster, populating it and it's items.
         *
         * @param SQLite3 $db Connection to the database.
         * @param int $id Monster identifier.
         * @param complete If false, query only k_monster.
         */
        public function __construct($db, $id, $complete = true){
            global $path;
            global $ELEMENT;
            global $ARCHETYPE;
            parent::__construct($db);
            $s =
              "SELECT " .
              "  id, " .
              "  family, " .
              "  name, " .
              "  element, " .
              "  archetype, " .
              "  base_stars, " .
              "  natural_stars, " .
              "  obtainable, " .
              "  can_awaken, " .
              "  awaken_bonus, " .
              "  skill_ups_to_max, " .
              "  leader_skill, " .
              "  hp, " .
              "  attack, " .
              "  defense, " .
              "  speed, " .
              "  crit_rate, " .
              "  crit_damage, " .
              "  resistance, " .
              "  accuracy, " .
              "  raw_hp, " .
              "  raw_attack, " .
              "  raw_defense, " .
              "  max_lvl_hp, " .
              "  max_lvl_attack, " .
              "  max_lvl_defense, " .
              "  awakens_from, " .
              "  awakens_to, " .
              "  fusion_food, " .
              "  homunculus, " .
              "  craft_cost " .
              "FROM k_unit " .
              "WHERE id = $id; ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->id = $r["id"];
                $this->family = $r["family"];
                $this->name = $r["name"];
                $this->element = $r["element"];
                switch($this->element){
                    case $ELEMENT["WATER"]:
                        $this->element_name = "Water";
                        break;
                    case $ELEMENT["FIRE"]:
                        $this->element_name = "Fire";
                        break;
                    case $ELEMENT["WIND"]:
                        $this->element_name = "Wind";
                        break;
                    case $ELEMENT["LIGHT"]:
                        $this->element_name = "Light";
                        break;
                    case $ELEMENT["DARK"]:
                        $this->element_name = "Dark";
                        break;
                }
                $this->archetype = $r["archetype"];
                switch($this->archetype){
                    case $ARCHETYPE["ATTACK"]:
                        $this->archetype_name = "Attack";
                        break;
                    case $ARCHETYPE["DEFENSE"]:
                        $this->archetype_name = "Defense";
                        break;
                    case $ARCHETYPE["HP"]:
                        $this->archetype_name = "HP";
                        break;
                    case $ARCHETYPE["SUPPORT"]:
                        $this->archetype_name = "Support";
                        break;
                    case $ARCHETYPE["MATERIAL"]:
                        $this->archetype_name = "Material";
                        break;
                }
                $this->base_stars = $r["base_stars"];
                $this->natural_stars = $r["natural_stars"];
                $this->obtainable = $r["obtainable"];
                $this->can_awaken = $r["can_awaken"];
                $this->awaken_bonus = $r["awaken_bonus"];
                $this->skill_ups_to_max = $r["skill_ups_to_max"];
                if (strlen($r["leader_skill"]) > 0){
                    $this->leader_skill = new K_Leader_Skill($this->db, $r["leader_skill"]);
                }
                $this->hp = $r["hp"];
                $this->attack = $r["attack"];
                $this->defense = $r["defense"];
                $this->speed = $r["speed"];
                $this->crit_rate = $r["crit_rate"];
                $this->crit_damage = $r["crit_damage"];
                $this->resistance = $r["resistance"];
                $this->accuracy = $r["accuracy"];
                $this->raw_hp = $r["raw_hp"];
                $this->raw_attack = $r["raw_attack"];
                $this->raw_defense = $r["raw_defense"];
                $this->max_lvl_hp = $r["max_lvl_hp"];
                $this->max_lvl_attack = $r["max_lvl_attack"];
                $this->max_lvl_defense = $r["max_lvl_defense"];
                $this->awakens_from = $r["awakens_from"];
                $this->awakens_to = $r["awakens_to"];
                $this->fusion_food = $r["fusion_food"];
                $this->homunculus = $r["homunculus"];
                $this->craft_cost = $r["craft_cost"];
            }
            if ($complete){
                $this->load_essences();
                $this->load_skills();
                $this->load_sources();
            }
            // No to, no from. Silver monster.
            if ($this->awakens_from == "" && $this->awakens_to == ""){
                $this->title = $this->element_name . " " . $this->name;
            }
            // Awakened
            elseif ($this->awakens_from != ""){
                $this->title = $this->name;
            }
            // Gold
            elseif (strlen($this->awakens_to) > 0){
                $this->title = $this->element_name . " " . $this->name;
            }
        }

        /**
         * Loads the information about the essences to awake the monster. 
         * It is autoatically called at construction time if the
         * $complete parameter is true (by default). No point in calling it
         * twice.
         */
        public function load_essences(){
            $this->essences = [];
            $s =
              "SELECT " .
              "  element, " .
              "  grade, " .
              "  amount " .
              "FROM k_unit_essence " .
              "WHERE " .
              "  unit = " . $this->id . " " .
              "ORDER BY " .
              "  element <> 'Magic', " .
              "  grade <> 'Low', " .
              "  grade <> 'Mid'; ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                $e = [
                    "element" => $r["element"],
                    "grade" => $r["grade"],
                    "amount" => $r["amount"]
                ];
                array_push($this->essences, $e);
            }
        }

        /**
         * Loads the information about the monster skills. 
         * It is autoatically called at construction time if the
         * $complete parameter is true (by default). No point in calling it
         * twice.
         */
        public function load_skills(){
            $this->skill = [];
            $s =
              "SELECT " .
              "  skill " .
              "FROM " .
              "  k_skill, " .
              "  k_unit_skill " .
              "WHERE " .
              "  id = skill AND " .
              "  unit = " . $this->id . " " .
              "ORDER BY slot; ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->skill, new K_Skill($this->db, $r["skill"]));
            }
        }

        /**
         * Loads the information about the monster sources. 
         * It is autoatically called at construction time if the
         * $complete parameter is true (by default). No point in calling it
         * twice.
         */
        public function load_sources(){
            $this->sources = [];
            $s =
              "SELECT source " .
              "FROM k_unit_source " .
              "WHERE unit = " . $this->id . ";";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->sources, new K_Source($this->db, $r["source"]));
            }
        }

        /**
         * Gets the path to the unit image. The image must be in the
         * img/unit/ directory, and it's name must be the unit id,
         * padded with '0' to 8 digits, and the extension must be '.png'.
         *
         * @return Path to the image, or a path to a fixed unknown image if it
         *         doesn't exist.
         */
        public function get_image(){
            global $dir;
            global $path;
            if (file_exists($dir . "img/unit/" . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png")){
                return $path["img"]["unit"] . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return $path["img"]["root"] . "unknown.png";
            }
        }

    }
?>
