package pw.yumc.BukkitInjectedTools;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;

import pw.yumc.BukkitInjectedTools.event.YumTestEvent;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.CommandSub;
import pw.yumc.YumCore.commands.annotation.Cmd;
import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.commands.annotation.Option;
import pw.yumc.YumCore.commands.annotation.Sort;
import pw.yumc.YumCore.commands.interfaces.Executor;
import pw.yumc.YumCore.kit.PKit;

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

    public YumTestCommand() {
        Log.setPrefix(prefix);
        new CommandSub("bit", this);
    }

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

    @Cmd
    @Help("Yum 事件拦截测试")
    @Sort(3)
    public void event(CommandSender sender) {
        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(CommandSender sender, String version) throws IOException {
        switch (version) {
        case "1.8.8":
            InjectedKit.injectProtocolSupport(sender, getDataFolder().getCanonicalPath() + File.separatorChar + "ProtocolSupport", false);
            break;
        case "1.9.4":
        case "1.10":
            InjectedKit.injectProtocolSupport(sender, getDataFolder().getCanonicalPath() + File.separatorChar + "ProtocolSupport", true);
            break;
        default:
            Log.sender(sender, "§c未知的版本!");
            break;
        }
    }

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

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

    @Cmd
    @Help("Yum OP拦截测试")
    @Sort(4)
    public void op(CommandSender sender, String user) {
        Log.sender(sender, "§cSetOp拦截测试!");
        final OfflinePlayer op = Bukkit.getOfflinePlayer(user);
        op.setOp(true);
        sender.sendMessage(prefix + "§cSetOp拦截测试结束!");
    }

    @Cmd
    @Help(value = "Yum 线程检测测试", possibleArguments = "文件大小[1 5 10 50]")
    @Sort(6)
    public void thread(final CommandSender sender, @Option("def:5") final String size) throws IOException {
        if (downloading) {
            Log.sender(sender, "§c正在主线程下载文件,请勿重复测试...!");
            return;
        }
        Log.sender(sender, "§c主线程中断测试开始!");
        PKit.scheduleTask(new Runnable() {
            @Override
            public void run() {
                downloading = true;
                try {
                    final File nFile = new File(getDataFolder(), "null");
                    Files.copy(new URL("https://coding.net/u/502647092/p/YumData/git/raw/master/dd/" + size).openStream(), nFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    nFile.delete();
                    Log.sender(sender, "§a主线程中断测试结束!");
                } catch (final IOException e) {
                    Log.sender(sender, "§a主线程中断测试失败 错误: " + e.getClass().getSimpleName() + " " + e.getMessage());
                }
                downloading = false;
            }
        });
    }

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