package com.yaohun.demonskills.data;

import com.yaohun.demonskills.core.Skill;
import me.Demon.DemonPlugin.DemonAPI;
import me.Demon.DemonPlugin.data.NbtItem;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class PlayerData {

    private File file;
    private FileConfiguration configuration;
    private String name;
    private int magicNow;
    private int magicMax;
    private HashMap<String,Integer> skillLevelMap = new HashMap<>(); // 获取技能等级
    private HashMap<String,Double> cooldownMap = new HashMap<>();
    private HashMap<Integer,String> skillBindMap = new HashMap<>(); // 技能按键绑定
    private HashMap<Integer, ItemStack> keyBoardStackMap = new HashMap<>(); // 技能按键绑定物品

    public PlayerData(String name) {
        this.name = name;
        this.file = new File("plugins/DemonSkills/PlayerData", name + ".yml");
        if(!file.exists()) {
            this.file.getParentFile().mkdirs();
            this.configuration = YamlConfiguration.loadConfiguration(this.file);
        } else {
            this.configuration = YamlConfiguration.loadConfiguration(this.file);
        }
        this.magicMax = this.configuration.getInt("Magic",50);
        this.magicNow = this.magicMax;
        loadSkillsKeyBoard();
    }

    private void loadSkillsKeyBoard(){
        ConfigurationSection section = this.configuration.getConfigurationSection("KeyBoard");
        if(section == null) {
            return;
        }
        for (String key : section.getKeys(false)) {
            int keyBoard = Integer.parseInt(key);
            ItemStack stack = section.getItemStack(key);
            if(!DemonAPI.itemIsNull(stack) && !DemonAPI.itemIsLore(stack)){
                NbtItem nbtItem = new NbtItem(stack);
                if(nbtItem.hasKey("skillName")) {
                    String skillName = nbtItem.getString("skillName");
                    int skillLevel = nbtItem.getInteger("skillLevel");
                    skillBindMap.put(keyBoard, skillName);
                    skillLevelMap.put(skillName, skillLevel);
                    keyBoardStackMap.put(keyBoard, stack);
                    System.out.println("#[Load] skillName: "+key+"  level: "+skillName+" keyBoard: "+keyBoard);
                }
            }
        }
    }

    public void SavePlayerData(){
        this.configuration.set("Magic",this.magicMax);
        this.configuration.set("KeyBoard",null);
        for (Integer keyBoard : keyBoardStackMap.keySet()) {
            ItemStack keyBoardStack = keyBoardStackMap.get(keyBoard);
            this.configuration.set("KeyBoard."+keyBoard,keyBoardStack);
        }
        try {
            this.configuration.save(this.file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String getPlayerName() {
        return name;
    }

    public int getMagicNow() {
        return magicNow;
    }
    public boolean hasMagic(int amount) {
        if(this.magicNow >= amount) {
            return true;
        }
        return false;
    }

    public void takeMagicNow(int amount) {
        magicNow -= amount;
    }

    public void setMagicNow(int magicNow) {
        this.magicNow = magicNow;
    }

    public int getMagicMax() {
        return magicMax;
    }

    public void resetSkillDataMap(){
        skillLevelMap = new HashMap<>();
        keyBoardStackMap = new HashMap<>();
    }

    public int getSkillLevel(String skill){
        if(this.skillLevelMap.containsKey(skill)) {
            return this.skillLevelMap.get(skill);
        }
        return 1;
    }

    public void setSkillLevel(String skillName, int level) {
        this.skillLevelMap.put(skillName, level);
    }

    public void setSkillBindMap(int keyBoard,String skillName){
        skillBindMap.put(keyBoard,skillName);
        if(skillName.equalsIgnoreCase("default")){
            keyBoardStackMap.remove(keyBoard);
        }
    }

    public String getSkillNameBind(int keyBoard){
        if(skillBindMap.containsKey(keyBoard)) {
            return skillBindMap.get(keyBoard);
        }
        return "default";
    }

    public void setKeyBoardStackMap(int keyBoard, ItemStack keyBoardStack){
        keyBoardStackMap.put(keyBoard,keyBoardStack);
    }

    public HashMap<Integer, ItemStack> getKeyBoardStackMap() {
        return keyBoardStackMap;
    }

    public void setCooldownData(String skillName, double cooldown){
        this.cooldownMap.put(skillName,cooldown);
    }

    public double getCooldownData(String skillName){
        if(this.cooldownMap.containsKey(skillName)) {
            return this.cooldownMap.get(skillName);
        }
        return 60.0;
    }
}
