package com.fs.live.controller;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.BaseEntity;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.PageRequest;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.bean.BeanUtils;
import com.fs.common.vo.LiveVo;
import com.fs.live.domain.Live;
import com.fs.live.domain.LiveMsg;
import com.fs.live.domain.LiveVideo;
import com.fs.live.service.ILiveGoodsService;
import com.fs.live.service.ILiveMsgService;
import com.fs.live.service.ILiveService;
import com.fs.live.service.ILiveVideoService;
import com.fs.live.websocket.bean.SendMsgVo;
import com.fs.live.websocket.service.WebSocketServer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;


@Api("直播信息接口")
@RestController
@AllArgsConstructor
@RequestMapping(value="/ws/app/live")
public class LiveController {

	private static final Logger log = LoggerFactory.getLogger(LiveController.class);
	private final ILiveService liveService;
	private final ILiveMsgService liveMsgService;
	@Autowired
	private ILiveVideoService videoService;
	@Autowired
	private WebSocketServer webSocketServer;


	// 直播开播回调
	@PostMapping("/startLiving")
	public R checkLiving(HttpServletRequest request, @RequestBody  Map<String, String> params) {
		log.info("请求参数：{}", params);
		SendMsgVo sendMsgVo = new SendMsgVo();
		sendMsgVo.setMsg("开始直播");
		sendMsgVo.setCmd("live_start");
		webSocketServer.broadcastMessage(Long.valueOf(params.get("stream_id")), JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
		return R.ok();
//		{app=200149.push.tlivecloud.com, appid=1319721001, appname=live, channel_id=667, errcode=0,
//		errmsg=, event_time=1753840982, event_type=1,
//		height=1080, idc_id=38, node=113.249.151.16,
//		sequence=702115889072680959, set_id=2, stream_id=667,
//		stream_param=txSecret=6E89AECCA08DBEE82E7F5870BCED7132&txTime=688ACD21, user_ip=125.80.218.101, width=1920}
	}

	// 直播结束回调
	@PostMapping("/endLiving")
	public R endLiving(HttpServletRequest request, @RequestBody  Map<String, String> params) {
		log.info("请求参数：{}", params);
		SendMsgVo sendMsgVo = new SendMsgVo();
		sendMsgVo.setMsg("结束直播");
		sendMsgVo.setCmd("live_end");
		webSocketServer.broadcastMessage(Long.valueOf(params.get("stream_id")), JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
		Live live = new Live();
		live.setLiveId(Long.valueOf(params.get("stream_id")));
		live.setStatus(3);
		live.setFinishTime(LocalDateTime.now());
		liveService.updateLiveEntity(live);
		return R.ok();
//		{app=200149.push.tlivecloud.com, appid=1319721001, appname=live, channel_id=673,
//				errcode=1, errmsg=The push client actively stopped the push, event_time=1755571239,
//				event_type=0, height=1080, idc_id=38, node=113.250.23.118, push_duration=1051237,
//				sequence=721865018844564968, set_id=2, stream_id=673,
//				stream_param=txSecret=A3EF362C9484D3D091C2E9B08C2C08CB&txTime=68A53145,
//				user_ip=113.248.98.28, width=1920}

	}

	// 直播录制文件回调 腾讯云 cos桶 fs
	@PostMapping("/liveReplayFile")
	public R liveReplayFile(HttpServletRequest request, @RequestBody  Map<String, String> params) {
		log.info("请求参数：{}", params);
		Long liveId = Long.valueOf(params.get("stream_id"));
		LiveVideo liveVideo = new LiveVideo();
		LiveVideo exist = videoService.selectLiveVideoByLiveId(liveId);
		if (exist != null) {
			liveVideo = exist;
		}
		Date nowDate = DateUtils.getNowDate();
		liveVideo.setLiveId(liveId);
		liveVideo.setVideoUrl(params.get("video_url"));
		liveVideo.setVideoType(2);
		liveVideo.setDuration(Long.valueOf(params.get("duration")));
		liveVideo.setCreateTime(nowDate);
		liveVideo.setUpdateTime(nowDate);
		liveVideo.setFileSize(Long.valueOf(params.get("file_size")));
		videoService.saveOrUpdate(liveVideo);
		return R.ok();
//		{app=200149.push.tlivecloud.com, appid=1319721001, appname=live,
//				callback_ext={"video_codec":"h264","session_id":"1755326625589574166","resolution":"1920x1080"},
//			channel_id=774, duration=137, end_time=1757387736, end_time_usec=476103, event_type=100, file_format=mp4,
//					file_id=1319721001_d69054948de44655b465aaf725cafc1c, file_size=45806289, media_start_time=80, record_bps=0,
//				record_file_id=1319721001_d69054948de44655b465aaf725cafc1c, record_temp_id=1595756, start_time=1757387600, start_time_usec=942579,
//				stream_id=774, stream_param=txSecret=CBF1E86FB1AD58B9CC25941F6B9DA854&txTime=68C0E304, task_id=1755326625589574166, video_id=1319721001_05396a7c47914b40aa9ffefcb0ea4626,
//				video_url=http://fs-1319721001.cos.ap-chongqing.myqcloud.com/origin/200149.push.tlivecloud.com/live/774/1755326625589574166-6a9899ea95be4e89be0eeb30cf458579/2025-09-09-11-13-20.mp4}

	}

	@PostMapping("/videoUpload")
	public R videoUpload(HttpServletRequest request, @RequestBody  Map<String, Object> params) {
		log.info("请求参数：{}", params);
		if(!params.containsKey("WorkflowExecution")) return R.error("参数错误");

		LinkedHashMap<String,Object> result = (LinkedHashMap<String,Object>) params.get("WorkflowExecution");
		String string = result.get("Object").toString();
		videoService.updateFinishStatus(string.replace(".mp4", ".m3u8"));

		return R.ok();
//		{app=200149.push.tlivecloud.com, appid=1319721001, appname=live, channel_id=673,
//				errcode=1, errmsg=The push client actively stopped the push, event_time=1755571239,
//				event_type=0, height=1080, idc_id=38, node=113.250.23.118, push_duration=1051237,
//				sequence=721865018844564968, set_id=2, stream_id=673,
//				stream_param=txSecret=A3EF362C9484D3D091C2E9B08C2C08CB&txTime=68A53145,
//				user_ip=113.248.98.28, width=1920}

	}



}
