package com.sakurarealm.playerrisk;

import com.palmergames.bukkit.towny.event.PlayerEnterTownEvent;
import com.palmergames.bukkit.towny.event.PlayerLeaveTownEvent;
import com.sakurarealm.playerrisk.api.PlayerRiskManager;
import com.sakurarealm.playerrisk.api.PlayerRiskSettings;
import org.apache.commons.lang.NotImplementedException;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.*;

public final class PlayerRisk extends JavaPlugin implements Listener {

    //用来记录在城镇之外的玩家
    Map<String,Integer> playsOutTown = new Hashtable<>();
    PlayerRiskManager playerRiskManager = null;

//    public PlayerRiskManager getPlayerRiskManager() {
//        if (playerRiskManager == null) {
//            throw new NullPointerException("PlayerRisk plugin is not initialized.");
//        }
//        return playerRiskManager;
//    }

    public void load() {
        saveDefaultConfig();
        FileConfiguration config = getConfig();
        ConfigurationSection settingsSection = config.getConfigurationSection("settings");
        playerRiskManager = new PlayerRiskManager(this, new PlayerRiskSettings(settingsSection));
    }

    @Override
    public void onEnable() {
        // Plugin startup logic
        load();
        getCommand("playerrisk").setExecutor(new CommandHandler(this));
        //注册监听器
        Bukkit.getPluginManager().registerEvents(this,this);

        getServer().getScheduler().scheduleSyncRepeatingTask(this, this::addRiskToOutTownPlayers, 0, 1200);
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }


    /**
     * TODO: 当玩家进入城镇时, 危险度不再增加.
     *
     * @param event The PlayerEnterTownEvent.
     */
    @EventHandler
    public void onPlayerEnterTown(PlayerEnterTownEvent event) {
        //当玩家进入城镇后，移出map集合并且清零
        Player player = event.getPlayer();
        playsOutTown.remove(player.getName());
        throw new NotImplementedException("Not implemented yet.");
    }

    /**
     * TODO: 当玩家离开城镇时, 危险度开始增加.
     *
     * @param event The PlayerLeaveTownEvent.
     */
    @EventHandler
    public void onPlayerLeaveTown(PlayerLeaveTownEvent event) {
        //当玩家离开城镇后,移出map集合并且清零
        Player player = event.getPlayer();
        playsOutTown.put(player.getName(),0);

        throw new NotImplementedException("Not implemented yet.");
    }

    /**
     * TODO: 当玩家死亡时, 危险度重置.
     *
     * @param event The PlayerDeathEvent.
     */
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        Player player = event.getEntity();
        playsOutTown.remove(player.getName());
        throw new NotImplementedException("Not implemented yet.");
    }
    // TODO: Add any other event handlers needed to manage player risk.

    //注册主任务,一直循环给增加在城镇之外的玩家的危险度
    public void addRiskToOutTownPlayers(){
        for (String name : playsOutTown.keySet()){
            //获取玩家当前的危险度
            int playerRiskLevel = playerRiskManager.getPlayerRiskLevel(name);
            //日志报告，测试用
            getLogger().info(ChatColor.GREEN+name+"危险度为:"+playerRiskLevel);
            //注意此处是把所有生物群系的危险度放在一起计算了,
            if (playerRiskLevel < playerRiskManager.settings.getLevelMax()){
                playerRiskManager.setPlayerRiskLevel(name,playerRiskLevel + 1);
            }
        }
    }
}
