<?php

namespace Robert\Sumo\Tasks;

use pocketmine\scheduler\Task;
use pocketmine\Server;
use pocketmine\item\Item;
use pocketmine\item\enchantment\Enchantment;
use Robert\Sumo\Main;
use pocketmine\entity\Effect;
class CountdownTask extends Task
{
    private $base;
    private $am;
    private $arenaSeconds = [
        // arena name => arena countdown seconds
    ];
    public function __construct(Main $base)
    {
        $this->base = $base;
    }
    public function onRun($umf)
    {
        $arenas = $this->base->getHornyArenaManager()->getArenas();
        foreach ($arenas as $n => $arena) {
            if ($arena->hasStarted() && $arena->getPlayerCount() == 2) {
                $players = $arena->getPlayers();
                $pNames = array_keys($players);
                $p1 = Server::getInstance()->getPlayerExact($pNames[0]);
                $p2 = Server::getInstance()->getPlayerExact($pNames[1]);
                if($p1 !== null && $p2 !== null) {
                    $p1->sendTip("§aYou: " . $p1->getName() . " §7(" . $p1->getPing() . "ms)
§cOpponent: " . $p2->getName() . " §7(" . $p2->getPing() . "ms)");
                    $p2->sendTip("§aYou: " . $p2->getName() . " §7(" . $p2->getPing() . "ms)
§cOpponent: " . $p1->getName() . " §7(" . $p1->getPing() . "ms)");
                }
            }
            if ($arena->getPlayerCount() <= 1) {
                if (isset($this->arenaSeconds[$n])) { unset($this->arenaSeconds[$n]); }

                if($arena->hasStarted()) { $arena->setStarted(false); }

                $arena->broadcastTip("§cWaiting for players...");
            } else {
                if (!isset($this->arenaSeconds[$n])) {
                    if($arena->getPlayerCount() !== 2) {
                        return;
                    }

                    if($arena->hasStarted()) {
                        return;
                    }
                    $this->startCountdown($n);
                    return;
                }

                if ($this->arenaSeconds[$n] > 0) {
                    $arena->broadcastTip("§bThe game is starting in §e" . $this->arenaSeconds[$n] . "§b seconds.");
                    foreach($arena->getPlayers() as $pName) {
                        $p = Server::getInstance()->getPlayerExact($pName);
                        if($p !== null) $p->getLevel()->addSound(new \pocketmine\level\sound\ClickSound($p));
                    }
                    $this->arenaSeconds[$n] = $this->arenaSeconds[$n] - 1;
                } else {
                    $arena->broadcastMessage("§8(§eSumo§8) §fThe game has started. §eGood luck!");
                    $arena->setStarted(true);
                    foreach($arena->getPlayers() as $pName) {
                        $p = Server::getInstance()->getPlayerExact($pName);
                        if($p !== null) {
                            $p->getInventory()->clearAll();
                            $stick = Item::get(Item::STICK);
                            /*$enchantment = Enchantment::getEnchantment(Enchantment::KNOCKBACK);
                            $enchantment->setLevel(2);
                            $stick->addEnchantment($enchantment);*/
                            $stick->setCustomName("§r§6Knockback Stick");
                            $p->getInventory()->addItem($stick);
                            // Add Bed after clearing inventory
                            $bed = Item::get(Item::BED, 0, 1);
                            $bed->setCustomName("§r§cLeave Game");
                            $p->getInventory()->setItem(8, $bed);
                            $p->getLevel()->addSound(new \pocketmine\level\sound\AnvilFallSound($p));
                            $p->addEffect(new Effect(Effect::DAMAGE_RESISTANCE, "", 0, 0, 0, false));
                        }
                    }
                    unset($this->arenaSeconds[$n]);
                }
            }
        }
    }
    public function startCountdown($arenaname)
    {
        $this->arenaSeconds[$arenaname] = $this->base->getConfig()->getNested("countdown-time", 5);
    }
}
