<?php
    /**
     * Inventory 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_Unit.php");

    /**
     * An inventory item.
     *
     * Represents an object from the table 'inventory'.
     *
     * @category Entity 
     */
    class Inventory extends Entity{

        /**
         * @var int Owner identifier.
         */
        public $uid;

        /**
         * @var int|K_Unit Item identifier. If its a monster piece, a K_Unit entity.
         */
        public $id;

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

        /**
         * @var string Item type name.
         */
        public $type;

        /**
         * @var int Item owned amount.
         */
        public $amount;

        /**
         * @var bool Indicates if the item are monster pieces.
         */
        public $is_monster_pices;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * building, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Item identifier.
         * @param int $type Item type identifier. Optional, will try to guess if null.
         */
        public function __construct($db, $id, $type = null){
            parent::__construct($db);
            $s = "
                SELECT
                  inventory.id AS id,
                  k_inventory.name AS name,
                  k_inventory.description AS description,
                  inventory.type AS type_ref,
                  k_inventory_type.name AS type,
                  inventory.amount AS amount
                FROM
                  inventory,
                  k_inventory,
                  k_inventory_type
                WHERE
                  inventory.type = k_inventory_type.id AND
                  inventory.id = k_inventory.id AND
                  inventory.type = k_inventory.type AND
                  inventory.id = $id
            ";
            if ($type != null){
                $s .= "AND inventory.type = $type";
            }
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            $this->type = $r["type"];
            $this->amount = $r["amount"];
            if ($r["type"] == INVENTORY_TYPE_ID::GUILD_MONSTER_PIECE || $r["type"] == INVENTORY_TYPE_ID::MONSTER_PIECE){
                $this->is_monster_pices = true;
                $this->id = new K_Unit($this->db, $r["id"]);
                $this->name = HTML::e($this->id->title) . " Pieces";
                $this->description = HTML::e($this->id->title) . " Pieces";
            }
            else{
                $this->is_monster_pices = false;
                $this->id = $r["id"];
                $this->name = HTML::e($r["name"]);
                $this->description = HTML::e($r["description"]);
            }
        }

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

