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

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.beans.StreamInfo;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author zwx
 * @create 2021/7/2 14:32
 */
@RestController
@RequestMapping("/wvp/proxy")
public class StreamProxyController {

	@Autowired
	private WvpConfig wvpConfig;
	@Autowired
	private BladeLogger bladeLogger;


	@ApiOperation(value = "移除拉流代理")
	@ApiOperationSupport(order = 3)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "app", value = "应用名", dataTypeClass = String.class),
		@ApiImplicitParam(name = "streamId", value = "流ID", dataTypeClass = String.class),
	})
	@GetMapping(value = "/del")
	public R<Boolean> del(String app, String streamId){
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("app", app);
		paramMap.put("stream", streamId);
		HttpRequest request = new HttpRequest(wvpUrl + "/api/proxy/del");
		HttpResponse response = request.form(paramMap).execute();
		String body = response.body();
		bladeLogger.info("【wvp-gb28181】del", "【requestParam】：" + paramMap + "【response】：" + body);
		JSONObject result = JSONObject.parseObject(body);
		Integer code = result.getInteger("code");
		if(code == 0){
			return R.status(true);
		}else{
			String msg = result.getString("msg");
			return R.fail(msg);
		}
	}

	@ApiOperation(value = "根据应用名和流id获取播放地址")
	@ApiOperationSupport(order = 4)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "app", value = "应用名", dataTypeClass = String.class),
		@ApiImplicitParam(name = "streamId", value = "流ID", dataTypeClass = String.class),
	})
	@GetMapping(value = "/play")
	public R<StreamInfo> play(String app, String streamId) {
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("app", app);
		paramMap.put("stream", streamId);
		HttpRequest request = new HttpRequest(wvpUrl + "/api/media/stream_info_by_app_and_stream");
		HttpResponse response = request.form(paramMap).execute();
		String body = response.body();
		bladeLogger.info("【wvp-gb28181】play", "【requestParam】：" + paramMap + "【response】：" + body);
		if (StringUtils.hasText(body)) {
			StreamInfo streamInfo = JSONObject.parseObject(body, StreamInfo.class);
			return R.data(streamInfo);
		} else {
			return R.data(null);
		}
	}

	@ApiOperation(value = "将不是h264的视频通过ffmpeg 转码为h264 + aac")
	@ApiOperationSupport(order = 5)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "streamId", value = "视频流ID", dataTypeClass = String.class),
	})
	@GetMapping(value = "/convert/{streamId}")
	public R<String> convert(@PathVariable(value = "streamId") String streamId) {
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		HttpRequest post = HttpRequest.post(String.format(wvpUrl + "/api/play/convert/%s", streamId));
		HttpResponse response = post.execute();
		String body = response.body();
		bladeLogger.info("【wvp-gb28181】convert", "【requestParam】：" + streamId + "【response】：" + body);
		return R.data(body);
	}

	@ApiOperation(value = "停止转码")
	@ApiOperationSupport(order = 6)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "streamId", value = "视频流ID", dataTypeClass = String.class),
	})
	@GetMapping(value = "/convertStop/{convertKey}")
	public R<String> convertStop(@PathVariable(value = "convertKey") String convertKey) {
		String wvpUrl = wvpConfig.getServerHost() + ":" + wvpConfig.getServerPort();
		HttpRequest post = HttpRequest.post(String.format(wvpUrl + "/api/play/convertStop/%s", convertKey));
		HttpResponse response = post.execute();
		String body = response.body();
		bladeLogger.info("【wvp-gb28181】convert", "【requestParam】：" + convertKey + "【response】：" + body);
		return R.data(body);
	}
}
