<?php

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


    /**
     * Monster source.
     *
     * Represents an object from the table 'k_source'.
     */
    class K_Source extends Entity{

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

        /**
         * Source name.
         */
        public $name;

        /**
         * Source description.
         */
        public $description;

        /**
         * Indicates if the source is farmable.
         */
        public $farmable;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * monster, populating it and it's items.
         *
         * @param SQLite3 $db Connection to the database.
         * @param int $id Monster identifier.
         */
        public function __construct($db, $id){
            global $path;
            parent::__construct($db);
            $s =
              "SELECT " .
              "  id, " .
              "  name, " .
              "  description, " .
              "  farmable " .
              "FROM k_source " .
              "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->farmable_source = $r["farmable"];
            }
        }

        /**
         * Gets the path to the source image. The image must be in the
         * img/source/ directory, and it's name must be the source id,
         * padded with '0' to 4 digits, 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/source/" . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png")){
                return $path["img"]["source"] . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return $path["img"]["root"] . "unknown.png";
            }
        }

    }
?>
