package com.yutou.qqbot.MessageEvent;

import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.QQNumberManager;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.models.WebSign.BaiHeHui;
import com.yutou.qqbot.models.WebSign.NicePT;
import com.yutou.qqbot.models.WebSign.Tsdm;
import com.yutou.qqbot.models.WebSign.ZhuZhu;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.Log;
import com.yutou.qqbot.utlis.StringUtils;
import com.yutou.qqbot.utlis.WebClient;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class AdminMessage extends Message {

    public static class QQCommands {
        private static final String POWER_ADD = "!新增权限";
        private static final String POWER_SHOW = "!查看权限";
        private static final String POWER_DEL = "!删除权限";
        private static final String MODEL_ADD = "!新增模块";
        private static final String MODEL_SHOW = "!查看模块";
        private static final String MODEL_DEL = "!删除模块";

        public static final String SYSTEM_RESTART_QQ = "!重启qq";
        public static final String SYSTEM_RESTART_TOOLS = "!重启服务";

        public static final String SYSTEM_UPDATE_QQ = "!更新qq";
        public static final String SYSTEM_UPDATE_NAS = "!更新服务";


        private static final String SHOW = "!列表";
        private static final String HELP = "!help";
        private static final String SIGN = "!签到";
    }

    public AdminMessage(Long qq, String msg) {
        sendQQ = qq;
        String[] msgGroup = msg.replace("！", "!").split(" ");

        switch (msgGroup[0]) {
            case QQCommands.HELP -> QQBotManager.getInstance().sendMessage(qq, Model.getCommands(QQCommands.class).toString());
            case QQCommands.POWER_ADD -> {
                if (msgGroup.length == 1) {
                    StringBuilder builder = Model.getCommands(Model.QQGroupCommands.class);
                    builder.append("\n");
                    builder.append(Model.getCommands(Model.QQFromCommands.class));
                    QQBotManager.getInstance().sendMessage(qq, "使用方法：" + QQCommands.POWER_ADD + " [qqnumber] 权限" + "\n" + "可设置以下权限，用&分割");
                    QQBotManager.getInstance().sendMessage(qq, builder.toString());
                } else {
                    if (msgGroup.length <= 2) {
                        addPower(msgGroup[1], null);
                    } else {
                        addPower(msgGroup[1], msgGroup[2]);
                    }
                }
            }
            case QQCommands.POWER_SHOW -> {
                if (msgGroup.length == 1) {
                    QQBotManager.getInstance().sendMessage(qq, "使用方法：" + QQCommands.POWER_SHOW + " [qqnumber]");
                } else {
                    List<String> list = QQNumberManager.getManager().getPower(Long.parseLong(msgGroup[1]));
                    StringBuilder builder = new StringBuilder();
                    for (String power : list) {
                        builder.append(power).append("\n");
                    }
                    QQBotManager.getInstance().sendMessage(qq, Long.parseLong(msgGroup[1]) + " 拥有以下权限:" + builder);
                }
            }
            case QQCommands.POWER_DEL -> {
                if (msgGroup.length == 1) {
                    QQBotManager.getInstance().sendMessage(qq, "使用方法：" + QQCommands.POWER_DEL + " [qqnumber] 权限 用&分割");
                } else {
                    if (msgGroup.length <= 2) {
                        delPower(msgGroup[1], null);
                    } else {
                        delPower(msgGroup[1], msgGroup[2]);
                    }
                }
            }
            case QQCommands.MODEL_ADD -> {
                if (msgGroup.length == 1) {
                    StringBuilder builder = new StringBuilder();
                    for (Class<?> aClass : Model.classList) {
                        try {
                            Model model = (Model) aClass.getDeclaredConstructor().newInstance();
                            builder.append("[").append(model.getModelName()).append("]");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        builder.append(aClass.getName()).append("\n");
                    }
                    QQBotManager.getInstance().sendMessage(qq, "使用方法：" + QQCommands.MODEL_ADD + " [qqnumber] 模块" + "\n" + "可设置以下模块，用&分割");
                    QQBotManager.getInstance().sendMessage(qq, builder.toString());
                } else {
                    if (msgGroup.length <= 2) {
                        addModel(msgGroup[1], null);
                    } else {
                        if ("this".equals(msgGroup[1])) {
                            addModel(sendQQ + "", msgGroup[2]);
                        } else {
                            addModel(msgGroup[1], msgGroup[2]);
                        }
                    }
                }
            }
            case QQCommands.MODEL_SHOW -> {
                if (msgGroup.length == 1) {
                    QQBotManager.getInstance().sendMessage(qq, "使用方法：" + QQCommands.MODEL_SHOW + " [qqnumber]");
                } else {
                    String qqGroup = msgGroup[1];
                    if ("this".equals(qqGroup)) {
                        qqGroup = sendQQ + "";
                    }
                    List<String> list = QQNumberManager.getManager().getUseModel(Long.parseLong(qqGroup));
                    StringBuilder builder = new StringBuilder();
                    for (String power : list) {
                        builder.append(power).append("\n");
                    }
                    QQBotManager.getInstance().sendMessage(qq, Long.parseLong(qqGroup) + " 拥有以下模块:" + builder);
                }
            }
            case QQCommands.MODEL_DEL -> {
                if (msgGroup.length == 1) {
                    QQBotManager.getInstance().sendMessage(qq, "使用方法：" + QQCommands.MODEL_DEL + " [qqnumber] 模块 用&分割");
                } else {
                    String qqGroup = msgGroup[1];
                    if ("this".equals(qqGroup)) {
                        qqGroup = sendQQ + "";
                    }
                    if (msgGroup.length <= 2) {
                        delModel(qqGroup, null);
                    } else {
                        delModel(qqGroup, msgGroup[2]);
                    }
                }
            }
            case QQCommands.SHOW -> {
                List<Long> users = QQNumberManager.getManager().getNumber();
                StringBuilder builder = new StringBuilder();
                for (Long user : users) {
                    builder.append(user).append(":\n");
                    List<String> modelPower = QQNumberManager.getManager().getUseModel(user);
                    builder.append("模块(").append(modelPower.size()).append("):\n");
                    for (String model : modelPower) {
                        builder.append(model).append("\n");
                    }
                    List<String> list = QQNumberManager.getManager().getPower(user);
                    builder.append("权限(").append(list.size()).append("):\n");
                    for (String power : list) {
                        builder.append(power).append("\n");
                    }
                    builder.append("\n");
                }
                QQBotManager.getInstance().sendMessage(qq, builder.toString());
            }
            case QQCommands.SYSTEM_RESTART_QQ -> {
                QQBotManager.getInstance().sendMessage(qq, "正在重启机器人");
                System.out.println("结束进程");
                AppTools.exec("cd /home/yutou/public/servier/qqbot && ./start.sh", null, true, false);
            }
            case QQCommands.SYSTEM_RESTART_TOOLS -> {
                QQBotManager.getInstance().sendMessage(qq, "正在重启服务");
                System.out.println("结束进程");
                AppTools.exec("cd /home/yutou/public/servier/tools && ./start.sh", null, true, false);
            }
            case QQCommands.SYSTEM_UPDATE_QQ -> {
                QQBotManager.getInstance().sendMessage(qq, "正在更新qq");
                System.out.println("结束进程");
                AppTools.exec("cd /home/yutou/git/QQBot && ./update.sh", null, true, false);
            }
            case QQCommands.SYSTEM_UPDATE_NAS -> {
                QQBotManager.getInstance().sendMessage(qq, "正在更新NAS服务");
                System.out.println("结束进程");
                AppTools.exec("cd /home/yutou/git/nas-service && ./update.sh", null, true, false);
            }

            case QQCommands.SIGN -> {
                boolean sign = true;
                boolean tsdmSign = false, baiheSign = false, nicePtSign = false, zhuzhuSign=false;
                Log.i("开始签到");
                try {
                    if (new Tsdm().tsdmSign()) {
                        tsdmSign = true;
                    }
                } catch (Exception e) {
                    sign = false;
                    QQBotManager.getInstance().sendMessage(qq, "tsdm签到失败:" + AppTools.getExceptionString(e));
                    e.printStackTrace();
                }
                try {
                    if (new BaiHeHui().sign()) {
                        baiheSign = true;
                    }
                } catch (Exception e) {
                    sign = false;
                    QQBotManager.getInstance().sendMessage(qq, "百合会签到失败:" + AppTools.getExceptionString(e));
                    e.printStackTrace();
                }
                try {
                    if (new NicePT().sign()) {
                        nicePtSign = true;
                    }
                } catch (Exception e) {
                    sign = false;
                    QQBotManager.getInstance().sendMessage(qq, "NicePT签到失败:" + AppTools.getExceptionString(e));
                    e.printStackTrace();
                }
                try {
                    if (new ZhuZhu().sign()) {
                        zhuzhuSign = true;
                    }
                } catch (Exception e) {
                    sign = false;
                    QQBotManager.getInstance().sendMessage(qq, "HiRes花园签到失败:" + AppTools.getExceptionString(e));
                    e.printStackTrace();
                }
                if (sign && tsdmSign && baiheSign && nicePtSign) {
                    QQBotManager.getInstance().sendMessage(qq, "签到任务完成");
                }else{
                    StringBuilder builder=new StringBuilder();
                    if(!tsdmSign){
                        builder.append("天使动漫签到失败\n");
                    }
                    if(!baiheSign){
                        builder.append("百合会签到失败\n");
                    }
                    if(!nicePtSign){
                        builder.append("NicePT签到失败\n");
                    }
                    if(!zhuzhuSign){
                        builder.append("HiRes花园签到失败\n");
                    }
                    QQBotManager.getInstance().sendMessage(qq,builder.toString());
                }
                WebClient.getInstance().quit();
            }
        }
    }

    private void addPower(String qq, String msg) {
        boolean flag = false;
        if (msg == null) {
            StringBuilder builder = Model.getCommands(Model.QQGroupCommands.class);
            builder.append("\n");
            builder.append(Model.getCommands(Model.QQFromCommands.class));
            for (String power : builder.toString().split("\n")) {
                if (!StringUtils.isEmpty(power)) {
                    flag = QQNumberManager.getManager().addPower(Long.parseLong(qq), power);
                }
            }
        } else {
            for (String power : msg.split("&")) {
                flag = QQNumberManager.getManager().addPower(Long.parseLong(qq), power);
            }
        }
        QQBotManager.getInstance().sendMessage(sendQQ, flag ? "新增成功" : "新增失败");
    }

    private void delPower(String qq, String msg) {
        if (msg == null) {
            List<String> list = QQNumberManager.getManager().getPower(Long.parseLong(qq));
            for (String power : list) {
                QQNumberManager.getManager().delPower(Long.parseLong(qq), power);
            }
        } else {
            for (String power : msg.split("&")) {
                QQNumberManager.getManager().delPower(Long.parseLong(qq), power);
            }
        }
    }

    private void addModel(String qq, String model) {
        boolean flag = false;
        try {
            if (model == null) {
                for (Class<?> power : Model.classList) {
                    flag = QQNumberManager.getManager().addUseModel(Long.parseLong(qq), power);
                }
            } else {
                for (String power : model.split("&")) {
                    flag = QQNumberManager.getManager().addUseModel(Long.parseLong(qq), Class.forName(power));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        QQBotManager.getInstance().sendMessage(sendQQ, flag ? "新增成功" : "新增失败");
    }

    private void delModel(String qq, String model) {
        boolean flag = false;
        try {
            if (model == null) {
                List<String> list = QQNumberManager.getManager().getUseModel(Long.parseLong(qq));
                for (String power : list) {
                    flag = QQNumberManager.getManager().delUseModel(Long.parseLong(qq), Class.forName(power));
                }
            } else {
                for (String power : model.split("&")) {
                    flag = QQNumberManager.getManager().delUseModel(Long.parseLong(qq), Class.forName(power));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        QQBotManager.getInstance().sendMessage(sendQQ, flag ? "删除成功" : "删除失败");

    }
}
