<?php

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


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

        /**
         * Owner identifier.
         */
        public $uid;

        /**
         * Item identifier. If its a monster piece, a {@see K_Monster} entity.
         */
        public $id;

        /**
         * Item name.
         */
        public $name;

        /**
         * Item type name.
         */
        public $type;

        /**
         * Item type name.
         */
        public $ammount;

        /**
         * 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 SQLite3 $db Connection to the database.
         * @param int $id Building identifier.
         */
        public function __construct($db, $id){
            global $path;
            global $INVENTORY_TYPE;
            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_type
                LEFT JOIN k_inventory ON
                   k_inventory.id = inventory.id
                WHERE
                  inventory.type = k_inventory_type.id AND
                  inventory.id = $id;
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            $this->type = $r["type"];
            $this->amount = $r["amount"];
            if ($r["type"] == $INVENTORY_TYPE["GUILD_MONSTER_PIECE"] || $r["type"] == $INVENTORY_TYPE["MONSTER_PIECE"]){
                $this->is_monster_pices = true;
                $this->id = new K_Unit($this->db, $r["id"]);
                $this->name = $this->id->title . " Pieces";
                $this->description = $this->id->title . " Pieces";
            }
            else{
                $this->is_monster_pices = false;
                $this->id = $r["id"];
                $this->name = $r["name"];
                $this->description = $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 monster id,
         * padded with '0' to 9 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/inventory/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png")){
                return $path["img"]["inventory"] . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return $path["img"]["root"] . "unknown.png";
            }
        }
    }
?>
