<?php

namespace duel\command\subcommand;

use pocketmine\command\CommandSender;

use duel\command\BoxingCommand;
use duel\command\SubCommand;
use pocketmine\utils\Config;
use duel\Loader;

class Criar extends SubCommand
{
	
	/** @var DuelCommand */
	private $command;
	
	private $config;
	
	/**
	  * CreateSubCommand constructor.
	  * @param DuelCommand $command
	  */
	public function __construct(BoxingCommand $command)
	{
		$this->command = $command;
        $this->config = new Config(Loader::getInstance()->getDataFolder() . 'config.yml', Config::YAML);
	}
	
	/**
	  * @param CommandSender $sender
	  * @param array $args
	  */
	public function execute(CommandSender $sender, array $args)
	{
		if (count($args) < 2) {
			 $sender->sendMessage('§fUse §r'.$this->config->get('Cor_Eventos_Arena').'/boxing criar <nome> §fpara criar um boxing.');
			 return false;
		}
		if (empty($this->command->data[$sender->getName()])) {
			 $sender->sendMessage('§fUse §r'.$this->config->get('Cor_Eventos_Arena').'/boxing setar <lobby|pos1|pos2> §fpara setar as posições.');
			 return false;
		}
		/** @var array */
		$data = $this->command->data[$sender->getName()];
		
		if (empty($data['lobby']) or empty($data['pos1']) or empty($data['pos2'])) {
			 $sender->sendMessage('§cYou have not set the locations!');
			 return false;
		}
		$manager = $this->command->loader->arenaManager;
		
		if ($manager->existsArena($args[1])) {
			 $sender->sendMessage('§cTheres already a boxing ring with that name!');
			 return false;
		}
		$manager->createArena(array_merge($data, ['name' => $args[1]]));
		unset($this->command->data[$sender->getName()]);
		$sender->sendMessage('§fO Boxing'.$this->config->get('Cor_Eventos_Arena').' '.$args[1].' §fFoi Criado.');
		return true;
	}
}