<?php

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

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

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

        /**
         * Skill attribute.
         */
        public $attribute;

        /**
         * Attribute incrrease amount.
         */
        public $amount;

        /**
         * Effect area.
         */
        public $area;

        /**
         * Affected element.
         */
        public $element;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * skill, populating it and it's items.
         *
         * @param SQLite3 $db Connection to the database.
         * @param int $id Skill identifier.
         */
        public function __construct($db, $id){
            global $path;
            parent::__construct($db);
            $s =
              "SELECT " .
              "  id, " .
              "  stat, " .
              "  amount, " .
              "  area, " .
              "  element " .
              "FROM k_leader_skill " .
              "WHERE id = $id; ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            $this->id = $r["id"];
            $this->attribute = $r["stat"];
            $this->amount = $r["amount"];
            $this->area = $r["area"];
            $this->element = $r["element"]; 
        }

        /**
         * Gets the path to the skill image. The image must be in the
         * img/skill_leader/ directory, and its name must be the skill id,
         * padded with '0' to 4 digites, and the extension must be '.png'.
         *
         * @return Path to the image, or a path to a fixed unknown image if it
         *         doesn't exist.
         */
        public function get_image(){
            global $base_dir;
            global $path;
            if (file_exists($base_dir . "img/content/skill_leader/" . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png")){
                return $path["img"]["skill_leader"] . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return $path["img"]["root"] . "unknown.png";
            }
        }

        /**
         * Composes a human readable description of the skill.
         *
         * @return The description.
         */
        public function create_description(){
            global $STAT;
            global $ELEMENT;
            global $EFFECT_AREA;
            $s = "Increases the ";
            switch ($this->attribute){
                case $STAT["ATK"]:
                    $s .= "attack power";
                    break;
                case $STAT["DEF"]:
                    $s .= "defense";
                    break;
                case $STAT["HP"]:
                    $s .= "HP";
                    break;
                case $STAT["SPD"]:
                    $s .= "attack speed";
                    break;
                case $STAT["CRIT_RATE"]:
                    $s .= "critical rate";
                    break;
                case $STAT["CRIT_DMG"]:
                    $s .= "critical damage";
                    break;
                case $STAT["ACC"]:
                    $s .= "accuracy";
                    break;
                case $STAT["RES"]:
                    $s .= "resistance";
                    break;
            }
            $s .= " of ally monsters ";
            switch ($this->element){
                case $ELEMENT["WATER"]:
                    $s .= " with Water attribute ";
                    break;
                case $ELEMENT["FIRE"]:
                    $s .= " with Fire attribute ";
                    break;
                case $ELEMENT["WIND"]:
                    $s .= " with Wind attribute ";
                    break;
                case $ELEMENT["LIGHT"]:
                    $s .= " with Light attribute ";
                    break;
                case $ELEMENT["DARK"]:
                    $s .= " with Dark attribute ";
                    break;
            }
            $s = $s . "by " . $this->amount . "%";
            switch ($this->area){
                case $EFFECT_AREA["DUNGEON"]:
                    $s = $s . " in the Dungeons";
                    break;
                case $EFFECT_AREA["ARENA"]:
                    $s = $s . " in the Arena";
                    break;
                case $EFFECT_AREA["GUILD"]:
                    $s = $s . " in Guild content";
                    break;
            }
            $s = $s . ".";
            return $s;
        }

    }
?>
