<?php

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

    /**
     * Guild skill group.
     *
     * Represents an object from the table 'k_guild_skill_group'.
     */
    class K_Guild_Skill_Group extends Entity{

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

        /**
         * Group name.
         */
        public $name;

        /**
         * Group description.
         */
        public $description;
        
        /**
         * List of {@see K_Guild_Skill}s in the group.
         */
        public $skills = [];

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * skill group, populating it and it's items.
         *
         * @param SQLite3 $db Connection to the database.
         * @param int $id Skill group identifier.
         */
        public function __construct($db, $id){
            global $path;
            parent::__construct($db);
            $s =
              "SELECT " .
              "  id, " .
              "  name, " .
              "  description " .
              "FROM k_guild_skill_group " .
              "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"];
            }
            $s =
              "SELECT id " .
              "FROM k_guild_skill " .
              "WHERE skill_group = $this->id; ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->skills, new K_Guild_Skill($db, $r["id"]));
            }
        }

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

    }
?>
