package com.yutou.qqbot.Controllers;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.napcat.QQDatabase;
import com.yutou.napcat.handle.At;
import com.yutou.napcat.handle.BaseHandle;
import com.yutou.napcat.handle.Image;
import com.yutou.napcat.handle.Text;
import com.yutou.napcat.model.SendMessageResponse;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.HttpTools;
import com.yutou.qqbot.utlis.RedisTools;
import com.yutou.qqbot.utlis.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AppController {
    @RequestMapping("/restart.do")
    @ResponseBody
    public String restart() {
        AppTools.exec("cd /home/yutou/public/servier/qqbot && ./start.sh", null, true, false);
        return "";
    }

    @ResponseBody
    @RequestMapping("/door/set.do")
    public String openDoor(String status) {
        RedisTools.set("door", status);
        return "1";
    }

    @ResponseBody
    @RequestMapping("/door/get.do")
    public String getDoor() {
        if ("open".equals(RedisTools.get("door"))) {
            RedisTools.set("door", "on");
            return "1";
        }
        return "0";
    }

    @ResponseBody
    @RequestMapping("/door/status.do")
    public String getStatus() {
        String status = RedisTools.get("door");
        if ("open".equals(status)) {
            return "0";
        } else if ("on".equals(status)) {
            return "2";
        } else if ("over".equals(status)) {
            RedisTools.set("door", "ready");
            return "1";
        }
        return "-1";
    }

    @ResponseBody
    @RequestMapping("/qq/send.do")
    public String sendQQ(@RequestBody JSONObject json) {
        File image = null;
        if (json.getString("message").isEmpty()) {
            return "not message";
        }
        List<BaseHandle<?>> list=new ArrayList<>();
        list.add(new Text(json.getString("message")));
        if(json.getString("image")!=null&&!json.getString("image").isEmpty()){
            list.add(new Image(json.getString("image")));
        }
        SendMessageResponse sent = QQBotManager.getInstance().sendMessage(QQDatabase.checkFriend(json.getLong("qq")),
                json.getLong("qq"),
                list
        );
        return sent == null ? "0" : sent.getId() + "";
    }

    @ResponseBody
    @RequestMapping("/qq/file.do")
    public String sendFile(@RequestParam("image") MultipartFile file) throws Exception {
        String path = AppTools.createFile("tmp", file, System.currentTimeMillis() + ".png");
        if (StringUtils.isEmpty(path)) {
            return "not file";
        }
        File _file = new File(path);
        QQBotManager.getInstance().sendMessage(_file, 583819556L, "time = " + AppTools.getToDayNowTimeToString());
        return "ok";
    }

    @RequestMapping("/test.do")
    @ResponseBody
    public String test(HttpServletResponse response) {
        System.out.println("NAS自动关机");
       /* try {
            response.sendRedirect("http://192.168.31.88:9999/live/index.m3u8");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //  return HttpTools.http_get("http://192.168.31.88:9999/live/index.m3u8",null);
        return "1";
    }

    @RequestMapping("*.ts")
    public void test2(HttpServletResponse response, HttpServletRequest request) {
        try {
            response.sendRedirect("http://192.168.31.88:9999/live" + request.getRequestURI());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
