package com.yutou.qqbot;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.napcat.enums.MessageEnum;
import com.yutou.napcat.event.MessageEvent;
import com.yutou.napcat.handle.BaseHandle;
import com.yutou.napcat.handle.OtherHandle;
import com.yutou.qqbot.MessageEvent.AdminMessage;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.RedisTools;
import com.yutou.qqbot.utlis.XiaoMiRouter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Set;

@Controller
public class QQBotController {
    @ResponseBody
    @RequestMapping("/tools/openpc.do")
    public String open_pc(HttpServletRequest request, String type) {
        if ("nas".equals(type)) {
            try {
                Process process = Runtime.getRuntime().exec("wakeonlan 00:D8:61:6F:02:2F");
                RedisTools.processOut(process.getInputStream());
                RedisTools.processOut(process.getErrorStream());
                process.destroy();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/tools/server.do")
    public String sendServerManager(String title, String msg) {
        AppTools.sendServer(title, msg);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/napcat/qq/bot.do")
    public String qq_bot(HttpServletRequest request, HttpServletResponse response) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            BufferedInputStream stream = new BufferedInputStream(request.getInputStream());
            byte[] bytes = new byte[1024];
            int len = 0, size;
            while ((len = stream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
            String str = outputStream.toString(StandardCharsets.UTF_8);
            MessageEvent event = MessageEvent.parseHandle(str);

            long qqNumber = event.isGroup() ? event.getGroupId() : event.getUserId();
            boolean isGroup = event.isGroup();

            if ((event.isGroup() && event.getSource().getUserId() == 583819556L) || (!event.isGroup() && event.getUserId() == 583819556L)) {
                new AdminMessage(qqNumber, event.getRawMessage());
            }
            for (Class<?> model : Model.classList) {
                if (QQNumberManager.getManager().isUseModel(qqNumber, model) || !isGroup) {
                    try {
                        Model useModel = (Model) model.getDeclaredConstructor().newInstance();
                        if (!useModel.isUserPublic()) {
                            if (!QQNumberManager.getManager().isExistsPower(event.getSource().getFromId(), useModel.getUsePowers())) {
                                continue;
                            }
                        }
                        useModel.onMessage(qqNumber, event, isGroup);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "200";
    }

    @ResponseBody
    @RequestMapping("/router/wan1ip.do")
    public String getWan1IP(){
        return XiaoMiRouter.getInstance().getWan1IPAddress();
    }
    @ResponseBody
    @RequestMapping("/router/wan2ip.do")
    public String getWan2IP(){
        return XiaoMiRouter.getInstance().getWan2IPAddress();
    }
}
