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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
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.log.logger.BladeLogger;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/wvp/playback")
public class RecordController {

	@Autowired
	private WvpConfig wvpConfig;

	@Autowired
	private BladeLogger bladeLogger;

	@ApiOperation("开始视频回放")
	@ApiOperationSupport(order = 1)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
		@ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
		@ApiImplicitParam(name = "startTime", value = "开始时间", dataTypeClass = String.class),
		@ApiImplicitParam(name = "endTime", value = "结束时间", dataTypeClass = String.class),
	})
	@GetMapping(value = "/start/{deviceId}/{channelId}")
	public R play(@PathVariable String deviceId, @PathVariable String channelId,
				 String startTime,String endTime){
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		String reqUrl = String.format("/api/playback/start/%s/%s", deviceId, channelId);
		Map<String, Object> params = new HashMap<>();
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		HttpResponse response = HttpRequest.get(wvpUrl + reqUrl).form(params).execute();
		bladeLogger.info("【wvp】playback-start", "【requestParam】：" + null + "【response】：" + response.body());
		System.out.println("【wvp】playback-start" + response.body());
		JSONObject result = JSONObject.parseObject(response.body());
		return R.data(result);
	}

	@ApiOperation("停止视频回放")
	@ApiOperationSupport(order = 2)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
		@ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
	})
	@GetMapping(value = "/stopPlay/{deviceId}/{channelId}")
	public R playStop(@PathVariable String deviceId, @PathVariable String channelId){
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		String reqUrl = String.format("/api/playback/stop/%s/%s", deviceId, channelId);
		HttpResponse response = HttpRequest.get(wvpUrl + reqUrl).execute();
		bladeLogger.info("【wvp】playback-stop", "【requestParam】：" + reqUrl + "【response】：" + response.body());
		int status = response.getStatus();
		if(status == 200){
			return R.success("停止点播成功");
		}else{
			return R.fail("操作失败");
		}
	}
}
