<?php
    /**
     * Rune craft items 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 craft item.
     *
     * Represents an object from the table 'rune_craft'.
     *
     * @category Entity
     */
    class Rune_Craft extends Entity{

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

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

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

        /**
         * @var string Item quality name.
         */
        public $quality_name;

        /**
         * @var K_Rune_Set Rune set the item can be used on.
         */
        public $rune;

        /**
         * @var int Stat the item modifies.
         *
         * @see RUNE_STAT
         */
        public $stat;

        /**
         * @var string Stat name.
         */
        public $stat_name;

        /**
         * @var bool Indicates if the Item is a gem.
         */
        public $gem;

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

        /**
         * @var bool Indicates if the Item is inmemorial.
         */
        public $inmemorial;

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

        /**
         * @var int Minimum value of the stat.
         */
        public $min;

        /**
         * @var int Maximum value of the stat.
         */
        public $max;

        /**
         * @var int Sell value.
         */
        public $value;

        /**
         * 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 " .
              "  rune_craft.id AS id, " .
              "  rune_craft.quality As quality, " .
              "  rune_craft.rune AS rune, " .
              "  rune_craft.stat AS stat, " .
              "  rune_craft.value AS value, " .
              "  k_rune_craft_type.name AS name, " .
              "  k_rune_craft_type.gem AS gem, " .
              "  k_rune_craft_type.inmemorial AS inmemorial, " .
              "  k_rune_craft_type.ancient AS ancient, " .
              "  k_rune_craft_range.min AS min, " .
              "  k_rune_craft_range.max AS max " .
              "FROM " .
              "  rune_craft, ".
              "  k_rune_craft_type, " .
              "  k_rune_craft_range " .
              "WHERE " . 
              "  rune_craft.id = $id AND " .
              "  rune_craft.type = k_rune_craft_type.id AND " .
              "  k_rune_craft_type.gem = k_rune_craft_range.gem AND " .
              "  k_rune_craft_type.gem = k_rune_craft_range.gem AND " .
              "  k_rune_craft_type.ancient = k_rune_craft_range.ancient AND " .
              "  rune_craft.quality = k_rune_craft_range.quality AND " .
              "  rune_craft.stat = k_rune_craft_range.stat;";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->id = $r["id"];
                $this->gem = $r["gem"];
                $this->quality = $r["quality"];
                $this->quality_name = $this->quality_name($r["quality"]);
                $this->stat = $r["stat"];
                $this->stat_name = $this->stat_name($r["stat"]);
                $this->ancient = $r["ancient"];
                $this->inmemorial = $r["inmemorial"];
                if ($this->inmemorial == 0){
                    $this->rune = new K_Rune_Set($this->db, $r["rune"]);
                    $this->name = $this->rune->name . " " . $r["stat"];
                }
                else{
                    $this->name = "Inmemorial " . $r["stat"];
                }
                $this->min = $r["min"];
                $this->max = $r["max"];
                $this->value = $r["value"];
            }
        }

        /**
         * Gets a stat name from the maps.
         *
         * @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 from the maps.
         *
         * @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 "";
            }
        }

    }
?>

