package com.sz.www.test.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sz.admin.monitor.pojo.dto.edgebox.AlarmReportDTO;
import com.sz.admin.monitor.pojo.dto.edgebox.AlgorithmTaskDTO;
import com.sz.admin.monitor.pojo.dto.edgebox.BoxAlarmReportDto;
import com.sz.admin.monitor.pojo.dto.edgebox.SetupChannelDTO;
import com.sz.admin.monitor.utils.AiBoxRequestUtil;
import com.sz.admin.monitor.utils.AlgMediaConfigResponse;
import com.sz.admin.system.pojo.dto.sysmessage.Message;
import com.sz.admin.system.service.SysMessageService;
import com.sz.core.common.entity.ApiResult;
import com.sz.core.common.entity.SocketMessage;
import com.sz.core.common.entity.TransferMessage;
import com.sz.core.common.enums.MessageTransferScopeEnum;
import com.sz.core.common.enums.SocketChannelEnum;
import com.sz.redis.WebsocketRedisService;
import com.sz.security.core.util.LoginUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ！！！ 【以下为功能演示内容，生产环境请删除】
 *
 * 网站测试
 *
 * @author sz
 * @since 2024/5/24
 * @version 1.0
 */
@Tag(name = "测试网站")
@RestController
@RequestMapping("www")
@RequiredArgsConstructor
@Profile({"dev", "local", "preview"})
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);
    private final WebsocketRedisService websocketRedisService;

    private final SysMessageService sysMessageService;

    private final AiBoxRequestUtil aiBoxRequestUtil;

    @PostMapping("/result")
    @Operation(summary = "获取报警信息报告")
    public ApiResult<Void> getResult(@RequestBody BoxAlarmReportDto boxAlarmReportDto)
    {
         log.info("获取到报警信息：{}", JSON.toJSONString(boxAlarmReportDto));
         return ApiResult.success();
    }
    @PostMapping("push/all")
    @Operation(summary = "全体推送-升级公告（socket）")
    public ApiResult<Void> sendUpgradeMsg() {
        SocketMessage bean = new SocketMessage();
        bean.setData("【全体推送】 系统即将进行升级，预计需要几分钟时间。请您稍等片刻，感谢您的耐心等待");
        bean.setChannel(SocketChannelEnum.UPGRADE_CHANNEL);
        bean.setScope(MessageTransferScopeEnum.SOCKET_CLIENT);
        TransferMessage msg = new TransferMessage();
        msg.setMessage(bean);
        msg.setFromUser("system");
        msg.setToPushAll(true);
        websocketRedisService.sendServiceToWs(msg);
        return ApiResult.success();
    }

    @PostMapping("push/user")
    @Operation(summary = "定向推送-升级公告（socket）")
    public ApiResult<Void> sendMsg() {
        SocketMessage bean = new SocketMessage();
        bean.setData("【定向推送】 系统即将进行升级，预计需要几分钟时间。请您稍等片刻，感谢您的耐心等待");
        bean.setChannel(SocketChannelEnum.UPGRADE_CHANNEL);
        bean.setScope(MessageTransferScopeEnum.SOCKET_CLIENT);

        TransferMessage msg = new TransferMessage();
        msg.setMessage(bean);
        msg.setFromUser("system");
        msg.setToPushAll(false);
        List<String> toUsers = new ArrayList<>();
        toUsers.add("1"); // 向 loginId = 1 的用户推送消息。既 admin 账号
        msg.setToUsers(toUsers);
        websocketRedisService.sendServiceToWs(msg);
        return ApiResult.success();
    }

    @Operation(summary = "测试socket踢下线")
    @PostMapping("kick")
    public ApiResult<Void> testKickOff() {
        TransferMessage tm = new TransferMessage();
        tm.setToPushAll(true);
        SocketMessage sb = new SocketMessage();
        sb.setChannel(SocketChannelEnum.KICK_OFF);
        tm.setMessage(sb);
        websocketRedisService.sendServiceToWs(tm);
        return ApiResult.success();
    }

    @Operation(summary = "测试消息发送")
    @GetMapping("message/send")
    public ApiResult<Void> sendMessage() {
        SocketMessage bean = new SocketMessage();
        Map<String, Object> alarmData = new HashMap<>();
        alarmData.put("title", "系统警告");
        alarmData.put("content", "通道66发生了移位，请注意查看！");
        bean.setData(JSON.toJSONString(alarmData));
        bean.setChannel(SocketChannelEnum.MESSAGE);
        bean.setScope(MessageTransferScopeEnum.SOCKET_CLIENT);
        TransferMessage msg = new TransferMessage();
        msg.setMessage(bean);
        msg.setFromUser("system");
        msg.setToPushAll(true);
        Message message = new Message();
        message.setMessageTypeCd("msg");
        message.setSenderId(1L);
        message.setTitle("系统警告");
        message.setContent("通道66发生了移位，请注意查看！");
        // 获取登录用户
      //  LoginUser loginUser = LoginUtils.getLoginUser();
        System.out.println(LoginUtils.getLoginUser().getUserInfo());
        message.setReceiverIds(List.of(1));
        sysMessageService.create(message);
      //  websocketRedisService.sendServiceToWs(msg);
        return ApiResult.success();
    }

    @Operation(summary = "测试盒子配置通道功能")
    @GetMapping("/test")
    public ApiResult<JSONObject> test() {
            SetupChannelDTO setupChannelDTO = new SetupChannelDTO();
        setupChannelDTO.setMediaUrl("rtsp://admin:rytec.2020@192.168.2.110:554/Streaming/Channels/901");
        setupChannelDTO.setMediaDesc("机柜球机");
            setupChannelDTO.setMediaName("机柜球机");
        //  aiBoxRequestUtil.setupVideoChannel(setupChannelDTO);
        return ApiResult.success();
    }

    @Operation(summary = "测试盒子配置任务功能")
    @GetMapping("/test1")
    public ApiResult<AlgMediaConfigResponse> test1() {
        AlgorithmTaskDTO algTaskConfigDto = new AlgorithmTaskDTO();
        algTaskConfigDto.setAlgInfo(List.of(8, 52));
        algTaskConfigDto.setMediaName("机柜球机");
        algTaskConfigDto.setTaskDesc("机柜球机");
        algTaskConfigDto.setAlgTaskSession("55");
        AlgorithmTaskDTO.UserDataDto userData = new AlgorithmTaskDTO.UserDataDto();
        userData.setMethodConfig(List.of(46,207));
        userData.setEnableFireSmokeFilter(true);
//        可以有，也可以不需要，有默认值
        userData.setFireThreshold(0.6);
        userData.setSmokeThreshold(0.6);
        userData.setFireSmokeKeepSec(3);
        userData.setFireSmokeAlarmIntervalSec(10);
        userData.setFaceRegSimilarity(0.45);
        userData.setFaceRegEnableGat1400(false);
        userData.setFaceRegEnableMultiCheck(false);
        userData.setFaceLowSimilarity(0d);
        userData.setFaceIdUploadIntervalMs(0);
        userData.setFrontFaceMinPixel(30);
        userData.setFaceRepositories(1);
        userData.setEnableFireDeepMode(false);
        userData.setThresholdForFace(0.5);
        algTaskConfigDto.setUserData(userData);
        return null;
    }
    @Operation(summary = "测试盒子开启关闭任务功能")
    @GetMapping("/test2")
    public ApiResult<AlgMediaConfigResponse> tes2() {
        //aiBoxRequestUtil.controlTask("55",0)
        return ApiResult.success();
    }
    @Operation(summary = "测试盒子删除任务功能")
    @GetMapping("/test3")
    public ApiResult<AlgMediaConfigResponse> tes3() {
        //aiBoxRequestUtil.deleteTask("55")
        return ApiResult.success();
    }
    @Operation(summary = "测试盒子删除通道功能")
    @GetMapping("/test4")
    public ApiResult<AlgMediaConfigResponse> tes4() {
        //aiBoxRequestUtil.deleteVideoChannel("机柜球机")
        return ApiResult.success();
    }
    @Operation(summary = "接收报警信息")
    @PostMapping("/shou")
    public void recevice(@RequestBody AlarmReportDTO alarmReportDto)
    {
        log.info("接收到报警信息：{}",alarmReportDto);
    }

}
