package com.yfd.monitor.vmanager.gdw2019.ptz;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yfd.monitor.conf.exception.ControllerException;
import com.yfd.monitor.gdw2019.bean.Device;
import com.yfd.monitor.gdw2019.transmit.callback.DeferredResultHolder;
import com.yfd.monitor.gdw2019.transmit.callback.RequestMessage;
import com.yfd.monitor.gdw2019.transmit.cmd.impl.SIPCommander;
import com.yfd.monitor.storager.IVideoManagerStorage;
import com.yfd.monitor.vmanager.bean.ErrorCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import javax.sip.InvalidArgumentException;
import javax.sip.SipException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Tag(name  = "云台控制")

@RestController
@RequestMapping("/api/ptz")
public class PtzController {

	private final static Logger logger = LoggerFactory.getLogger(PtzController.class);

	@Autowired
	private SIPCommander cmder;

	@Autowired
	private IVideoManagerStorage storager;

	@Autowired
	private DeferredResultHolder resultHolder;

	/***
	 * 云台控制
	 * @param deviceId 设备id
	 * @param channelId 通道id
	 * @param command	控制指令
	 * @param horizonSpeed	水平移动速度
	 * @param verticalSpeed	垂直移动速度
	 * @param zoomSpeed	    缩放速度
	 */

	@Operation(summary = "云台控制")
	@Parameter(name = "deviceId", description = "设备国标编号", required = true)
	@Parameter(name = "channelId", description = "通道国标编号", required = true)
	@Parameter(name = "command", description = "控制指令,允许值: left, right, up, down, upleft, upright, downleft, downright, zoomin, zoomout, stop", required = true)
	@Parameter(name = "horizonSpeed", description = "水平速度", required = true)
	@Parameter(name = "verticalSpeed", description = "垂直速度", required = true)
	@Parameter(name = "zoomSpeed", description = "缩放速度", required = true)
	@PostMapping("/control/{deviceId}/{channelId}")
	public void ptz(@PathVariable String deviceId,@PathVariable String channelId, String command, int horizonSpeed, int verticalSpeed, int zoomSpeed){
		JSONObject jobj=getPanInfo(deviceId,channelId);  //统一处理了一下，如果红外摄像头，取了可见光摄像头的设备编号和通道编号
		deviceId=jobj.getStr("deviceId");
		channelId=jobj.getStr("channelId");
		if (logger.isDebugEnabled()) {
			logger.debug(String.format("设备云台控制 API调用，deviceId：%s ，channelId：%s ，command：%s ，horizonSpeed：%d ，verticalSpeed：%d ，zoomSpeed：%d",deviceId, channelId, command, horizonSpeed, verticalSpeed, zoomSpeed));
		}
		Device device = storager.queryVideoDevice(deviceId);
		int cmdCode = 0;
		switch (command){
			case "left":
				cmdCode = 2;
				break;
			case "right":
				cmdCode = 1;
				break;
			case "up":
				cmdCode = 8;
				break;
			case "down":
				cmdCode = 4;
				break;
			case "upleft":
				cmdCode = 10;
				break;
			case "upright":
				cmdCode = 9;
				break;
			case "downleft":
				cmdCode = 6;
				break;
			case "downright":
				cmdCode = 5;
				break;
			case "zoomin":
				cmdCode = 16;
				break;
			case "zoomout":
				cmdCode = 32;
				break;
			case "stop":
				break;
			default:
				break;
		}
		try {
			cmder.frontEndCmd(device, channelId, cmdCode, horizonSpeed, verticalSpeed, zoomSpeed);
		} catch (SipException | InvalidArgumentException | ParseException e) {
			logger.error("[命令发送失败] 云台控制: {}", e.getMessage());
			throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage());
		}
	}


	@Operation(summary = "通用前端控制命令")
	@Parameter(name = "deviceId", description = "设备国标编号", required = true)
	@Parameter(name = "channelId", description = "通道国标编号", required = true)
	@Parameter(name = "cmdCode", description = "指令码", required = true)
	@Parameter(name = "parameter1", description = "数据一", required = true)
	@Parameter(name = "parameter2", description = "数据二", required = true)
	@Parameter(name = "combindCode2", description = "组合码二", required = true)
	@PostMapping("/front_end_command/{deviceId}/{channelId}")
	public void frontEndCommand(@PathVariable String deviceId,@PathVariable String channelId,int cmdCode, int parameter1, int parameter2, int combindCode2){
		JSONObject jobj=getPanInfo(deviceId,channelId);  //统一处理了一下，如果红外摄像头，取了可见光摄像头的设备编号和通道编号
		deviceId=jobj.getStr("deviceId");
		channelId=jobj.getStr("channelId");
		if (logger.isDebugEnabled()) {
			logger.debug(String.format("设备云台控制 API调用，deviceId：%s ，channelId：%s ，cmdCode：%d parameter1：%d parameter2：%d",deviceId, channelId, cmdCode, parameter1, parameter2));
		}
		Device device = storager.queryVideoDevice(deviceId);

		try {
			cmder.frontEndCmd(device, channelId, cmdCode, parameter1, parameter2, combindCode2);
            if(cmdCode==130&&parameter1==0&&combindCode2==0){
                storager.updatePatroldeviceTime(deviceId,String.valueOf(parameter2));
            }
		} catch (SipException | InvalidArgumentException | ParseException e) {
			logger.error("[命令发送失败] 前端控制: {}", e.getMessage());
			throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage());
		}
	}

    @Parameter(name = "deviceId", description = "设备国标编号", required = true)
    @Parameter(name = "channelId", description = "通道国标编号", required = true)
    @Parameter(name = "prepos", description = "预置位号", required = true)
    @PostMapping("/callDevicePrePos")
    public void callDevicePrePos (String deviceId,String channelId,String prepos){
		JSONObject jobj=getPanInfo(deviceId,channelId);  //统一处理了一下，如果红外摄像头，取了可见光摄像头的设备编号和通道编号
		deviceId=jobj.getStr("deviceId");
		channelId=jobj.getStr("channelId");

        Device device = storager.queryVideoDevice(deviceId);
       try {
            cmder.frontEndCmd(device, channelId, 130, 0, Integer.parseInt(prepos),0);
            storager.updatePatroldeviceTime(deviceId,prepos);
       } catch (SipException | InvalidArgumentException | ParseException e) {
            logger.error("[命令发送失败] 前端控制: {}", e.getMessage());
            throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage());
        }
    }

	@Operation(summary = "预置位查询")
	@Parameter(name = "deviceId", description = "设备国标编号", required = true)
	@Parameter(name = "channelId", description = "通道国标编号", required = true)
	@GetMapping("/preset/query/{deviceId}/{channelId}")
	public DeferredResult<String> presetQueryApi(@PathVariable String deviceId, @PathVariable String channelId) {
		if (logger.isDebugEnabled()) {
			logger.debug("设备预置位查询API调用");
		}
		Device device = storager.queryVideoDevice(deviceId);
		String uuid =  UUID.randomUUID().toString();
		String key =  DeferredResultHolder.CALLBACK_CMD_PRESETQUERY + (ObjectUtils.isEmpty(channelId) ? deviceId : channelId);
		DeferredResult<String> result = new DeferredResult<String> (3 * 1000L);
		result.onTimeout(()->{
			logger.warn(String.format("获取设备预置位超时"));
			// 释放rtpserver
			RequestMessage msg = new RequestMessage();
			msg.setId(uuid);
			msg.setKey(key);
			msg.setData("获取设备预置位超时");
			resultHolder.invokeResult(msg);
		});
		if (resultHolder.exist(key, null)) {
			return result;
		}
		resultHolder.put(key, uuid, result);
		try {
			cmder.presetQuery(device, channelId, event -> {
				RequestMessage msg = new RequestMessage();
				msg.setId(uuid);
				msg.setKey(key);
				msg.setData(String.format("获取设备预置位失败，错误码： %s, %s", event.statusCode, event.msg));
				resultHolder.invokeResult(msg);
			});
		} catch (InvalidArgumentException | SipException | ParseException e) {
			logger.error("[命令发送失败] 获取设备预置位: {}", e.getMessage());
			throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage());
		}
		return result;
	}

	private JSONObject getPanInfo(String deviceId, String channelId) {
		JSONObject  jsonObject=new JSONObject();
		jsonObject.putOpt("deviceId",deviceId);
		jsonObject.putOpt("channelId",channelId);
		List<Map> list =storager.getRiisPatrolDeviceByCode(deviceId);
		if(list.size()>0){
			Map map=list.get(0);
			String channelinfo=map.get("channelinfo").toString();
			if (JSONUtil.isTypeJSONArray(channelinfo)){
				JSONArray jarray= JSONUtil.parseArray(channelinfo);
				for (int i = 0; i < jarray.size(); i++) {
					JSONObject jobj=jarray.getJSONObject(i);
					if(jobj.getStr("channel_type").equals("2")){ //红外
						if(ObjUtil.isNotEmpty(jobj.getStr("pan_code"))){
							jsonObject.putOpt("deviceId",jobj.getStr("pan_code"));
							jsonObject.putOpt("channelId",jobj.getStr("pan_channelcode"));
						}
					}
				}
			}
		}
		return  jsonObject;
	}

}
