<?php

namespace bedwars\util;

use pocketmine\level\Level;
use pocketmine\level\format\generic\BaseLevelProvider;

use pocketmine\nbt\tag\StringTag;

use pocketmine\block\Block;

use bedwars\BedWars;
use bedwars\game\Game;

class Util {
    
    /**
     * @param Level $level
     * @return bool
     */
    public static function is_buggy_map(Level $level) : bool {
        $levelName = (string) $level->getName();
        $folderName = (string) $level->getName();
        if ($levelName == $folderName) {
        	return false;
        }
        
        $provider = $level->getProvider();
        
        if ($provider instanceof BaseLevelProvider) {
            $provider->getLevelData()->LevelName = new StringTag('LevelName', $folderName);
            $provider->saveLevelData();
        }
        return true;
    }

    /**
    * @param BedWars $plugin
    */
    public static function loadSaves(BedWars $plugin) {
        foreach (scandir(BedWars::$saves) as $value) {
            if ($value != "." and $value != "..") {
                $file = BedWars::$saves . $value . "/" . $value . ".js"; 
                
                if (!is_file($file)) {
                    continue;
                }
                
                $args = yaml_parse_file($file); 
                
                if (!isset($args['name'], $args['mode'], $args['world'], $args['teams'])) {
                    continue;
                } 
                
                $game = new Game();
                $game->setName($args['name']);
                $game->setMode($args['mode']);
                $game->setWorld($args['world']);
                $game->setDefaultTeams($args['teams']);
                $game->setGeneretors($args['generetors']);
                $game->setHub((array) $args['hub']);

                $game->backupMap();
                $game->reset();
                
                $plugin->arenas[$value] = $game;
                $plugin->getLogger()->info($value . " is loaded");
            }
        }
        
    }
}
?>