package com.yaohun.demonskills.data;

import com.yaohun.demonskills.core.Skill;
import com.yaohun.demonskills.manage.SkillManager;
import com.yaohun.demonskills.util.RandomUtil;
import me.Demon.DemonPlugin.data.NbtItem;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

public class SkillData {

    private String skillName;
    private String itemName;
    private List<String> itemLore;
    private String itemNbt;
    private ItemStack itemStack;

    public SkillData(String skillName, ConfigurationSection section) {
        this.skillName = skillName;
        this.itemNbt = section.getString(skillName+".nbt","default");
        String type = section.getString(skillName+".type");
        this.itemName = section.getString(skillName+".name");
        this.itemLore = section.getStringList(skillName+".lore");
        this.itemStack = new ItemStack(Material.valueOf(type),1);
    }

    public ItemStack getItemStack(String playerName, int skillLevel) {
        ItemStack stack = itemStack.clone();
        ItemMeta meta = stack.getItemMeta();
        Skill skill = SkillManager.getGuiSkillStack(skillName,skillLevel);
        int needMagic = 10;
        double cooldown = 60.0;
        if(skill != null){
            needMagic = skill.needMagic;
            cooldown = skill.cooldownTime;
        }
        String levelName = "I";
        if (skillLevel == 2) {
            levelName = "II";
        } else if (skillLevel == 3) {
            levelName = "III";
        } else if (skillLevel == 4) {
            levelName = "IV";
        } else if (skillLevel == 5) {
            levelName = "V";
        } else if (skillLevel == 6) {
            levelName = "VI";
        } else if (skillLevel == 7) {
            levelName = "VII";
        } else if (skillLevel == 8) {
            levelName = "VIII";
        } else if (skillLevel == 9) {
            levelName = "IX";
        } else if (skillLevel == 10) {
            levelName = "X";
        }
        String itemName = this.itemName.replace("{level}", levelName);
        List<String> lore = new ArrayList<>();
        for (int i = 0; i < itemLore.size(); i++) {
            String s = itemLore.get(i);
            if(s.contains("{magic}")){
                s = s.replace("{magic}",String.valueOf(needMagic));
            }
            if(s.contains("{player}")){
                s = s.replace("{player}",playerName);
            }
            if(s.contains("{cooldown}")){
                s = s.replace("{cooldown}",String.format("%.1f",cooldown));
            }
            lore.add(s);
        }
        meta.setDisplayName(itemName);
        meta.setLore(lore);
        stack.setItemMeta(meta);
        NbtItem nbtItem = new NbtItem(stack);
        nbtItem.setString("skillOwner",playerName);
        nbtItem.setString("skillName",skillName);
        nbtItem.setInteger("skillLevel",skillLevel);
        nbtItem.setInteger("skillNumber", RandomUtil.getRandomInt(1000,9999));
        if(!itemNbt.equalsIgnoreCase("default")) {
            String[] split = itemNbt.split("#");
            String key = split[0];
            String value = split[1];
            nbtItem.setString(key,value);
        }
        return nbtItem.getItem();
    }
}
