<?php

    require_once($path["entity"] . "Entity.php");
    require_once($path["entity"] . "K_Rune_Set.php");


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

        /**
         * Owner identifier.
         */
        public $uid;

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

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

        /**
         * {@see K_Rune_Set}.
         */
        public $type;

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

        /**
         * Rune stars.
         */
        public $stars;

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

        /**
         * Level of the rune.
         */
        public $level;

        /**
         * Rune quality.
         */
        public $quality;

        /**
         * Rune original quality.
         */
        public $original_quality;

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

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

        /**
         * Price of the rune.
         */
        public $value;

        /**
         * Current efficiency of the rune.
         */
        public $efficiency;

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

        /**
         * Main stat of the rune.
         */
        public $main_stat;

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

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

        /**
         * Extra stat of the rune.
         */
        public $innate_stat;

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

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

        /**
         * Number of substats.
         */
        public $substats;

        /**
         * Substat 1 of the rune.
         */
        public $substat_1;

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

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

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

        /**
         * Substat 2 of the rune.
         */
        public $substat_2;

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

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

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

        /**
         * Substat 3 of the rune.
         */
        public $substat_3;

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

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

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

        /**
         * Substat 4 of the rune.
         */
        public $substat_4;

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

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

        /**
         * 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 SQLite3 $db Connection to the database.
         * @param int $id Rune id.
         */
        public function __construct($db, $id){
            global $path;
            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();
            }
        }

        /**
         * Decodes some properties and assigns name variables.
         */
        public 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 frm the maps.
         *
         * @param int $stat Stat id.
         * @return string Stat name (Uppercase)
         */
        private function stat_name($stat){
            global $RUNE_STAT;
            switch ($stat){
                case $RUNE_STAT["HP"]:
                    return "HP";
                case $RUNE_STAT["HP%"]:
                    return "HP%";
                case $RUNE_STAT["ATK"]:
                    return "ATK";
                case $RUNE_STAT["ATK%"]:
                    return "ATK%";
                case $RUNE_STAT["DEF"]:
                    return "DEF";
                case $RUNE_STAT["DEF%"]:
                    return "DEF%";
                case $RUNE_STAT["SPD"]:
                    return "SPD";
                case $RUNE_STAT["CRR"]:
                    return "CRR";
                case $RUNE_STAT["CRD"]:
                    return "CRD";
                case $RUNE_STAT["RES"]:
                    return "RES";
                case $RUNE_STAT["ACC"]:
                    return "ACC";
                default:
                    return "";
            }
        }

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

    }
?>
