package com.yutou.qqbot.Controllers;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.data.jianr.AndroidDevice;
import com.yutou.qqbot.data.jianr.JianRScriptV2Data;
import com.yutou.qqbot.utlis.JianRTaskManager;
import com.yutou.qqbot.utlis.RedisTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class JianRScriptController {
    @RequestMapping("/jianr/run.do")
    @ResponseBody
    public JSONObject runScript(String task, String device, String modelName) {
        JSONObject json = new JSONObject();
        String data = RedisTools.getHashMap(JianRTaskManager.Redis_Script, task);
        String deviceJsonString = RedisTools.getHashMap(JianRTaskManager.Redis_Device, device);
        if (data == null || deviceJsonString == null) {
            json.put("code", -1);
            json.put("msg", "没有找到该方案或设备错误，请配置");
        } else {
            JianRScriptV2Data script = JSON.parseObject(data, JianRScriptV2Data.class);
            AndroidDevice androidDevice = JSON.parseObject(deviceJsonString, AndroidDevice.class);
            JianRTaskManager manager = JianRTaskManager.getInstance();
            if (manager.isRunning()) {
                manager.stop();
            }
            for (int i = 0; i < androidDevice.getDeviceDisplay().size(); i++) {
                if (androidDevice.getDeviceDisplay().get(i).getTitle().equals(modelName)) {
                    manager.setModelId(i);
                    break;
                }
            }
            manager.setTask(script, androidDevice);
            manager.start();
            json.put("code", 0);
            json.put("msg", "任务创建成功");
        }
        return json;
    }

    @RequestMapping("/jianr/stop.do")
    @ResponseBody
    public JSONObject stop() {
        JianRTaskManager.getInstance().stop();
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", "任务已停止");
        return json;
    }

    @RequestMapping("/jianr/status.do")
    @ResponseBody
    public JSONObject status() {
        JSONObject json = new JSONObject();
        JSONObject data = new JSONObject();
        JianRTaskManager manager = JianRTaskManager.getInstance();
        data.put("status", manager.isRunning());
        data.put("runIndex", manager.getRunIndex());
        data.put("taskName", manager.getTaskName());
        data.put("log", manager.getLog());
        json.put("code", 0);
        json.put("data", data);
        return json;
    }

    @RequestMapping("/jianr/task/list.do")
    @ResponseBody
    public JSONObject tasks() {
        JSONObject json = new JSONObject();
        JSONArray array = new JSONArray();
        Map<String, String> map = RedisTools.getHashMap(JianRTaskManager.Redis_Script);
        array.addAll(map.keySet());
        json.put("code", 0);
        json.put("data", array);
        return json;
    }

    @RequestMapping("/jianr/task/add.do")
    @ResponseBody
    public JSONObject addTask(String task) {
        JSONObject json = new JSONObject();
        JSONObject data = JSON.parseObject(task);
        if (data == null) {
            json.put("code", -1);
            json.put("msg", "JSON格式错误");
        } else {
            String taskName = data.getString("title");
            if (RedisTools.getHashMap(JianRTaskManager.Redis_Script).containsKey(taskName)) {
                json.put("code", -1);
                json.put("msg", "该任务名称已存在");
            } else {
                RedisTools.setHashMap(JianRTaskManager.Redis_Script, taskName, task);
                json.put("code", 0);
                json.put("msg", "任务创建成功");
            }
        }
        return json;
    }

    @RequestMapping("/jianr/task/get.do")
    @ResponseBody
    public JSONObject getTask(String task) {
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("data", RedisTools.getHashMap(JianRTaskManager.Redis_Script, task));
        return json;
    }

    @RequestMapping("/jianr/task/remove.do")
    @ResponseBody
    public JSONObject removeTask(String task) {
        RedisTools.removeHashMap(JianRTaskManager.Redis_Script, task);
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", "任务删除成功");
        return json;
    }

    @RequestMapping("/jianr/device/connect.do")
    @ResponseBody
    public JSONObject connectDevice(String device) {
        JSONObject json = new JSONObject();
        JianRTaskManager.getInstance().connect(device);
        json.put("code", 0);
        json.put("msg", "成功");
        return json;
    }

    @RequestMapping("/jianr/device/add.do")
    @ResponseBody
    public JSONObject addDevice(String device) {
        Map<String, String> hashMap = RedisTools.getHashMap(JianRTaskManager.Redis_Device);
        JSONObject deviceJson = JSON.parseObject(device);
        JSONObject json = new JSONObject();
        json.put("code", 0);
        if (deviceJson == null) {
            json.put("msg", "JSON格式错误");
        } else {
            if (hashMap.containsKey(device)) {
                json.put("msg", "设备已存在");
            } else {
                json.put("msg", RedisTools.setHashMap(JianRTaskManager.Redis_Device,
                        deviceJson.getString("title"), device) ?
                        "设备添加成功" : "设备添加失败");
            }
        }
        return json;
    }

    @RequestMapping("/jianr/device/edit.do")
    @ResponseBody
    public JSONObject editDevice(String device, String oldDevice) {
        JSONObject deviceJson = JSON.parseObject(device);
        JSONObject json = new JSONObject();
        json.put("code", 0);
        if (deviceJson == null) {
            json.put("msg", "JSON格式错误");
        } else {
            RedisTools.removeHashMap(JianRTaskManager.Redis_Device, oldDevice);
            json.put("msg", RedisTools.setHashMap(JianRTaskManager.Redis_Device,
                    deviceJson.getString("title"), device) ?
                    "设备修改成功" : "设备修改失败");
        }
        return json;
    }

    @RequestMapping("/jianr/device/del.do")
    @ResponseBody
    public JSONObject delDevice(String device) {
        JSONObject deviceJson = JSON.parseObject(device);
        JSONObject json = new JSONObject();
        json.put("code", 0);
        if (deviceJson == null) {
            json.put("msg", "JSON格式错误");
        } else {
            json.put("msg", RedisTools.removeHashMap(JianRTaskManager.Redis_Device) ?
                    "设备修改成功" : "设备修改失败");
        }
        return json;
    }

    @RequestMapping("/jianr/device/list.do")
    @ResponseBody
    public JSONObject getDeviceList() {
        Map<String, String> hashMap = RedisTools.getHashMap(JianRTaskManager.Redis_Device);
        JSONObject json = new JSONObject();
        JSONArray array = new JSONArray();
        json.put("code", 0);
        hashMap.keySet().forEach(key -> {
            JSONObject device = JSONObject.parseObject(hashMap.get(key));
            JSONArray child = new JSONArray();
            JSONObject item = new JSONObject();
            JSONObject del = new JSONObject();
            JSONObject edit = new JSONObject();
            JSONObject select = new JSONObject();
            JSONObject connect = new JSONObject();
            item.put("title", key);
            del.put("title", "删除");
            del.put("data", "del");
            del.put("device", device);
            edit.put("title", "编辑");
            edit.put("data", "edit");
            edit.put("device", device);
            select.put("title", "选择");
            select.put("data", "select");
            select.put("device", device);
            connect.put("title", "连接设备");
            connect.put("data", "connect");
            connect.put("device", device);
            child.add(connect);
            child.add(select);
            child.add(del);
            child.add(edit);
            item.put("child", child);
            array.add(item);
        });
        json.put("data", array);
        return json;
    }

    public static void main(String[] args) {
        Map<String, String> hashMap = RedisTools.getHashMap(JianRTaskManager.Redis_Device);
        JSONObject json = new JSONObject();
        JSONArray array = new JSONArray();
        json.put("code", 0);
        hashMap.keySet().forEach(key -> {
            JSONObject item = JSONObject.parseObject(hashMap.get(key));
            array.add(item);
        });
        json.put("data", array);
        System.out.println(json);
    }
}
