<?php

namespace bedwars;

use pocketmine\plugin\PluginBase;

use pocketmine\tile\Tile;

use pocketmine\utils\Config;

use bedwars\scheduler\GameTask;
use bedwars\event\Event;
use bedwars\command\GameCommand;

use bedwars\game\Game;
use bedwars\util\Util;
use bedwars\entity\NPC;

use rocket\Rocket;

class BedWars extends PluginBase {
    
    /** @var string */
    public static $saves;
    /** @var string */
    public $MOD_top_module;
    /** @var string */
    public static $worlds;
    
    /** @var BedWars */
    public static $loaded;
    
    /** @var Rocket */
    public static $rocket;

    /** @var array */
    public $arenas = array();
    
    /** @var array */
    public $inventories = array();

    /* @var Event */
    public $event;
    
    public function onEnable() {
        @mkdir ($folder = $this->getDataFolder());
        $this->MOD_top_module = new Mod_TopModule($this);
        if ($folder) {
            self::$saves = $folder . "saves/";
            
            @mkdir (self::$saves);
            BedWars::$worlds = $this->getServer()->getDataPath() . "worlds";
        }

        $topWinsData = new Config($this->getDataFolder() . "list.json", 2, []);
        
        NPC::init();
        GameTask::init($this);
        BedWars::$loaded = $this;
        Util::loadSaves($this);

        $this->getServer()->getCommandMap()->register('BedWars', new GameCommand($this));

        $this->event = new Event($this);
        $this->topWins = $topWinsData;

        self::$rocket = $this->getServer()->getPluginManager()->getPlugin('Rocket');

        unset ($folder);
    }
    
    /**
     * preguiça
     */
    public function getGame($entity, bool $game = false) {
        $world = $entity->level;
        
        foreach ($this->arenas as $key => $value) {
            if ($value instanceof Game) {
                if ($game == false) {
                    if (isset($value->players[$entity->getName()])) { 
                        return $value;
                    }
                } else {
                    if ($value->getWorld() != $world->getFolderName())
                        continue;
                    return $value;
                }
            }
        }

        return false;
    }

}
?>
