package com.usky.iot.controller.web;

import com.alibaba.fastjson.JSON;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.HttpUtils;
import com.usky.common.core.util.StringUtils;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.iot.service.BaseAlarmService;
import com.usky.iot.service.DmpDataInfoService;
import com.usky.iot.service.DmpDeviceInfoService;
import com.usky.iot.service.DmpDeviceStatusService;
import com.usky.backend.domain.request.DmpDeviceDataRequestVO;
import com.usky.transfer.RemoteTransferService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zyj
 * @date 2023/6/8 13:52:05
 */
@RestController
@RequestMapping("/deviceHttp")
public class DeviceHttpController {

    @Autowired
    private BaseAlarmService baseAlarmService;

    @Autowired
    private DmpDataInfoService dmpDataInfoService;

    @Autowired
    private RemoteTransferService remoteTransferService;

    @Autowired
    private DmpDeviceInfoService dmpDeviceInfoService;

    @GetMapping(value = "/status")
    public ApiResult<Void> status(){
        dmpDeviceInfoService.deviceStatus();
        return ApiResult.success();
    }

    /**
     * 设备实时历史数据-对接客户
     * @param dmpDeviceDataRequestVO
     * @return
     */
    @PostMapping("/data")
    public ApiResult<Void> data(@RequestBody DmpDeviceDataRequestVO dmpDeviceDataRequestVO){
        dmpDataInfoService.data(dmpDeviceDataRequestVO);
        return ApiResult.success();
    }

    /**
     * 下发设备控制命令
     */
    @GetMapping("/control")
    public ApiResult<Map<String,Object>> control(@RequestParam("productCode") String productCode,
                                                 @RequestParam("deviceUuid") String deviceUuid,
                                                 @RequestParam("commandCode") String commandCode,
                                                 @RequestParam("commandValue") String commandValue,
                                                 @RequestParam(value = "domain",required = false) String domain,
                                                 @RequestParam(value = "userId",required = false) Long userId,
                                                 @RequestParam(value = "userName",required = false) String userName,
                                                 @RequestParam(value = "categoryType",required = false , defaultValue = "1") Integer categoryType,
                                                 @RequestParam(value = "gatewayUuid",required = false) String gatewayUuid){
        return ApiResult.success(dmpDeviceInfoService.control(productCode,deviceUuid,commandCode,commandValue,domain,userId,userName,categoryType,gatewayUuid));
    }


    /**
     * 设备管理-海康监控系统实时视频预览取流
     *
     * @param productCode 产品编码
     * @param deviceId  设备Id
     * @param streamType  码流类型，0:主码流 1:子码流 2:第三码流 参数不填，默认为主码流
     * @param protocol  取流协议（应用层协议），“rtsp”:RTSP协议,“rtmp”:RTMP协议,“hls”:HLS协议（HLS协议只支持海康SDK协议、EHOME协议、GB28181协议、ONVIF协议接入的设备；只支持H264视频编码和AAC音频编码）,参数不填，默认为RTSP协议
     * @param transmode  传输协议（传输层协议），0:UDP 1:TCP 默认是TCP 注： GB28181 2011及以前版本只支持UDP传输
     * @return
     */
    @GetMapping("/hikVideoStream")
    public ApiResult<Map<String, Object>> hikVideoStream(@RequestParam("productCode") String productCode,
                                                         @RequestParam("deviceId") String deviceId,
                                                         @RequestParam("streamType") Integer streamType,
                                                         @RequestParam("protocol") String protocol,
                                                         @RequestParam("transmode") Integer transmode){
        Integer tenantId = SecurityUtils.getTenantId();
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();

        Map<String,Object> map = new HashMap<>();
        map.put("method","streamControl");
        Map<String,Object> map1 = new HashMap<>();
        map1.put("device_id",deviceId);
        map1.put("streamType",streamType);
        map1.put("protocol",protocol);
        map1.put("transmode",transmode);
        map.put("params",map1);

        return ApiResult.success(remoteTransferService.deviceControl(productCode,deviceId, JSON.toJSONString(map),tenantId,userId,userName));
    }

    /**
     * 获取海康系统行车记录数据
     * @param startTime
     * @param endTime
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("driverRecordList")
    public ApiResult<Map<String,Object>> driverRecordList(@RequestParam(value = "startTime",required = false) String startTime,
                                                          @RequestParam(value = "endTime",required = false) String endTime,
                                                          @RequestParam(value = "pageNo") Integer pageNo,
                                                          @RequestParam(value = "pageSize") Integer pageSize){
        Integer tenantId = SecurityUtils.getTenantId();
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();

        Map<String,Object> map = new HashMap<>();
        map.put("method","driverRecordControl");
        Map<String,Object> map1 = new HashMap<>();
        if(StringUtils.isNotBlank(startTime)){
            map1.put("startTime",startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            map1.put("endTime",endTime);
        }
        map1.put("pageNo",pageNo);
        map1.put("pageSize",pageSize);
        map.put("params",map1);

        return ApiResult.success(remoteTransferService.deviceControl("501_HGHK","0001",JSON.toJSONString(map),tenantId,userId,userName));
    }

    /**
     * 查询停车库剩余车位数
     * @param parkSyscode
     * @return
     */
    @GetMapping("parkRemainSpaceList")
    public ApiResult<Map<String,Object>> parkRemainSpaceList(@RequestParam(value = "parkSyscode") String parkSyscode){
        Integer tenantId = SecurityUtils.getTenantId();
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();

        Map<String,Object> map = new HashMap<>();
        map.put("method","parkRemainSpaceControl");
        Map<String,Object> map1 = new HashMap<>();
        map1.put("parkSyscode",parkSyscode);
        map.put("params",map1);

        return ApiResult.success(remoteTransferService.deviceControl("501_HGHK","0002",JSON.toJSONString(map),tenantId,userId,userName));
    }


}
