package com.yaohun.storyline;

import com.yaohun.storyline.config.Config;
import com.yaohun.storyline.core.Quest;
import com.yaohun.storyline.data.PlayerData;
import com.yaohun.storyline.gui.DialogueGui;
import com.yaohun.storyline.listener.PlayerListener;
import com.yaohun.storyline.manage.ExternalManager;
import com.yaohun.storyline.manage.PlayerManager;
import com.yaohun.storyline.manage.QuestManager;
import com.yaohun.storyline.network.DragonChannel;
import com.yaohun.storyline.util.MessageUtil;
import com.yaohun.storyline.util.QuestShow;
import eos.moe.dragoncore.api.CoreAPI;
import eos.moe.dragoncore.api.easygui.EasyScreen;
import eos.moe.dragoncore.api.easygui.component.EasyEntityView;
import eos.moe.dragoncore.network.PacketSender;
import me.Demon.DemonLevels.command.MainCmd;
import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

public class Storyline extends JavaPlugin {

    private static Storyline instance;
    private static PlayerManager playerManager;

    @Override
    public void onEnable() {
        instance = this;
        saveDefaultConfig();
        QuestManager.init(this);
        Config.reloadConfig(this);
        MessageUtil.init(this);
        playerManager = new PlayerManager();

        getServer().getPluginManager().registerEvents(new DialogueGui(),this);
        getServer().getPluginManager().registerEvents(new DragonChannel(),this);
        getServer().getPluginManager().registerEvents(new PlayerListener(),this);

        new ExternalManager(this).registerAll();
        if(getServer().getPluginManager().getPlugin("PlaceholderAPI") != null){
            new PapiExpansion(this).register();
        }
    }

    @Override
    public void onDisable() {
        getPlayerManager().saveAllPlayerData();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String Command, String[] args) {
        if (!sender.isOp()) return true;
        if (args.length == 0) {
            sender.sendMessage("");
            sender.sendMessage("§e------- ======= §6主线任务 §e======= -------");
            sender.sendMessage("§2/quests info §f- §2查看当前任务进度");
            sender.sendMessage("§2/quests reload §f- §2重新载入配置文件");
            sender.sendMessage("§2/quests setloc §e[任务] §f- §2设置任务坐标");
            sender.sendMessage("§2/quests add §e[任务] §2<数量> §f- §2增加任务进度");
            sender.sendMessage("§2/quests js §e[任务ID] §2<玩家> §f- §2接受任务");
            sender.sendMessage("§2/quests tj §e[任务ID] §2<玩家> §f- §2提交任务");
            sender.sendMessage("§e------- ======= §6主线任务 §e======= -------");
            sender.sendMessage("");
            return true;
        }
        if (args.length == 1 && args[0].equalsIgnoreCase("debug")) {
            if(Config.debug){
                Config.debug = false;
                DemonAPI.sendMessage(sender,"调试模式: §c关闭");
            } else {
                Config.debug = true;
                DemonAPI.sendMessage(sender,"调试模式: §a启动");
            }
            return true;
        }
        if (args.length == 2 && args[0].equalsIgnoreCase("create")) {
            String name = args[1];
            Player player = Bukkit.getPlayer(name);
            if (player == null) return true;
            new BukkitRunnable() {
                @Override
                public void run() {
                    if(player.getLevel() <= 20){
                        getPlayerManager().createPlayerData(player);
                        DemonAPI.sendMessage(sender,"成功为 "+name+" 玩家创建引导任务档案.");
                    } else {
                        System.out.println("创建异常 "+name+" 等级已超过20级,无法创建");
                    }
                }
            }.runTaskLater(this,20L * 3);
            return true;
        }
        if (args.length == 2 && args[0].equalsIgnoreCase("stop")) {
            String name = args[1];
            Player player = Bukkit.getPlayer(name);
            if (player == null) return true;
            getPlayerManager().stopPlayerData(player);
            DemonAPI.sendMessage(sender,"玩家 "+name+" 剧情任务已终止.");
            return true;
        }
        if (args.length == 2 && args[0].equalsIgnoreCase("view")) {
            PacketSender.setThirdPersonView((Player) sender, Integer.parseInt(args[1]));
            DemonAPI.sendMessage(sender,"视角切换: §a"+args[1]);
            return true;
        }
        if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
            QuestManager.reloadQuestConfig(this);
            Config.reloadConfig(this);
            for (PlayerData playerData : playerManager.getAllPlayerData()) {
                playerData.reload(QuestManager.getQuestArrayList());
            }
            sender.sendMessage("§c[主线任务] §f任务及配置文件已重载完成.");
            return true;
        }
        if (args.length == 1 && args[0].equalsIgnoreCase("save")) {
            getPlayerManager().saveAllPlayerData();
            sender.sendMessage("§c[主线任务] §f所有玩家数据已保存.");
            return true;
        }
        if (args.length >= 1 && args[0].equalsIgnoreCase("reset")) {
            String playerName = sender.getName();
            if (args.length == 2) {
                playerName = args[1];
            }
            Player player = Bukkit.getPlayer(playerName);
            if (player == null) {
                return true;
            }
            PlayerData playerData = getPlayerManager().getPlayerData(playerName);
            if(playerData == null) return true;
            playerData.resetPlayerData();
            sender.sendMessage("§c[主线任务] §f任务数据已重置.");
            return true;
        }
        if (args.length >= 1 && args[0].equalsIgnoreCase("info")) {
            String playerName = sender.getName();
            if (args.length == 2) {
                playerName = args[1];
            }
            Player player = Bukkit.getPlayer(playerName);
            if (player == null) {
                return true;
            }
            sender.sendMessage("§7[§6任务§7] §f当前已接受任务: ");
            PlayerData playerData = getPlayerManager().getPlayerData(playerName);
            if(playerData == null) return true;
            for (Quest quest : playerData.getAllQuest()) {
                int amount = playerData.getQuestAmount(quest);
                QuestShow.Show(sender, quest.getDisplayName(), amount, quest.getTargetAmount());
            }
        }
        if (args.length >= 2 && args[0].equalsIgnoreCase("add")) {
            String idKey = args[1];
            int questID = Integer.parseInt(idKey);
            Quest quest = QuestManager.getQuest(questID);
            if (quest == null) {
                sender.sendMessage("§7[§6任务§7] §f未找到该任务。");
                return true;
            }
            int addAmount = 1;
            if (args.length >= 3) {
                addAmount = Integer.parseInt(args[2]);
            }
            String playerName = sender.getName();
            if (args.length >= 4) {
                playerName = args[3];
            }
            Player player = Bukkit.getPlayer(playerName);
            if (player == null) {
                sender.sendMessage("§7[§6任务§7] §f目标玩家不在线。");
                return true;
            }
            PlayerData data = getPlayerManager().getPlayerData(playerName);
            if(data == null) return true;
            data.addQuestAmount(quest, addAmount);
            sender.sendMessage("§7[§6任务§7] §f目标玩家: " + player.getName());
            sender.sendMessage("§7[§6任务§7] §f任务名称: " + quest.getDisplayName());
            int nowAmount = data.getQuestAmount(quest);
            int questAmount = quest.getTargetAmount();
            if (nowAmount >= quest.getTargetAmount()) {
                sender.sendMessage("§f当前进度: §a" + questAmount + "§f/§a" + questAmount);
            } else {
                sender.sendMessage("§f当前进度: §c" + nowAmount + "§f/§c" + questAmount);
            }
            return true;
        }
        if (args.length >= 2 && args[0].equalsIgnoreCase("js")) {
            String idKey = args[1];
            int questID = Integer.parseInt(idKey);
            Quest quest = QuestManager.getQuest(questID);
            if (quest == null) {
                sender.sendMessage("§7[§6任务§7] §f未找到该任务。");
                return true;
            }
            String playerName = sender.getName();
            if (args.length >= 3) {
                playerName = args[2];
            }
            Player player = Bukkit.getPlayer(playerName);
            if (player == null) {
                return true;
            }
            PlayerData playerData = getPlayerManager().getPlayerData(playerName);
            if(playerData == null) return true;
            if (playerData.isReceivedQuest(quest)) {
                player.sendMessage("§7[§6任务§7] §f你已接受该任务。");
                return true;
            }
            playerData.receiveQuest(quest);
            player.sendMessage("§7[§6任务§7] §f成功接受任务: " + quest.getDisplayName());
            return true;
        }
        if (args.length >= 2 && args[0].equalsIgnoreCase("tj")) {
            String idKey = args[1];
            int questID = Integer.parseInt(idKey);
            Quest quest = QuestManager.getQuest(questID);
            if (quest == null) {
                sender.sendMessage("§7[§6任务§7] §f未找到该任务。");
                return true;
            }
            String playerName = sender.getName();
            if (args.length >= 3) {
                playerName = args[2];
            }
            Player player = Bukkit.getPlayer(playerName);
            if (player == null) {
                return true;
            }
            PlayerData playerData = getPlayerManager().getPlayerData(playerName);
            if(playerData == null) return true;
            if (!playerData.isReceivedQuest(quest)) {
                player.sendMessage("§7[§6任务§7] §f你还未接受该任务。");
                return true;
            }
            if (playerData.isFinishedQuest(quest)) {
                player.sendMessage("§7[§6任务§7] §f该任务已完成。");
                return true;
            }
            if (!playerData.canFinishQuest(quest)) {
                player.sendMessage("§7[§6任务§7] §f任务尚未完成,无法提交。");
                return true;
            }
            playerData.finisheQuest(quest);
            player.sendMessage("§7[§6任务§7] §f任务提交成功: " + quest.getDisplayName());
            return true;
        }

        if (args.length == 2 && args[0].equalsIgnoreCase("setloc")) {
            String idKey = args[1];
            int questID = Integer.parseInt(idKey);
            Quest quest = QuestManager.getQuest(questID);
            if (quest == null) {
                sender.sendMessage("§7[§6任务§7] §f未找到该任务。");
                return true;
            }
            Player player = (Player) sender;
            quest.setStartingPoint(player.getLocation());
            sender.sendMessage("§c[主线任务] §f任务 §e"+quest.getQuestID()+" §f坐标设置完成.");
            return true;
        }
        return false;
    }

    public static Storyline inst() {
        return instance;
    }

    public static PlayerManager getPlayerManager() {
        return playerManager;
    }
}
