package com.yfd.platform.modules.test.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yfd.platform.component.WebSocketServer;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.utils.HttpRESTfulUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Date: 2024/4/17 18:14
 * @Description:
 */
@RestController
@RequestMapping("/")
@Api(value = "VoiceController", tags = "测试接口")
public class VoiceController {

    @Resource
    private HttpRESTfulUtils httpRESTfulUtils;

    @PostMapping("/voiceprintDataCollect")
    @ApiOperation("智能分析识别通知")
    public ResponseResult voiceprintDataCollect(@RequestBody String params) {

        Map<String, Object> param = new HashMap<>();
        JSONObject jsonObject = JSONUtil.parseObj(params);
        String requestId = jsonObject.getStr("requestId");
        JSONArray objectList = jsonObject.getJSONArray("objectList");
        JSONArray resultList = new JSONArray();
        for (int i = 0; i < objectList.size(); i++) {
            JSONObject object = objectList.getJSONObject(i);
            JSONObject result = new JSONObject();
            result.putOnce("objectId", object.getStr("objectId"));
            result.putOnce("code", "2000");
            JSONArray results = new JSONArray();
            JSONObject file = new JSONObject();
            file.putOnce("filename", "voice.wav");
            file.putOnce("fileUrl", "http://192.168.1.20:8082/video/voice.wav");
            results.add(file);
            result.putOnce("results", results);
            resultList.add(result);
        }
        param.put("requestId", requestId);
        param.put("resultList", resultList);
        httpRESTfulUtils.sendHttpPost("json", "192.168.1.20", "8082", "", "voiceprintDataCollectRetNotify", param,
                null);

        return ResponseResult.successData(params);
    }

    @PostMapping("/voiceprintNnalyse")
    @ApiOperation("智能分析识别通知")
    public ResponseResult voiceprintNnalyse(@RequestBody String params) {
        Map<String, Object> param = new HashMap<>();
        JSONObject jsonObject = JSONUtil.parseObj(params);
        String requestId = jsonObject.getStr("requestId");
        JSONArray objectList = jsonObject.getJSONArray("objectList");
        JSONArray resultList = new JSONArray();
        for (int i = 0; i < objectList.size(); i++) {
            JSONObject object = objectList.getJSONObject(i);
            JSONArray typeList = object.getJSONArray("typeList");
            JSONObject resultObject = new JSONObject();
            JSONArray results = new JSONArray();
            for (int j = 0; j < typeList.size(); j++) {
                JSONObject result = new JSONObject();
                String type = typeList.getStr(j);
                result.putOnce("type", type);
                result.putOnce("value", "1");
                result.putOnce("startTime", DateUtil.now());
                result.putOnce("endTime", DateUtil.now());
                result.putOnce("conf", "0.99");
                result.putOnce("desc", "局部放电" + j);
                results.add(result);
            }
            resultObject.putOnce("objectId", object.getStr("objectId"));
            resultObject.putOnce("code", "2000");
            resultObject.putOnce("results", results);
            resultList.add(resultObject);
        }
        param.put("requestId", requestId);
        param.put("resultList", resultList);
        httpRESTfulUtils.sendHttpPost("json", "192.168.1.20", "8082", "", "voiceprintNnalyseRetNotify", param,
                null);
        return ResponseResult.successData(params);
    }


    @PostMapping("/sendMediaFileUrl")
    @ApiOperation("机巢服务发送文件路径")
    public ResponseResult sendMediaFileUrl(String jobId) {
        // 创建一个固定大小的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(28);
        for (int i = 1; i <= 28; i++) {
            int finalI = i;
            executorService.submit(() -> {
                int count = finalI % 10;
                if (count == 0) {
                    count = 10;
                }
                String fileName = String.format("ssdfasdf_航点%s.jpeg", count);
                Map<String, Object> param = new HashMap<>(10);
                param.put("url", "http://192.168.1.20:8070/video/20240709174959.png");
                param.put("fileName", fileName);
                param.put("jobId", jobId);
                String api = "dock/task/sendMediaFileUrl";
                httpRESTfulUtils.sendHttpPost("json", "192.168.1.20", "8070", "", api, param, null);
            });

        }
        return ResponseResult.success();
    }

}
