<?php

namespace Robert\Sumo\Commands;

use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use Robert\Sumo\Main;

class SumoCommand extends Command
{
    /** @param Main */
    private $base;
    public function __construct(Main $base)
    {
        $this->base = $base;
        return parent::__construct("sumo", "Sumo command", "Usage: /sumo join|quit", []);
    }

    public function execute(CommandSender $sender, $commandLabel, array $args)
    {
        if ($this->getName() == "sumo") {
            if (!$sender instanceof Player) {
                return;
            }
            /** @param Player */
            $p = $sender;
            if (count($args) <= 0) {
                $p->sendMessage($this->usageMessage);
            }
            switch ($args[0]) {
                case "admin":
                    if (!isset($args[1])) {
                        $p->sendMessage("Usage: /sumo admin spawn1|spawn2");
                        return;
                    }
                case "spawn1":
                    $p->sendMessage("§8(§eSumo§8) §fSpawn 1 is now horny");
                    $level = $p->getLevel()->getName();
                    $x = $p->getX();
                    $y = $p->getY();
                    $z = $p->getZ();
                    $this->base->getConfig()->setNested("arenas.$level.spawn1.x", $x);
                    $this->base->getConfig()->save();
                    $this->base->getConfig()->setNested("arenas.$level.spawn1.y", $y);
                    $this->base->getConfig()->save();
                    $this->base->getConfig()->setNested("arenas.$level.spawn1.z", $z);
                    $this->base->getConfig()->save();
                    break;
                case "spawn2":
                    $p->sendMessage("§8(§eSumo§8) §fSpawn 2 is now horny");
                    $level = $p->getLevel()->getName();
                    $x = $p->getX();
                    $y = $p->getY();
                    $z = $p->getZ();
                    $this->base->getConfig()->setNested("arenas.$level.spawn2.x", $x);
                    $this->base->getConfig()->save();
                    $this->base->getConfig()->setNested("arenas.$level.spawn2.y", $y);
                    $this->base->getConfig()->save();
                    $this->base->getConfig()->setNested("arenas.$level.spawn2.z", $z);
                    $this->base->getConfig()->save();
                    break;
                case "join":
                    if($this->base->getHornyArenaManager()->isInGame($p->getName())) {
                        $p->sendMessage("§8(§eSumo§8) §cYou are already in a game.");
                        return;
                    }
                    $this->base->getHornyArenaManager()->joinGame($p->getName());
                    break;
                case "quit":
                    if(!$this->base->getHornyArenaManager()->isInGame($p->getName())) {
                        $p->sendMessage("§8(§eSumo§8) §cYou are not in a game.");
                        return;
                    }
                    $p->sendMessage("§8(§eSumo§8) §fReturning you to the lobby...");
                    $this->base->getHornyArenaManager()->leaveGame($p->getName());
                    break;
            }
        }
    }
}
