package org.springblade.third.videodevice.wvp.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.tool.api.R;
import org.springblade.third.videodevice.gb28181.conf.WvpConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

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

	@Autowired
	private WvpConfig wvpConfig;

	@ApiOperation(value = "云台控制")
	@ApiOperationSupport(order = 1)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
		@ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
		@ApiImplicitParam(name = "cmdCode", value = "指令码", dataTypeClass = Integer.class),
		@ApiImplicitParam(name = "horizonSpeed", value = "水平速度", dataTypeClass = Integer.class),
		@ApiImplicitParam(name = "verticalSpeed", value = "垂直速度", dataTypeClass = Integer.class),
		@ApiImplicitParam(name = "zoomSpeed", value = "缩放速度", dataTypeClass = Integer.class),
	})
	@PostMapping(value = "/control/{deviceId}/{channelId}")
	public R ptz(@PathVariable String deviceId,@PathVariable String channelId, int cmdCode, int horizonSpeed, int verticalSpeed, int zoomSpeed){
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		String reqUrl = String.format("/api/ptz/control/%s/%s", deviceId, channelId);
		Map<String, Object> params = new HashMap<>();
		params.put("cmdCode", cmdCode);
		params.put("horizonSpeed", horizonSpeed);
		params.put("verticalSpeed", verticalSpeed);
		params.put("zoomSpeed", zoomSpeed);
		HttpResponse response = HttpRequest.post(wvpUrl + reqUrl).form(params).execute();
		int status = response.getStatus();
		if(status == 200){
			return R.data(null);
		}else{
			return R.fail(null, "操作失败");
		}
	}

	@ApiOperation(value = "通用前端控制命令")
	@ApiOperationSupport(order = 2)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
		@ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
		@ApiImplicitParam(name = "cmdCode", value = "指令码", dataTypeClass = Integer.class),
		@ApiImplicitParam(name = "parameter1", value = "数据一", dataTypeClass = Integer.class),
		@ApiImplicitParam(name = "parameter2", value = "数据二", dataTypeClass = Integer.class),
		@ApiImplicitParam(name = "combindCode2", value = "组合码二", dataTypeClass = Integer.class),
	})
	@PostMapping(value = "/front_end_command/{deviceId}/{channelId}")
	public R frontEndCommand(@PathVariable String deviceId,@PathVariable String channelId,int cmdCode, int parameter1, int parameter2, int combindCode2){
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		String reqUrl = String.format("/api/ptz/front_end_command/%s/%s", deviceId, channelId);
		Map<String, Object> params = new HashMap<>();
		params.put("cmdCode", cmdCode);
		params.put("parameter1", parameter1);
		params.put("parameter2", parameter2);
		params.put("combindCode2", combindCode2);
		HttpResponse response = HttpRequest.post(wvpUrl + reqUrl).form(params).execute();
		int status = response.getStatus();
		if(status == 200){
			return R.data(null);
		}else{
			return R.fail(null, "操作失败");
		}
	}

	@ApiOperation(value = "预置位查询")
	@ApiOperationSupport(order = 3)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
		@ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
	})
	@PostMapping(value = "/preset/query/{deviceId}/{channelId}")
	public R presetQueryApi(@PathVariable String deviceId, @PathVariable String channelId){
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		String reqUrl = String.format("/api/ptz/preset/query/%s/%s", deviceId, channelId);
		HttpResponse response = HttpRequest.post(wvpUrl + reqUrl).execute();
		int status = response.getStatus();
		if(status == 200){
			return R.data(null);
		}else{
			return R.fail(null, "操作失败");
		}
	}
}
