package com.yutou.qqbot.models;

import com.yutou.napcat.event.GroupMessageEvent;
import com.yutou.napcat.event.MessageEvent;
import com.yutou.napcat.handle.*;
import com.yutou.napcat.http.NapCatApi;
import com.yutou.napcat.model.MessageBean;
import com.yutou.okhttp.HttpBody;
import com.yutou.qqbot.QQNumberManager;
import com.yutou.qqbot.interfaces.ModelInterface;
import com.yutou.qqbot.utlis.ConfigTools;
import com.yutou.qqbot.utlis.Log;
import lombok.val;


import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public abstract class Model implements ModelInterface {

    public static class QQGroupCommands {
        public final static String QQ_HELP = "!help";
        public final static String QQ_SYSTEM_RESTART = "!restart";
        public final static String QQ_UPDATE_IP = "!更新ip";
        public final static String QQ_GET_IP = "!ip";
        public final static String QQ_OPEN_PC = "!开机";
        public final static String QQ_GET_VERSION = "!version";
        public final static String QQ_CMD = "!cmd";
        public final static String QQ_BANGUMI_TODAY = "!今日动画";
        public final static String QQ_BANGUMI_LIST = "!动画";
        public final static String QQ_BANGUMI_SUB = "!查动画";
        public final static String QQ_AUDIO = "!语音";
        public final static String QQ_AUDIO_OPEN_LAMP = "!开灯";
        public final static String QQ_AUDIO_OPEN_AIR = "!开空调";
        public final static String QQ_BT_RELOAD = "!刷bt";
        public final static String QQ_TOOLS_IDEA = "!idea";
        public final static String QQ_TOOLS_IDEA_FILE = "!idea>";
        public final static String QQ_TOOLS_IDEA_URL = "!idea_url";

        public final static String QQ_BANGUMI_INFO = "!保存动画信息>";
        public final static String QQ_MOYU = "!摸鱼";

        public final static String BILI_LIVE_DANMU_SEND = "!b站签到";
        public final static String BILI_LIVE_DANMU_LIST = "!b站列表";
        public final static String BILI_LIVE_DANMU_DEL = "!b站签到删除";

        public final static String QQ_WOODEN = "!电子木鱼";
        public final static String QQ_TIMEOUT = "!timer";

        public final static String GPT = "!百度gpt";
        public final static String GPT_CLEAR = "!百度失忆";

    }

    public static class QQFromCommands {
        public static final String TURNIP_PROPHET = "大头菜";
        public static final String TSDM_PAY = "!tsdm";
        public static final String BAIDU_DOWN = "!bd";
        public static final String ROUTER_ADD = "!添加设备";
        public static final String ROUTER_DEL = "!删除设备";
        public static final String BT_DOWNLOAD = "下载bt";
        public static final String BILI_MANGA_SIGN = "!b站漫画签到";
        public static final String BILI_MANGA_PAY = "!b站漫画积分兑换";
        public static final String BILI_MANGA_PAY_STOP = "!b站漫画积分兑换取消";
    }


    public static List<Class<?>> classList;
    long group;
    public MessageEvent event;

    static {
        classList = new ArrayList<>();
    }

    public Model() {
        if (!classList.contains(getClass())) {
            classList.add(getClass());
        }

    }

    public String msg;
    protected boolean isGroupPower = false;
    private boolean isGroup;
    public Long user;

    public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
        this.event = event;
        msg = event.getRawMessage();
        if(msg==null){
            msg="";
        }
        msg = msg.replace("！", "!").trim();
        this.isGroup = event.isGroup();
        if (isGroup) {
            user = event.getSource().getFromId();
            group = event.getGroupId();
            if (QQNumberManager.getManager().isExistsPower(group, msg.split(" ")[0])) {
                isGroupPower = true;
            }
        } else {
            user = qq;
        }
    }

    public synchronized void onTime(Long qq, String time) {
    }

    public static StringBuilder getCommands(Class<?> commands) {
        StringBuilder builder = new StringBuilder();
        for (Field field : commands.getDeclaredFields()) {
            try {
                field.setAccessible(true);
                builder.append(field.get(null)).append("\n");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return builder;
    }

    public String getMessage(String text) {
        StringBuilder chain = new StringBuilder();
        if (isGroup) {
            chain.append(new At(user));
            chain.append("\n");
        }
        chain.append(text);
        return chain.toString();
    }

    public boolean isAt() {
        return msg.contains("@" + ConfigTools.load(ConfigTools.CONFIG, ConfigTools.QQ_NUMBER, String.class));
    }
    public boolean checkImage(){
        val image = event.findType(Image.class);
        if(image!=null){
            return true;
        }
        val reply = event.findType(Reply.class);
        if(reply!=null){
            val replyEvent = getReply(reply.getData().getId());
            if(replyEvent!=null){
                return replyEvent.findType(Image.class)!=null;
            }
        }
        return false;
    }
    public MessageEvent getReply(long replyId){
        HttpBody<MessageBean> body = null;
        try {
            body = NapCatApi.getMessageApi().getMessage(MessageHandleBuild.create().setMessageId(String.valueOf(replyId)).build()).execute().body();
            if (body == null) {
                return null;
            }
            return MessageEvent.parseHandleHttp(body.getSrc());
        } catch (IOException e) {
            Log.e(e);
        }
        return null;

    }

    public boolean isAdmin() {
        return user == 583819556L;
    }
}
