<?php
    /**
     * Skill 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_Skill_Level.php");
    require_once(PATH::ENTITY . "K_Effect.php");


    /**
     * Monster skill.
     *
     * Represents an object from the table 'k_skill'.
     *
     * @category Entity
     */
    class K_Skill extends Entity{

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

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

        /**
         * @var string Skill description.
         */
        public $description;

        /**
         * @var int Skill order.
         */
        public $slot;

        /**
         * @var int Skill cooldown turns.
         */
        public $cooltime;

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

        /**
         * @var bool Wether the skill is passive or not.
         */
        public $passive;

        /**
         * @var bool Wether the skill is AOE.
         */
        public $aoe;

        /**
         * @var int Skill max level.
         */
        public $max_level;

        /**
         * @var string Skill formula.
         */
        public $multiplier_formula;

        /**
         * @var K_SKill The skill upgrading this one.
         */
        public $upgrade;

        /**
         * @var \K_Skill_Level[] Available skill levels.
         */
        public $level = [];

        /**
         * @var \K_Effect[] Associated eeffects.
         */
        public $effect = [];

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * skill, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Skill identifier.
         */
        public function __construct($db, $id){
            parent::__construct($db);
            $s =
              "SELECT " .
              "  id, " .
              "  name, " .
              "  description, " .
              "  slot, " .
              "  cooltime, " .
              "  hits, " .
              "  passive, " .
              "  aoe, " .
              "  max_level, " .
              "  multiplier_formula " .
              "FROM k_skill " .
              "WHERE id = $id; ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            $this->id = $r["id"];
            $this->name = HTML::e($r["name"]);
            $this->description = HTML::e($r["description"]);
            $this->slot = $r["slot"];
            $this->cooltime = $r["cooltime"];
            $this->hits = $r["hits"];
            $this->passive = $r["passive"];
            $this->aoe = $r["aoe"];
            $this->max_level = $r["max_level"];
            $this->multiplier_formula = HTML::e($r["multiplier_formula"]);
            $s =
              "SELECT " .
              "  skill, " .
              "  level " .
              "FROM k_skill_level " .
              "WHERE skill = " . $this->id . " " .
              "ORDER BY level; ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->level, new K_Skill_Level($this->db, $r["skill"], $r["level"]));
            }
            $s =
              "SELECT effect " .
              "FROM k_skill_effect " .
              "WHERE skill = " . $this->id . "; ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->effect, new K_Effect($this->db, $r["effect"]));
            }
        }

        /**
         * Gets the path to the skill image. The image must be in the
         * img/skill/ directory, and its name must be the set id,
         * padded with '0' to 8 digits, and the extension must be '.png'.
         *
         * @return string Image URL, or a fixed unknown image.
         */
        public function get_image(){
            if (file_exists(PATH::BASE . "img/skill/" . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png")){
                return URL::IMG["SKILL"] . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return URL::IMG["UNKNOWN"];
            }
        }
    }
?>

