<?php
    /**
     * Rune entity file.
     *
     * Creates the entity and makes it available.
     *
     * @category Entity
     */

    /**
     * Require dependent entities if not present.
     */
    require_once(PATH::ENTITY . "Entity.php");
    require_once(PATH::ENTITY . "K_Rune_Set.php");

    /**
     * Owned rune.
     *
     * Represents an object from the table 'rune'.
     *
     * @category Entity
     */
    class Rune extends Entity{

        /**
         * @var int Owner identifier.
         */
        public $uid;

        /**
         * @var int Rune identifier.
         */
        public $id;

        /**
         * @var int ID of the monster the rune is assigned to.
         */
        public $assigned_to;

        /**
         * @var K_Rune_Set Rune set.
         */
        public $type;

        /**
         * @var int Position of the rune type (1-6).
         */
        public $slot;

        /**
         * @var int Rune stars.
         */
        public $stars;

        /**
         * @var bool Indicates if the rune is ancient.
         */
        public $ancient;

        /**
         * @var int Level of the rune.
         */
        public $level;

        /**
         * @var int Rune quality.
         *
         * @see QUALITY
         */
        public $quality;

        /**
         * @var int Rune original quality.
         *
         * @see QUALITY
         */
        public $original_quality;

        /**
         * @var string Rune quality name (uppercase).
         */
        public $quality_name;

        /**
         * @var string Rune original quality name (uppercase).
         */
        public $original_quality_name;

        /**
         * @var int Price of the rune.
         */
        public $value;

        /**
         * @var float Current efficiency of the rune.
         */
        public $efficiency;

        /**
         * @var float Maximum potential efficiency of the rune.
         */
        public $max_efficiency;

        /**
         * @var int Main stat of the rune.
         *
         * @see RUNE_STAT
         */
        public $main_stat;

        /**
         * @var string Main stat name (uppercase).
         */
        public $main_stat_name;

        /**
         * @var int Main stat value of the rune.
         */
        public $main_stat_value;

        /**
         * @var int Extra stat of the rune.
         *
         * @see RUNE_STAT
         */
        public $innate_stat;

        /**
         * @var string Extra stat name (uppercase).
         */
        public $innate_stat_name;

        /**
         * @var string Extra stat value of the rune.
         */
        public $innate_stat_value;

        /**
         * @var int Number of substats.
         */
        public $substats;

        /**
         * @var int Substat 1 of the rune.
         *
         * @see RUNE_STAT
         */
        public $substat_1;

        /**
         * @var string Substat 1 name (uppercase).
         */
        public $substat_1_name;

        /**
         * @var int Substat 1 value of the rune.
         */
        public $substat_1_value;

        /**
         * @var int Ammount gained by the substat 1 enchant.
         */
        public $substat_1_craft;

        /**
         * @var int Substat 2 of the rune.
         *
         * @see RUNE_STAT
         */
        public $substat_2;

        /**
         * @var string Substat 2 name (uppercase).
         */
        public $substat_2_name;

        /**
         * @var int Substat 2 value of the rune.
         */
        public $substat_2_value;

        /**
         * @var int Ammount gained by the substat 2 enchant.
         */
        public $substat_2_craft;

        /**
         * @var int Substat 3 of the rune.
         *
         * @see RUNE_STAT
         */
        public $substat_3;

        /**
         * @var string Substat 3 name (uppercase).
         */
        public $substat_3_name;

        /**
         * @var int Substat 3 value of the rune.
         */
        public $substat_3_value;

        /**
         * @var int Ammount gained by the substat 3 enchant.
         */
        public $substat_3_craft;

        /**
         * @var int Substat 4 of the rune.
         *
         * @see RUNE_STAT
         */
        public $substat_4;

        /**
         * @var string Substat 4 name (uppercase).
         */
        public $substat_4_name;

        /**
         * @var int Substat 4 value of the rune.
         */
        public $substat_4_value;

        /**
         * @var int Ammount gained by the substat 4 enchant.
         */
        public $substat_4_craft;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * rune, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Rune id.
         */
        public function __construct($db, $id){
            parent::__construct($db);
            $s =
              "SELECT " .
              "  id, " .
              "  assigned_to, " .
              "  type, " .
              "  slot, " .
              "  stars, " .
              "  level, " .
              "  ancient, " .
              "  quality, " .
              "  original_quality, " .
              "  value, " .
              "  efficiency, " .
              "  max_efficiency, " .
              "  main_stat, " .
              "  main_stat_value, " .
              "  innate_stat, " .
              "  innate_stat_value, " .
              "  substat_1, " .
              "  substat_1_value, " .
              "  substat_1_enchant, " .
              "  substat_1_grind, " .
              "  substat_2, " .
              "  substat_2_value, " .
              "  substat_2_enchant, " .
              "  substat_2_grind, " .
              "  substat_3, " .
              "  substat_3_value, " .
              "  substat_3_enchant, " .
              "  substat_3_grind, " .
              "  substat_4, " .
              "  substat_4_value, " .
              "  substat_4_enchant, " .
              "  substat_4_grind " .
              "FROM rune " .
              "WHERE id = '$id'; ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->id = $r["id"];
                $this->assigned_to = $r["assigned_to"];
                $this->type = new K_Rune_Set($this->db, $r["type"]);
                $this->slot = $r["slot"];
                $this->stars = $r["stars"];
                $this->level = $r["level"];
                $this->ancient = $r["ancient"];
                $this->quality = $r["quality"];
                $this->original_quality = $r["original_quality"];
                $this->value = $r["value"];
                $this->efficiency = $r["efficiency"];
                $this->max_efficiency = $r["max_efficiency"];
                $this->main_stat = $r["main_stat"];
                $this->main_stat_value = $r["main_stat_value"];
                $this->innate_stat = $r["innate_stat"];
                $this->innate_stat_value = $r["innate_stat_value"];
                $this->substat_1 = $r["substat_1"];
                $this->substat_1_value = $r["substat_1_value"];
                $this->substat_1_enchant = $r["substat_1_enchant"];
                $this->substat_1_grind = $r["substat_1_grind"];
                $this->substat_2 = $r["substat_2"];
                $this->substat_2_value = $r["substat_2_value"];
                $this->substat_2_enchant = $r["substat_2_enchant"];
                $this->substat_2_grind = $r["substat_2_grind"];
                $this->substat_3 = $r["substat_3"];
                $this->substat_3_value = $r["substat_3_value"];
                $this->substat_3_enchant = $r["substat_3_enchant"];
                $this->substat_3_grind = $r["substat_3_grind"];
                $this->substat_4 = $r["substat_4"];
                $this->substat_4_value = $r["substat_4_value"];
                $this->substat_4_enchant = $r["substat_4_enchant"];
                $this->substat_4_grind = $r["substat_4_grind"];
                $this->refresh_names();
            }
        }

        /**
         * Calculates name variables.
         */
        private function refresh_names(){
            $this->main_stat_name = $this->stat_name($this->main_stat);
            $this->innate_stat_name = $this->stat_name($this->innate_stat);
            $this->substat_1_name = $this->stat_name($this->substat_1);
            $this->substat_2_name = $this->stat_name($this->substat_2);
            $this->substat_3_name = $this->stat_name($this->substat_3);
            $this->substat_4_name = $this->stat_name($this->substat_4);
            $this->quality_name = $this->quality_name($this->quality);
            $this->original_quality_name = $this->quality_name($this->original_quality);
        }

        /**
         * Gets a stat name from the mappings.
         *
         * @param int $stat Stat id.
         * @return string Stat name (Uppercase)
         */
        private function stat_name($stat){
            switch ($stat){
                case RUNE_STAT_ID::HP:
                    return "HP";
                case RUNE_STAT_ID::HP_P:
                    return "HP%";
                case RUNE_STAT_ID::ATK:
                    return "ATK";
                case RUNE_STAT_ID::ATK_P:
                    return "ATK%";
                case RUNE_STAT_ID::DEF:
                    return "DEF";
                case RUNE_STAT_ID::DEF_P:
                    return "DEF%";
                case RUNE_STAT_ID::SPD:
                    return "SPD";
                case RUNE_STAT_ID::CRR:
                    return "CRR";
                case RUNE_STAT_ID::CRD:
                    return "CRD";
                case RUNE_STAT_ID::RES:
                    return "RES";
                case RUNE_STAT_ID::ACC:
                    return "ACC";
                default:
                    return "";
            }
        }

        /**
         * Gets a stat name frm the mappings.
         *
         * @param int $q Quality id.
         * @return string Quality (Uppercase)
         */
        private function quality_name($q){
            switch ($q){
                case QUALITY_ID::NORMAL:
                    return "NORMAL";
                case QUALITY_ID::MAGIC:
                    return "MAGIC";
                case QUALITY_ID::RARE:
                    return "RARE";
                case QUALITY_ID::HERO:
                    return "HERO";
                case QUALITY_ID::LEGEND:
                    return "LEGEND";
                default:
                    return "";
            }
        }

    }
?>
