<?php

namespace duel;

use pocketmine\event\Listener;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\PluginTask;
use pocketmine\entity\Entity;

use duel\provider\YamlProvider;
use duel\utils\ArenaManager;
use duel\arena\EmptyArenaChooser;
use duel\command\BoxingCommand;
use duel\command\IpCommand;
use pocketmine\utils\Config;

class Loader extends PluginBase implements Listener {

    /** @var Loader */
    private static $instance;
public $MOD_top_module;
    /** @var null|YamlProvider */
    public $yamlProvider = null;

    /** @var null|ArenaManager */
    public $arenaManager = null;

    /** @var null|EmptyArenaChooser */
    public $emptyArenaChooser = null;

    public function onLoad()
    {
        self::$instance = $this;
        $this->saveResource('config.yml');
        $this->saveResource('status.yml');
        $this->saveResource('SanityIP.yml');
        $this->saveResource('Boxing_KB.yml');
    }

    public function onEnable()
    {
        $this->MOD_top_module = new Mod_TopModule($this);
        $pluginManager = $this->getServer()->getPluginManager();
        $plugin = $pluginManager->getPlugin("DueloBoxing");
        $versão = $plugin->getDescription()->getVersion();
        $this->getServer()->getPluginManager()->registerEvents($this, $this);
        $this->getLogger()->info('Made by Slxyer');
        $this->yamlProvider = new YamlProvider($this);
        $this->arenaManager = new ArenaManager($this);
        $this->emptyArenaChooser = new EmptyArenaChooser($this);
        foreach (scandir($this->getDataFolder().'arenas') as $file) {
			if ($file != '.' and $file != '..') {
			    $levelName = explode(".", $file)[0];
			    $this->getServer()->loadLevel($levelName);
			}
		}
        $this->getServer()->getCommandMap()->register('boxing:', new BoxingCommand($this));
        $this->getServer()->getCommandMap()->register('ip:', new IpCommand($this));
        $this->getServer()->getScheduler()->scheduleRepeatingTask(new class($this) extends PluginTask {
            /** @var Loader */
            private $loader;

            /**
             * @param Loader $loader
             */
            public function __construct(Loader $loader)
            {
                $this->loader = $loader;
                parent::__construct($loader);
            }

            /**
             * @param int $currentTick
             */
            public function onRun($currentTick)
            {
                foreach ($this->loader->arenaManager->arenas as $arena) {
                    $arena->tick();
                }
            }
        }, 20);

        $this->getServer()->getScheduler()->scheduleRepeatingTask(new class($this) extends PluginTask {
            /** @var Loader */
            private $loader;

            /**
             * @param Loader $loader
             */
            public function __construct(Loader $loader)
            {
                $this->loader = $loader;
                parent::__construct($loader);
            }

            /**
             * @param int $currentTick
             */
            public function onRun($currentTick)
            {
                foreach ($this->loader->arenaManager->arenas as $arena) {
                    $arena->msg();
                }
            }
        }, 2);
    }

    /**
     * @return Loader
     */
    public static function getInstance() : Loader
    {
        return self::$instance;
    }
}