<?php

namespace Robert\Sumo\Arena;

use pocketmine\level\Position;
use pocketmine\Server;
use pocketmine\entity\Effect;
use pocketmine\item\Item;
use Robert\Sumo\Mod_TopModule;
use Robert\Sumo\Arena\Arena;
use Robert\Sumo\Main;

class ArenaManager
{
    private $base;
    private $inGame = [];

    /** @var array<string, Arena> */
    private $arenas = [
        // "arena name" => Arena object
    ];

    public function __construct(Main $base)
    {
        $this->base = $base;
    }

    public function registerArena($name, $spawn1, $spawn2)
    {
        Server::getInstance()->loadLevel($name);
        $this->arenas[$name] = new Arena($name, $spawn1, $spawn2);
    }
    public function getArenas()
    {
        return $this->arenas;
    }
    public function unregisterArena($name)
    {
        unset($arenas[$name]);
    }

    public function isInGame(string $name)
    {
        return isset($this->inGame[$name]);
    }

    public function joinGame(string $name)
    {
        $p = Server::getInstance()->getPlayerExact($name);
        if ($p != null) {
            $arena = $this->getRandomArena();
 $p->addEffect(Effect::getEffect(11)->setAmplifier(50)->setDuration(99999999)); 
            $this->inGame[$name] = $arena;
            if ($arena != null) {
                if($arena->hasStarted()) {
                    return;
                }
                $this->base->getServer()->getLogger()->info($arena->getName());
                $level = Server::getInstance()->getLevelByName($arena->getName());
                if($level == null) return;
                $arena->registerPlayer($p->getName());
                $p->teleport($level->getSafeSpawn());
                $p->getInventory()->clearAll();
        $p->sendTip("");
                $bed = Item::get(Item::BED, 0, 1);
                $bed->setCustomName("§r§cLeave Game");
                $p->getInventory()->setItem(8, $bed);
                if ($arena->getPlayerCount() < 1) {
                    $p->teleport(new Position($arena->getSpawn1()['x'], $arena->getSpawn1()['y'], $arena->getSpawn1()['z'], $level));
                }
                if ($arena->getPlayerCount() >= 1 && $arena->getPlayerCount() < 2) {
                    $p->teleport(new Position($arena->getSpawn2()['x'], $arena->getSpawn2()['y'], $arena->getSpawn2()['z'], $level));
                }
            } else {
                $p->sendMessage("§8(§eSumo§8) §cAll arenas are full.");
            }
        }
    }

    public function leaveGame(string $name)
    {
        $p = Server::getInstance()->getPlayerExact($name);
        if ($p !== null) {
            $p->getInventory()->clearAll();
            $p->sendTip("");
        }
        $p = Server::getInstance()->getPlayerExact($name);
        if ($p !== null) { $p->getInventory()->clearAll(); }
        $p->sendTip("");
        $arena = $this->getArenaByPlayer($name);
        if($arena->getPlayerCount() == 2) {

            if(!$arena->hasStarted()) {
                $arena->unregisterPlayer($name);
                unset($this->inGame[$name]);
                return;
            }


            $winner = "";
            foreach($arena->getPlayers() as $n) {
                if($n !== $name) {
                    $winner = $n;
                }
            }
            Server::getInstance()->broadcastMessage("§8(§eSumo§8) §e" . $winner . " §fwon the game!");
            $w = Server::getInstance()->getPlayerExact($winner);
            if ($w !== null) {
                $w->getInventory()->clearAll();
                $w->teleport(Server::getInstance()->getDefaultLevel()->getSafeSpawn());
            }
            

                $p->teleport(Server::getInstance()->getDefaultLevel()->getSafeSpawn());
            $w->removeAllEffects();
            $this->base->MOD_top_module->onWin($w);
            $this->base->MOD_top_module->reloadData();
            
            foreach($arena->getPlayers() as $n) {
                $arena->unregisterPlayer($n);
                unset($this->inGame[$n]);
            }
        }
$p->removeAllEffects();
        if($arena->getPlayerCount() == 1) {
            $arena->unregisterPlayer($name);
            unset($this->inGame[$name]);
        }
    }
    public function getRandomArena()
    {
        foreach ($this->arenas as $name => $arena) {
            $a = $this->arenas[$name];
            if (!$a->hasStarted() && $a->getPlayerCount() < 2) {
                return $a;
            }
        }
        return null;
    }
    public function getArenaByName(string $arena): Arena
    {
        return $this->arenas[$arena];
    }
    public function getArenaByPlayer(string $name): Arena
    {
        return $this->inGame[$name];
    }

    public function getPlayers()
    {
        return $this->inGame;
    }
}
