package com.yaohun.demonskills;

import com.yaohun.demonskills.config.Config;
import com.yaohun.demonskills.config.MessageUtil;
import com.yaohun.demonskills.core.Skill;
import com.yaohun.demonskills.core.SkillStoneUpgrade;
import com.yaohun.demonskills.data.SkillData;
import com.yaohun.demonskills.gui.StoneEquipGui;
import com.yaohun.demonskills.manage.SkillManager;
import com.yaohun.demonskills.data.PlayerData;
import com.yaohun.demonskills.gui.SkillStoneGui;
import com.yaohun.demonskills.gui.SkillsGui;
import com.yaohun.demonskills.listener.PlayerListener;
import com.yaohun.demonskills.listener.SkillKeyListener;
import com.yaohun.demonskills.manage.PlayerManager;
import com.yaohun.demonskills.util.AsyncPlayerDataSaver;
import com.yaohun.demonskills.util.CDTimeAPI;
import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

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

public class SkillsMain extends JavaPlugin {

    private static SkillsMain instance;
    private static PlayerManager playerManager;


    @Override
    public void onEnable() {
        instance = this;
        SkillManager.init();
        Config.reloadConfig(this);
        playerManager = new PlayerManager();
        getServer().getPluginManager().registerEvents(new SkillsGui(),this);
        getServer().getPluginManager().registerEvents(new SkillKeyListener(),this);
        getServer().getPluginManager().registerEvents(new PlayerListener(),this);
        getServer().getPluginManager().registerEvents(new SkillStoneGui(),this);
        getServer().getPluginManager().registerEvents(new StoneEquipGui(),this);
        // getServer().getPluginManager().registerEvents(new CommonAttackListener(),this);
        if(getServer().getPluginManager().getPlugin("PlaceholderAPI") != null){
            new PapiExpansion(this).register();
        }
        Bukkit.getConsoleSender().sendMessage("§b[魂式技能] §a插件成功载入Server！");
        Bukkit.getConsoleSender().sendMessage("§b[魂式技能] §a妖魂QQ: §b1763917516");

        new AsyncPlayerDataSaver(this);
    }

    @Override
    public void onDisable() {
        playerManager.SaveAllPlayerData();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String Command, String[] args) {
        if (args.length == 1 && args[0].equalsIgnoreCase("open")) {
            SkillsGui.OpenGui((Player) sender);
            return true;
        }
        if (!sender.isOp()) {
            return true;
        }
        if (args.length == 0) {
            sender.sendMessage("");
            sender.sendMessage("§e------- ======= §6技能系统 §e======= -------");
            sender.sendMessage("§2/" + Command + " open §f- §2打开界面");
            sender.sendMessage("§2/" + Command + " admin §f- §2灵石仓库");
            sender.sendMessage("§2/" + Command + " upgrade §f- §2进阶灵石");
            sender.sendMessage("§2/" + Command + " reduce §f- §2降级灵石");
            sender.sendMessage("§2/" + Command + " stone §e[等级] §2<玩家> §f- §2随机一枚");
            sender.sendMessage("§2/" + Command + " give §e[灵石] §2<等级> §b<玩家> §f- §2给予灵石");
            sender.sendMessage("§2/" + Command + " mail §e[灵石] §2<等级> §b<玩家> §f- §2给予灵石");
            sender.sendMessage("§e------- ======= §6技能系统 §e======= -------");
            return true;
        }
        if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
            SkillManager.skillDataMap.clear();
            SkillManager.init();
            DemonAPI.sendMessage(sender, "配置文件已重载.");
            return true;
        }
        if (args.length == 1 && args[0].equalsIgnoreCase("upgrade")) {
            SkillStoneUpgrade.carryOut((Player) sender,true);
            return true;
        }
        if (args.length == 1 && args[0].equalsIgnoreCase("reduce")) {
            SkillStoneUpgrade.carryOut((Player) sender,false);
            return true;
        }
        if (args[0].equalsIgnoreCase("admin")) {
            int level = 1;
            String playerName = sender.getName();
            if(args.length == 2) {
                level = Integer.parseInt(args[1]);
            }
            if(args.length == 3) {
                playerName = args[2];
            }
            SkillStoneGui.OpenGui((Player) sender,playerName,level);
            return true;
        }
        if (args[0].equalsIgnoreCase("give")) {
            if(args.length == 1){
                List<String> stringList = new ArrayList<>(SkillManager.skillDataMap.keySet());
                sendMessage(sender,"灵石方案: §6"+String.join("§7, §6", stringList));
                return true;
            }
            String skillName = args[1];
            int skillLevel = 1;
            String playerName = sender.getName();
            if(args.length >= 3){
                skillLevel = Integer.parseInt(args[2]);
            }
            if(args.length >= 4){
                playerName = args[3];
            }
            Player player = Bukkit.getPlayer(playerName);
            if(player == null){
                player = (Player) sender;
            }
            if (!SkillManager.isSkillDataExit(skillName)) {
                sendMessage(sender,"技能石 §6"+skillName+" §a不存在.");
                return true;
            }
            SkillData skillData = SkillManager.skillDataMap.get(skillName);
            ItemStack itemStack = skillData.getItemStack(playerName,skillLevel);
            player.getInventory().addItem(itemStack);
            String itemName = DemonAPI.getItemName(itemStack);
            String message = Config.langData.getMessage("getNotification").replace("{name}",itemName);
            MessageUtil.sendMessage(player,message,Sound.ENTITY_EXPERIENCE_ORB_PICKUP);
            sender.sendMessage("[技能系统] 玩家 "+playerName+" 获得 "+skillName+"[Lv."+skillLevel+"]  [背包]");
            return true;
        }
        if (args[0].equalsIgnoreCase("mail")) {
            if(args.length == 1){
                List<String> stringList = new ArrayList<>(SkillManager.skillDataMap.keySet());
                sendMessage(sender,"灵石方案: §6"+String.join("§7, §6", stringList));
                return true;
            }
            String skillName = args[1];
            int skillLevel = 1;
            String playerName = sender.getName();
            if(args.length >= 3){
                skillLevel = Integer.parseInt(args[2]);
            }
            if(args.length >= 4){
                playerName = args[3];
            }
            if (!SkillManager.isSkillDataExit(skillName)) {
                sendMessage(sender,"技能石 §6"+skillName+" §a不存在.");
                return true;
            }
            SkillData skillData = SkillManager.skillDataMap.get(skillName);
            ItemStack itemStack = skillData.getItemStack(playerName,skillLevel);
            DemonAPI.giveMailItem(playerName,itemStack);
            sender.sendMessage("[技能系统] 玩家 "+playerName+" 获得 "+skillName+"[Lv."+skillLevel+"]  [邮箱]");
            return true;
        }
        if (args[0].equalsIgnoreCase("stone")) {
            int skillLevel = 1;
            String playerName = sender.getName();
            if(args.length >= 2){
                skillLevel = Integer.parseInt(args[1]);
            }
            String skillName = SkillManager.getRandomStone();
            if (!SkillManager.isSkillDataExit(skillName)) {
                sendMessage(sender,"技能石 §6"+skillName+" §a不存在.");
                return true;
            }
            if(args.length >= 3){
                playerName = args[2];
            }
            Player player = Bukkit.getPlayer(playerName);
            SkillData skillData = SkillManager.skillDataMap.get(skillName);
            ItemStack itemStack = skillData.getItemStack(playerName,skillLevel);
            if(player == null){
                DemonAPI.giveMailItem(playerName,itemStack);
                sender.sendMessage("[技能系统] 玩家 "+playerName+" 获得 "+skillName+"[Lv."+skillLevel+"]  [邮箱]");
            } else {
                player.getInventory().addItem(itemStack);
                sender.sendMessage("[技能系统] 玩家 "+playerName+" 获得 "+skillName+"[Lv."+skillLevel+"]  [背包]");
                String itemName = DemonAPI.getItemName(itemStack);
                String message = Config.langData.getMessage("getNotification").replace("{name}",itemName);
                MessageUtil.sendMessage(player,message,Sound.ENTITY_EXPERIENCE_ORB_PICKUP);
            }
            return true;
        }
        if (args[0].equalsIgnoreCase("info")) {
            String playerName = sender.getName();
            if(args.length == 2){
                playerName = args[1];
            }
            PlayerData playerData = playerManager.getPlayerData(playerName);
            sendMessage(sender,"正在<§b"+playerName+"§a>查看技能数据:");
            sender.sendMessage("§7- §a魔法值: §e"+playerData.getMagicNow()+"§7/§6"+playerData.getMagicMax());
            sender.sendMessage("§7- §a技能按键绑定: ");
            sender.sendMessage("§7- §r19§e(R)  §7>>  §6"+playerData.getSkillNameBind(19));
            sender.sendMessage("§7- §r44§e(Z)  §7>>  §6"+playerData.getSkillNameBind(44));
            sender.sendMessage("§7- §r45§e(X)  §7>>  §6"+playerData.getSkillNameBind(45));
            sender.sendMessage("§7- §r46§e(C)  §7>>  §6"+playerData.getSkillNameBind(46));
            sender.sendMessage("§7- §r47§e(V)  §7>>  §6"+playerData.getSkillNameBind(47));
            return true;
        } else {
            String skillName = args[0];
            Player player = (Player) sender;
            Skill skill = SkillManager.getNameConvertSkill(player,skillName);
            if(skill == null){
                sender.sendMessage("§f[§c技能§f] §a技能 §c"+skillName+" §a貌似不存在.");
                return true;
            }
            String cooldownKey = "skill_"+skill.name;
            CDTimeAPI.setPlayerCD(player.getUniqueId(),cooldownKey,-1000);
            SkillManager.castSkill(player, skill);
        }
        return false;
    }

    public static SkillsMain inst() {
        return instance;
    }

    public static PlayerManager getPlayerManager() {
        return playerManager;
    }

    public static void sendMessage(CommandSender sender, String message) {
        sender.sendMessage("§f[§c技能§f] §a"+message);
    }
}
