<?php

/**
 * Plugin create by JuqnGOOD
 * Plugin concluído em 09/04/20 17:27 [Edits: null]
 * Teste de plugin 09/04/20 18:30 [Erros: (09/04/20 18:30 0 erros)]
 */

namespace Juqn;

use Juqn\Commands\MLGCommand;
use Juqn\Arena\ArenaManager;
use Juqn\Task\GameTask;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;

class Loader extends PluginBase {
	
	public $managerArena;
	public $MOD_top_module;
	public function onEnable(){
		//$this->getLogger()->info("Plugin carregado com sucesso");
		$this->MOD_top_module = new Mod_TopModule($this);
		$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
		$this->getServer()->getScheduler()->scheduleRepeatingTask(new GameTask($this), 25);
		$this->getServer()->getCommandMap()->register("MLGBlock", new MLGCommand($this));
		
		@mkdir($this->getDataFolder());
		@mkdir($this->getDataFolder()."Arenas");
		@mkdir($this->getDataFolder()."Maps");
		
		$this->managerArena = new ArenaManager($this);
	}
	
	public function onDisable(){
		$manager = $this->getArenaManager();
		foreach($manager->getArenas() as $class){
			$this->getLogger()->notice("Arena ".$class->getName()." fechada com ".count($class->getPlayers())." jogadores em jogo");
			foreach($class->getPlayers() as $player){
				$class->quit($player);
			}
		}
	}
	
	public function getArenaManager() : ArenaManager {
		return $this->managerArena;
	}
	
	public function onCommand(CommandSender $sender, Command $command,  $label, array $args){
		$manager = $this->getArenaManager();
		if($sender instanceof Player){
			if($command->getName() == "mlg"){
				if(isset($args[0])){
					switch(strtolower($args[0])){
						case 'entrar':
							if(isset($manager->sessions[$sender->getName()])){
								$sender->sendMessage("§cВы создаете арену, не может участвовать в игре!");
								return true;
							}
							if(!$manager->inArena($sender)){
								if($manager->joinArena($sender)) {
                                } else {
								    $sender->sendMessage("§c» §fВсе карты заняты!");
                                }
							} else {
								$sender->sendMessage("§c» §fAll maps are occupied!");
							}
							break;
						case 'quit':
							if($manager->inArena($sender)){
								$arena = $manager->inArena($sender);
								$arena->quit($sender);
								foreach($arena->getPlayers() as $player){
									$player->sendMessage("§a» §fИгрок §e{$sender->getName()} §fвышел из игры! §7[".count($arena->getPlayers())."/{$arena->getSlots()}]");
								}
							} else {
								$sender->sendMessage("§cВы не в игре");
							}
							break;
					}
				} else {
				}
			}
		} else {
			$sender->sendMessage("§cИспользуйте команду в игре");
		}
	}
}