<?php

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


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

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

        /**
         * Decoration name.
         */
        public $name;

        /**
         * Decoration description.
         */
        public $description;

        /**
         * Area of effect (if any).
         */
        public $area;

        /**
         * Affected stat (if any).
         */
        public $stat;

        /**
         * Affected element (if any).
         */
        public $element;

        /**
         * Max. level ().
         */
        public $max_level;

        /**
         * 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 SQLite3 $db Connection to the database.
         * @param int $id Decoration identifier.
         */
        public function __construct($db, $id){
            global $path;
            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 = $r["name"];
                $this->description = $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/content/decoration/ directory, and its name must be the monster 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/decoration/" . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png")){
                return $path["img"]["decoration"] . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return $path["img"]["root"] . "unknown.png";
            }
        }

    }
?>
