<?php
    /**
     * Transformation 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.php");
    require_once(PATH::ENTITY . "K_Leader_Skill.php");
    require_once(PATH::ENTITY . "K_Source.php");


    /**
     * Unit transformation.
     *
     * Represents an object from the table 'k_unit_transformation'.
     *
     * @category Entity
     */
    class K_Unit_Transformation extends Entity{

        /**
         * @var int Transformed unit identifier.
         */
        public $id;

        /**
         * @var int Transformed unit family id.
         */
        public $family;

        /**
         * @var int ID of the unit that transformes into this.
         */
        public $unit;

        /**
         * @var \K_Monster_Skill[] Skills in the transformed form.
         */
        public $skill = [];

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * trnsformed unit, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id TRansformed unit identifier.
         */
        public function __construct($db, $id){
            parent::__construct($db);
            $s = "
              SELECT
                id,
                family,
                unit
              FROM k_unit_transformation
              WHERE id = $id;
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->id = $r["id"];
                $this->family = $r["family"];
                $this->unit = $r["unit"];
                $this->load_skills();
            }
        }

        /**
         * Loads the information about the unit skills in the transformed form.
         */
        private function load_skills(){
            $this->skill = [];
            $s = "
              SELECT skill
              FROM
                k_skill,
                k_unit_skill
              WHERE
                id = skill AND
                unit = " . $this->id . "
              ORDER BY slot;
            ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->skill, new K_Skill($this->db, $r["skill"]));
            }
        }

        /**
         * Gets the path to the unit image. The image must be in the
         * img/unit/ directory, and it's name must be the unit 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/unit/" . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png")){
                return URL::IMG["UNIT"] . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return URL::IMG["UNKNOWN"];
            }
        }

    }
?>
