package com.mosty.yszx.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.mosty.base.model.entity.lzother.ZhjmxfXfqwXfqyGzdw;
import com.mosty.base.model.query.yszx.TbYsSxtPageOneQuery;
import com.mosty.base.utils.CameraPreviewUtil;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.token.JwtSysUser;
import com.mosty.base.model.dto.yszx.TbYsSxtDto;
import com.mosty.base.model.entity.yszx.TbYsSxt;
import com.mosty.base.model.query.yszx.TbYsSxtNoPageQuery;
import com.mosty.base.model.query.yszx.TbYsSxtQuery;
import com.mosty.yszx.service.TbYsSxtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dw
 * @since 2022/7/18
 * 要素中心-摄像头接口
 **/
@Api(tags = "要素中心-摄像头接口")
@RestController
@AllArgsConstructor
@RequestMapping("/tbYsSxt")
    public class TbYsSxtController {

    private final TbYsSxtService tbYsSxtService;

    @ApiOperation("摄像头新增")
    @Log(title = "摄像头新增", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public ResponseResult<Integer> insertEntity(@RequestBody TbYsSxtDto dto) {
        return ResponseResult.success(this.tbYsSxtService.insertEntity(dto));
    }

    @ApiOperation("摄像头修改")
    @JwtSysUser
    @Log(title = "摄像头修改", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResponseResult<Void> updateEntity(@RequestBody TbYsSxtDto dto) {
        this.tbYsSxtService.updateEntity(dto);
        return ResponseResult.success();
    }

    @ApiOperation("查询摄像头列表--分页")
    @JwtSysUser
    @GetMapping("getPageList")
    public ResponseResult<IPage<TbYsSxt>> getPageList(TbYsSxtQuery dto) {
        return ResponseResult.success(this.tbYsSxtService.getPageList(dto));
    }

    @ApiOperation("查询摄像头列表--不分页")
    @JwtSysUser
    @GetMapping("getList")
    public ResponseResult<List<TbYsSxt>> getList(TbYsSxtNoPageQuery dto) {
        return ResponseResult.success(this.tbYsSxtService.getList(dto));
    }

    @ApiOperation("查询摄像头详情")
    @JwtSysUser
    @GetMapping("{id}")
    public ResponseResult<TbYsSxt> getList(@PathVariable("id") String id) {
        return ResponseResult.success(this.tbYsSxtService.getInfo(id));
    }

    @ApiOperation("根据设备编号查询摄像头详情")
    @JwtSysUser
    @GetMapping("getInfoBySbbh/{sbbh}")
    public ResponseResult<TbYsSxtDto> getInfoBySbbh(@PathVariable("sbbh") String sbbh) {
        return ResponseResult.success(this.tbYsSxtService.getInfoBySbbh(sbbh));
    }

    @ApiOperation("删除摄像头信息")
    @JwtSysUser
    @DeleteMapping("{id}")
    public ResponseResult<TbYsSxt> delEntity(@PathVariable("id") String id) {
        this.tbYsSxtService.delEntity(id);
        return ResponseResult.success();
    }

    @ApiOperation("查询摄像头-事件中心")
    @JwtSysUser
    @Log(title = "查询摄像头-事件中心", businessType = BusinessType.OTHER)
    @GetMapping("getSjList")
    public ResponseResult<List<TbYsSxt>> getSjList(String jd, String wd, String sjid) {
        return ResponseResult.success(this.tbYsSxtService.getSjList(jd, wd, sjid));
    }

    @ApiOperation("获取所有的感知源的设备编号")
    @JwtSysUser
    @PostMapping("getSxtBhList")
    public ResponseResult<List<String>> getSxtBhList(@RequestBody List<String> ids) {
        return ResponseResult.success(this.tbYsSxtService.getSxtBhList(ids));
    }

    @ApiOperation("添加泸州感知源数据")
    @JwtSysUser
    @PostMapping("addLzGzy")
    public ResponseResult<Void> addLzGzy(@RequestBody ZhjmxfXfqwXfqyGzdw dto) {
        this.tbYsSxtService.addLzGzy(dto);
        return ResponseResult.success();
    }

    @ApiOperation("查询感知源信息")
    @JwtSysUser
    @PostMapping("selectGzy")
    public ResponseResult<TbYsSxtDto> selectGzy(@RequestBody TbYsSxtPageOneQuery dto) {
        return ResponseResult.success(this.tbYsSxtService.selectGzy(dto));
    }

    @ApiOperation("摄像头更新经纬度")
    @JwtSysUser
    @GetMapping("updateJwd")
    public ResponseResult<Void> updateJwd() {
        this.tbYsSxtService.updateJwd();
        return ResponseResult.success();
    }



    @ApiOperation("更新泸州感知源数据")
    @Log(title = "更新泸州感知源数据", businessType = BusinessType.OTHER)
    @GetMapping("updateLzGzy")
    @JwtSysUser
    public ResponseResult<String> updateLzGzy() {
        this.tbYsSxtService.updateLzGzy();
        return ResponseResult.success();
    }


    @ApiOperation("获取HLS地址")
    @Log(title = "查询HLS地址", businessType = BusinessType.OTHER)
    @GetMapping("previewURLs")
    @JwtSysUser
    @ApiImplicitParams({@ApiImplicitParam(name = "sbbh", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "protocol", value = "协议类型( 0-udp，1-tcp),默认为tcp，在protocol设置为rtsp或者rtmp时有效", dataType = "String"),
    })
    public ResponseResult<String> previewURLs(String sbbh,String protocol) {
        if(StringUtils.isBlank(protocol)){
            protocol = "hls";
        }
        String getCamsApi = "/api/video/v1/cameras/previewURLs";
        String result = "";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", sbbh);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", protocol);
        jsonBody.put("transmode", 0);
        jsonBody.put("expand", "transcode=0");
        result = CameraPreviewUtil.getCameraPreviewURL(getCamsApi, jsonBody);
        return ResponseResult.success(result);
    }


    @ApiOperation("获取监控点回放取流URL")
    @Log(title = "获取监控点回放取流URL", businessType = BusinessType.OTHER)
    @PatchMapping("playbackURLs")
    @JwtSysUser
    @ApiImplicitParams({@ApiImplicitParam(name = "sbbh", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "beginTime", value = "开始查询时间（ISO8601格式yyyy-MM-dd'T'HH:mm:ss.SSSzzz，和结束时间相差不超过三天）", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束查询时间（ISO8601格式yyyy-MM-dd'T'HH:mm:ss.SSSzzz，和开始时间相差不超过三天）", dataType = "String"),
            @ApiImplicitParam(name = "protocol", value = "协议类型( 0-udp，1-tcp),默认为tcp，在protocol设置为rtsp或者rtmp时有效", dataType = "String"),
    })
    public ResponseResult<String> playbackURLs(String sbbh,String beginTime,String endTime,String protocol) {
        if(StringUtils.isBlank(protocol)){
            protocol = "hls";
        }
        String getCamsApi = "/api/video/v1/cameras/playbackURLs";
        String result = "";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", sbbh);
        jsonBody.put("beginTime",beginTime+"+08:00");
        jsonBody.put("endTime",endTime+"+08:00");
        jsonBody.put("recordLocation", 0);
        jsonBody.put("protocol", protocol);
        jsonBody.put("needReturnClipInfo", true);
        jsonBody.put("expand", "transcode=0");
        result = CameraPreviewUtil.getCameraPreviewURL(getCamsApi, jsonBody);
        return ResponseResult.success(result);
    }

    @ApiOperation("根据监控点编号进行云台操作")
    @Log(title = "根据监控点编号进行云台操作", businessType = BusinessType.OTHER)
    @PatchMapping("controlling")
    @JwtSysUser
    @ApiImplicitParams({@ApiImplicitParam(name = "sbbh", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(name = "action", value = "开始或停止操作(0 开始 1 停止)", dataType = "Integer"),
            @ApiImplicitParam(name = "command", value = "控制命令(不区分大小写) 说明： LEFT 左转 RIGHT 右转 UP 上转 DOWN 下转 ZOOM_IN 焦距变大 ZOOM_OUT 焦距变小 LEFT_UP 左上 LEFT_DOWN 左下 RIGHT_UP 右上 RIGHT_DOWN 右下 FOCUS_NEAR 焦点前移 FOCUS_FAR 焦点后移 IRIS_ENLARGE 光圈扩大 IRIS_REDUCE 光圈缩小 以下命令presetIndex不可为空： GOTO_PRESET到预置点", dataType = "String"),
            @ApiImplicitParam(name = "speed", value = "云台速度(取值范围1-100,默认40)", dataType = "Integer"),
            @ApiImplicitParam(name = "presetIndex", value = "预置点编号(取值范围为1-128)", dataType = "Integer"),
    })
    public ResponseResult<String> controlling(String sbbh,Integer action,String command,Integer speed,Integer presetIndex) {
        String getCamsApi = "/api/video/v1/ptzs/controlling";
        String result = "";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", sbbh);
        jsonBody.put("action", action);
        jsonBody.put("command", command);
        jsonBody.put("speed", speed);
        jsonBody.put("presetIndex", presetIndex);
        result = CameraPreviewUtil.getCameraPreviewURL(getCamsApi, jsonBody);
        return ResponseResult.success(result);
    }

}
