<?php
    /**
     * Decoration key entity file.
     *
     * Creates the entity and makes it available.
     *
     * @category Entity
     */

    /**
     * Require dependent entities if not present.
     */
    require_once(PATH::ENTITY . "Entity.php");


    /**
     * Decoration.
     *
     * Represents an object from the table 'k_decoration'.
     *
     * @category Entity
     */
    class K_Decoration extends Entity{

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

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

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

        /**
         * @var int Area of effect identifier (if any).
         */
        public $area;

        /**
         * @var int Affected stat id (if any).
         */
        public $stat;

        /**
         * @var int Affected element  id(if any).
         */
        public $element;

        /**
         * @var int Max. level.
         */
        public $max_level;

        /**
         * @var int[] Effect bonus per level.
         */
        public $level_bonus = [];

        /**
         * Cost per level.
         */
        public $level_cost = [];

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * decoration, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Decoration identifier.
         */
        public function __construct($db, $id){
            parent::__construct($db);
            $s = "
                SELECT
                  id,
                  name,
                  description,
                  area,
                  affected_stat,
                  element,
                  max_level
                FROM k_decoration
                WHERE id = $id;
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->id = $r["id"];
                $this->name = HTML::e($r["name"]);
                $this->description = HTML::e($r["description"]);
                $this->area = $r["area"];
                $this->stat = $r["affected_stat"];
                $this->element = $r["element"];
                $this->max_level = $r["max_level"];
                $s = "
                    SELECT
                      level,
                      bonus,
                      cost
                    FROM k_decoration_level
                    WHERE decoration = $id
                ";
                $q = $this->db->query($s);
                while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                    $this->level_bonus[$r["level"]] = $r["bonus"];
                    $this->level_cost[$r["level"]] = $r["cost"];
                }
            }
        }

        /**
         * Gets the path to the building image. The image must be in the
         * img/decoration/ directory, and its name must be the decoration
         * id, padded with '0' to 4 digites, 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/decoration/" . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png")){
                return URL::IMG["DECORATION"] . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return URL::IMG["UNKNOWN"];
            }
        }

    }
?>

