<?php

    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'.
     */
    class Rune_Craft extends Entity{

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

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

        /**
         * Item quality.
         */
        public $quality;

        /**
         * Item quality name.
         */
        public $quality_name;

        /**
         * {@see K_Rune_Set} the item modifies.
         */
        public $rune;

        /**
         * Stat the item modifies.
         */
        public $stat;

        /**
         * Stat name.
         */
        public $stat_name;

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

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

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

        /**
         * Item name.
         */
        public $name;

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

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

        /**
         * Sell value.
         */
        public $value;

        /**
         * 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 " .
              "  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){
            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 from 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 "";
            }
        }

    }
?>
