<?php
    /**
     * Area type entity file.
     *
     * Creates the entity and makes it available.
     *
     * @category Entity
     */

    /**
     * Require dependent entities if not present.
     */
    require_once(PATH::ENTITY . "Entity.php");


    /**
     * An area.
     *
     * Represents an object from the table 'k_area'.
     *
     * @category Entity
     */
    class K_Area extends Entity{

        /**
         * @var int Area identifier (it may not be unique!).
         */
        public $id;

        /**
         * @var int Area type.
         */
        public $type;

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

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * run, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Area identifier.
         * @param int $type Area type (required).
         */
        public function __construct($db, $id, $type){
            parent::__construct($db);
            $this->id = $id;
            $s = "
                SELECT
                  id,
                  type,
                  name
                FROM k_area
                WHERE id = '$id'
                ORDER BY type = '$type' DESC;
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->type = $r["type"];
                $this->name = HTML::e($r["name"]);
            }
            elseif($this->id > 1000){
                $this->type = $AREA_TYPE::CAIROS_DUNGEON;
                $this->name = "Hall of Heroes";
            }
        }

        /**
         * Gets the path to the area image. The image must be in the
         * img/area/ directory, and its name must be the area type id,
         * padded with '0' to 9 digites, and the extension must be '.png'.
         *
         * @param string $stage Stage number, for a better image in Cairos.
         * @return string Image URL, or a fixed unknown image.
         */
        public function get_image($stage = null){
            if ($this->id > 10000 && file_exists(PATH::BASE . "img/area/HOH.png")){
                // Hall of Heroes
                return URL::IMG["AREA"] . "HOH.png";
            }
            elseif (
              (
                $this->type == AREA_TYPE::SCENARIO ||
                $this->type == AREA_TYPE::RIFT_DUNGEON ||
                $this->type == AREA_TYPE::DIMENSIONAL_HOLE ||
                $this->type == AREA_TYPE::RIFT_RAID ||
                $this->type == AREA_TYPE::ARENA ||
                $this->type == AREA_TYPE::GUILD_WAR ||
                $this->type == AREA_TYPE::GUILD_SIEGE ||
                $this->type == AREA_TYPE::TARTARUS_LABYRINTH ||
                $this->type == AREA_TYPE::TRIAL_OF_ASCENSION ||
                $this->type == AREA_TYPE::WORLD_BOSS ||
                $this->type == AREA_TYPE::DIMENSIONAL_RIFT
              ) && file_exists(PATH::BASE . "img/area/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png")
            ){
                return URL::IMG["AREA"] . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png";
            }
            elseif ($this->type == AREA_TYPE::CAIROS_DUNGEON && in_array($this->id, $DUNGEON)){
                switch ($this->id){
                    case DUNGEON::GIANTS_KEEP:
                        switch ($stage){
                            case 1:
                            case 6:
                                // Light
                                if (file_exists(PATH::BASE . "img/unit/00060204.png")){
                                    return URL::IMG["UNIT"] . "00060204.png";
                                }
                                break;
                                break;
                            case 2:
                            case 7:
                                // Dark
                                if (file_exists(PATH::BASE . "img/unit/00060204.png")){
                                    return URL::IMG["UNIT"] . "00060204.png";
                                }
                                break;
                            case 3:
                            case 8:
                                // Wind
                                if (file_exists(PATH::BASE . "img/unit/00060203.png")){
                                    return URL::IMG["UNIT"] . "00060203.png";
                                }
                                break;
                            case 4:
                            case 9:
                                // Fire
                                if (file_exists(PATH::BASE . "img/unit/00060202.png")){
                                    return URL::IMG["UNIT"] . "00060202.png";
                                }
                                break;
                            case 5:
                            case 10:
                                // Water
                                if (file_exists(PATH::BASE . "img/unit/00060201.png")){
                                    return URL::IMG["UNIT"] . "00060201.png";
                                }
                                break;
                        }
                        break;
                    case DUNGEON::DRAGONS_LAIR:
                        switch ($stage){
                            case 1:
                            case 6:
                                // Wind
                                if (file_exists(PATH::BASE . "img/unit/00060303.png")){
                                    return URL::IMG["UNIT"] . "00060303.png";
                                }
                                break;
                                break;
                            case 2:
                            case 7:
                                // Light
                                if (file_exists(PATH::BASE . "img/unit/00060304.png")){
                                    return URL::IMG["UNIT"] . "00060304.png";
                                }
                                break;
                            case 3:
                            case 8:
                                // Water
                                if (file_exists(PATH::BASE . "img/unit/00060301.png")){
                                    return URL::IMG["UNIT"] . "00060301.png";
                                }
                                break;
                            case 4:
                            case 9:
                                // Dark
                                if (file_exists(PATH::BASE . "img/unit/00060305.png")){
                                    return URL::IMG["UNIT"] . "00060305.png";
                                }
                                break;
                            case 5:
                            case 10:
                                // Fire
                                if (file_exists(PATH::BASE . "img/unit/00060302.png")){
                                    return URL::IMG["UNIT"] . "00060302.png";
                                }
                                break;
                        }
                        break;
                    case DUNGEON::NECROPOLIS:
                        switch ($stage){
                            case 1:
                            case 6:
                                // Water
                                if (file_exists(PATH::BASE . "img/unit/000621301.png")){
                                    return URL::IMG["UNIT"] . "000621301.png";
                                }
                                break;
                                break;
                            case 2:
                            case 7:
                                // Fire
                                if (file_exists(PATH::BASE . "img/unit/000621302.png")){
                                    return URL::IMG["UNIT"] . "000621302.png";
                                }
                                break;
                            case 3:
                            case 8:
                                // Wind
                                if (file_exists(PATH::BASE . "img/unit/000621303.png")){
                                    return URL::IMG["UNIT"] . "000621303.png";
                                }
                                break;
                            case 4:
                            case 9:
                                // Light
                                if (file_exists(PATH::BASE . "img/unit/000621304.png")){
                                    return URL::IMG["UNIT"] . "000621304.png";
                                }
                                break;
                            case 5:
                            case 10:
                                // Dark
                                if (file_exists(PATH::BASE . "img/unit/000621305.png")){
                                    return URL::IMG["UNIT"] . "000621305.png";
                                }
                                break;
                        }
                        break;
                    case DUNGEON::HALL_OF_WATER:
                        if (file_exists(PATH::BASE . "img/unit/00060101.png")){
                            return URL::IMG["UNIT"] . "00060101.png";
                        }
                        break;
                    case DUNGEON::HALL_OF_FIRE:
                        if (file_exists(PATH::BASE . "img/unit/00060102.png")){
                            return URL::IMG["UNIT"] . "00060102.png";
                        }
                        break;
                    case DUNGEON::HALL_OF_WIND:
                        if (file_exists(PATH::BASE . "img/unit/00060103.png")){
                            return URL::IMG["UNIT"] . "00060103.png";
                        }
                        break;
                    case DUNGEON::HALL_OF_LIGHT:
                        if (file_exists(PATH::BASE . "img/unit/00060104.png")){
                            return URL::IMG["UNIT"] . "00060104.png";
                        }
                        break;
                    case DUNGEON::HALL_OF_DARK:
                        if (file_exists(PATH::BASE . "img/unit/00060105.png")){
                            return $PATH["IMG"]["UNIT"] . "00060105.png";
                        }
                        break;
                    case DUNGEON::HALL_OF_MAGIC:
                        if (file_exists(PATH::BASE . "img/unit/00062004.png")){
                            return URL::IMG["UNIT"] . "00062004.png";
                        }
                        break;
                    default:
                        if (file_exists(PATH::BASE . "img/area/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png")){
                            return URL::IMG["AREA"] . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png";
                        }
                }
            }
            return URL::IMG["UNKNOWN"];
        }

    }
?>

