<?php

namespace Juqn\Task;

use Juqn\Arena\Arena;
use Juqn\Loader;
use pocketmine\math\Vector3;
use pocketmine\scheduler\PluginTask;
use pocketmine\Server;
use Juqn\Mod_TopModule;

class GameTask extends PluginTask
{

    public $plugin;

    public function __construct(Loader $plugin)
    {
        parent::__construct($plugin);
        $this->plugin = $plugin;
    }

    public function getPlugin()
    {
        return $this->plugin;
    }

    public function onRun($currentTick)
    {
        $manager = $this->getPlugin()->getArenaManager();
        if (count($manager->arenas) <= 0) {
            return false;
        }
        foreach ($manager->arenas as $arena) {
            if ($arena instanceof Arena) {
                switch ($arena->getStatus()) {
                    case $arena::STATUS_WAITING:
                        if (count($arena->getPlayers()) >= 2) {
                            $arena->status = $arena::STATUS_STARTING;
                        }
                        foreach ($arena->getPlayers() as $player) {

                        }
                        break;
                    case $arena::STATUS_STARTING:
                        $arena->starting--;
                        if (count($arena->getPlayers()) <= 1) {
                            $arena->status = $arena::STATUS_WAITING;
                            $arena->starting = 30;
                        }
                        if ($arena->starting == 0) {
                            foreach ($arena->getPlayers() as $player) {
                                $arena->getKit($player);
                                $arena->startGame();
                            }
                            $arena->status = $arena::STATUS_RUN;
                        }
                        foreach ($arena->getPlayers() as $player) {
                            $player->sendPopup("§a" . $arena->starting . "");
                        }
                        break;
                    case $arena::STATUS_RUN:
                        if(count($arena->getPlayers()) == 1){
                            foreach ($arena->getPlayers() as $player){
                                //$this->getPlugin()->getServer()->broadcastMessage("§7×--------»§a MlgBlock §7«-------×");
                                $player->setFood(20);
                                $player->setHealth(20);
                                $player->removeAllEffects();
                                $player->getInventory()->clearAll();
                                $levels = $this->getPlugin()->getServer()->getPluginManager()->getPlugin("PLevel");
                                $ea = $this->getPlugin()->getServer()->getPluginManager()->getPlugin("EconomyAPI");
                                $levels->addXp($player, 5);
                                $this->plugin->MOD_top_module->onWin($player);
                                $wins = $this->plugin->getServer()->getPluginManager()->getPlugin("WinsSystem");

$wins->mlgWins($player);
                                $this->plugin->MOD_top_module->reloadData();
                                $ea->addMoney($player, 5); $player->teleport(Server::getInstance()->getDefaultLevel()->getSafeSpawn());
                            }
                            $arena->reset();
                        }
                        break;
                }
            }
        }
        return true;
    }
}