package com.yaohun.demonskills;

import com.yaohun.demonskills.data.PlayerData;
import com.yaohun.demonskills.manage.PlayerManager;
import com.yaohun.demonskills.util.CDTimeAPI;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.UUID;

/**
 * @Author: Baka
 * @Date: 2019/12/23 13:20
 */
public class PapiExpansion extends PlaceholderExpansion {
    private Plugin plugin;

    public PapiExpansion(Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean persist() {
        return true;
    }

    @Override
    public boolean canRegister() {
        return true;
    }

    @Override
    public String getAuthor() {
        return plugin.getDescription().getAuthors().toString();
    }

    @Override
    public String getIdentifier() {
        return "auskill";
    }

    @Override
    public String getVersion() {
        return plugin.getDescription().getVersion();
    }

    @Override
    public String onPlaceholderRequest(Player player, String identifier) {
        UUID uuid = player.getUniqueId();
        PlayerManager playerManager = SkillsMain.getPlayerManager();
        if (identifier.contains("_")) {
            PlayerData playerData = playerManager.getPlayerData(player.getName());
            String[] sp = identifier.split("_");
            return getSkillCooldow(uuid,playerData,sp);
        }
        return "";
    }

    private String getSkillCooldow(UUID uuid,PlayerData playerData,String[] sp){
        String papiType = sp[0];
        String skillKey = sp[1];
        if(papiType.equalsIgnoreCase("cd")){
            if(skillKey.equalsIgnoreCase("翻滚")){
                String cooldownKey = "skill_翻滚";
                double time = (double) CDTimeAPI.getCD(uuid, cooldownKey) / 1000L;
                if(time <= 0){
                    time = 0;
                }
                return String.format("%.1f",time);
            } else {
                int keyBoard = Integer.parseInt(sp[1]);
                String skillName = playerData.getSkillNameBind(keyBoard);
                if(skillName.equalsIgnoreCase("default")){
                    return "0.0";
                }
                String cooldownKey = "skill_"+skillName;
                double time = (double) CDTimeAPI.getCD(uuid, cooldownKey) / 1000L;
                if(time <= 0){
                    time = 0;
                }
                return String.format("%.1f",time);
            }
        }
        if(papiType.equalsIgnoreCase("cdmax")){
            if(skillKey.equalsIgnoreCase("翻滚")){
                return "5.0";
            } else {
                int keyBoard = Integer.parseInt(sp[1]);
                String skillName = playerData.getSkillNameBind(keyBoard);
                if(skillName.equalsIgnoreCase("default")){
                    return "10.0";
                }
                return String.format("%.1f",playerData.getCooldownData(skillName));
            }
        }
        if (sp[0].equalsIgnoreCase("icon")){
            int keyBoard = Integer.parseInt(sp[1]);
            return playerData.getSkillNameBind(keyBoard);
        }
        return "0.0";
    }
}