<?php

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


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

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

        /**
         * Area type.
         */
        public $type;

        /**
         * Area name.
         */
        public $name;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * run, populating it and it's items.
         *
         * @param SQLite3 $db Connection to the database.
         * @param int $id Area identifier.
         * @param int $type Area type (required).
         */
        public function __construct($db, $id, $type){
            global $path;
            global $AREA_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 = $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 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($stage = null){
            global $base_dir;
            global $path;
            global $AREA_TYPE;
            global $DUNGEON;
            if ($this->id > 10000 && file_exists($base_dir . "img/area/HOH.png")){
                // Hall of Heroes
                return $path["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($base_dir . "img/area/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png")
            ){
                return $path["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($base_dir . "img/unit/00060204.png")){
                                    return $path["img"]["unit"] . "00060204.png";
                                }
                                break;
                                break;
                            case 2:
                            case 7:
                                // Dark
                                if (file_exists($base_dir . "img/unit/00060204.png")){
                                    return $path["img"]["unit"] . "00060204.png";
                                }
                                break;
                            case 3:
                            case 8:
                                // Wind
                                if (file_exists($base_dir . "img/unit/00060203.png")){
                                    return $path["img"]["unit"] . "00060203.png";
                                }
                                break;
                            case 4:
                            case 9:
                                // Fire
                                if (file_exists($base_dir . "img/unit/00060202.png")){
                                    return $path["img"]["unit"] . "00060202.png";
                                }
                                break;
                            case 5:
                            case 10:
                                // Water
                                if (file_exists($base_dir . "img/unit/00060201.png")){
                                    return $path["img"]["unit"] . "00060201.png";
                                }
                                break;
                        }
                        break;
                    case $DUNGEON["DRAGONS_LAIR"]:
                        switch ($stage){
                            case 1:
                            case 6:
                                // Wind
                                if (file_exists($base_dir . "img/unit/00060303.png")){
                                    return $path["img"]["unit"] . "00060303.png";
                                }
                                break;
                                break;
                            case 2:
                            case 7:
                                // Light
                                if (file_exists($base_dir . "img/unit/00060304.png")){
                                    return $path["img"]["unit"] . "00060304.png";
                                }
                                break;
                            case 3:
                            case 8:
                                // Water
                                if (file_exists($base_dir . "img/unit/00060301.png")){
                                    return $path["img"]["unit"] . "00060301.png";
                                }
                                break;
                            case 4:
                            case 9:
                                // Dark
                                if (file_exists($base_dir . "img/unit/00060305.png")){
                                    return $path["img"]["unit"] . "00060305.png";
                                }
                                break;
                            case 5:
                            case 10:
                                // Fire
                                if (file_exists($base_dir . "img/unit/00060302.png")){
                                    return $path["img"]["unit"] . "00060302.png";
                                }
                                break;
                        }
                        break;
                    case $DUNGEON["NECROPOLIS"]:
                        switch ($stage){
                            case 1:
                            case 6:
                                // Water
                                if (file_exists($base_dir . "img/unit/000621301.png")){
                                    return $path["img"]["unit"] . "000621301.png";
                                }
                                break;
                                break;
                            case 2:
                            case 7:
                                // Fire
                                if (file_exists($base_dir . "img/unit/000621302.png")){
                                    return $path["img"]["unit"] . "000621302.png";
                                }
                                break;
                            case 3:
                            case 8:
                                // Wind
                                if (file_exists($base_dir . "img/unit/000621303.png")){
                                    return $path["img"]["unit"] . "000621303.png";
                                }
                                break;
                            case 4:
                            case 9:
                                // Light
                                if (file_exists($base_dir . "img/unit/000621304.png")){
                                    return $path["img"]["unit"] . "000621304.png";
                                }
                                break;
                            case 5:
                            case 10:
                                // Dark
                                if (file_exists($base_dir . "img/unit/000621305.png")){
                                    return $path["img"]["unit"] . "000621305.png";
                                }
                                break;
                        }
                        break;
                    case $DUNGEON["HALL_OF_WATER"]:
                        if (file_exists($base_dir . "img/unit/00060101.png")){
                            return $path["img"]["unit"] . "00060101.png";
                        }
                        break;
                    case $DUNGEON["HALL_OF_FIRE"]:
                        if (file_exists($base_dir . "img/unit/00060102.png")){
                            return $path["img"]["unit"] . "00060102.png";
                        }
                        break;
                    case $DUNGEON["HALL_OF_WIND"]:
                        if (file_exists($base_dir . "img/unit/00060103.png")){
                            return $path["img"]["unit"] . "00060103.png";
                        }
                        break;
                    case $DUNGEON["HALL_OF_LIGHT"]:
                        if (file_exists($base_dir . "img/unit/00060104.png")){
                            return $path["img"]["unit"] . "00060104.png";
                        }
                        break;
                    case $DUNGEON["HALL_OF_DARK"]:
                        if (file_exists($base_dir . "img/unit/00060105.png")){
                            return $path["img"]["unit"] . "00060105.png";
                        }
                        break;
                    case $DUNGEON["HALL_OF_MAGIC"]:
                        if (file_exists($base_dir . "img/unit/00062004.png")){
                            return $path["img"]["unit"] . "00062004.png";
                        }
                        break;
                    default:
                        if (file_exists($base_dir . "img/area/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png")){
                            return $path["img"]["area"] . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png";
                        }
                }
            }
            return $path["img"]["root"] . "unknown.png";
        }

    }
?>
