package com.platform.modules.app.controller;

import com.platform.modules.live.service.LiveChannelService;
import com.platform.modules.live.vo.AppSceneEntityVo;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.plugins.Page;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.modules.app.annotation.AppPermission;
import com.platform.modules.app.annotation.Login;
import com.platform.modules.live.constants.SceneConstants;
import com.platform.modules.live.entity.LiveSceneEntity;
import com.platform.modules.live.service.LiveCommentService;
import com.platform.modules.live.service.LiveSceneLiveInfoService;
import com.platform.modules.live.service.LiveSceneService;
import com.platform.modules.live.vo.LiveCommentAppVo;
import com.platform.modules.live.vo.SceneCommentCountVo;

import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * APP现场接口
 *
 * @author tong
 */
@RestController
@RequestMapping("/app/live/scene")
@Api(value = "APP现场模块接口", tags = "APP现场模块接口")
public class AppLiveSceneController extends AppAbstractController {

	@Autowired
	private LiveSceneService liveSceneService;

	@Autowired
	private LiveSceneLiveInfoService liveSceneLiveInfoService;

	@Autowired
	private LiveCommentService liveCommentService;

	@Autowired
	private LiveChannelService liveChannelService;

	/**
	 * 保存
	 */
	@Login
	@PostMapping("/save")
	@ApiOperation(value = "现场保存接口")
	@ApiImplicitParams({@ApiImplicitParam(value = "记者id", name = "userId", paramType = "query",  dataType = "Integer"),
//			@ApiImplicitParam(value = "栏目ID", name = "channelIdList", dataType = "int", allowMultiple = true),
//			@ApiImplicitParam(value = "栏目名称", name = "channelNameList", dataType = "String", allowMultiple = true),
//			@ApiImplicitParam(value = "直播记者ID", name = "userIdList", dataType = "int", allowMultiple = true),
			@ApiImplicitParam(value = "标题图", name = "titleFile", dataType = "file", paramType = "query"),
			@ApiImplicitParam(value = "视频文件", name = "videoFile", dataType = "file", paramType = "query")})
	public R save(@ModelAttribute AppSceneEntityVo liveScene, Integer userId,
//			List<Integer> channelIdList, List<String> channelNameList, List<Integer> userIdList,
				  MultipartFile titleFile, MultipartFile videoFile) {

		if (userId == null || null == liveScene || liveScene.getLiveEnd() == null
				|| liveScene.getLiveStart() == null || StringUtils.isBlank(liveScene.getLivePosition())
				|| StringUtils.isBlank(liveScene.getTitle()) || liveScene.getSceneType() == null) {
			return R.error("现场保存失败，缺少必要参数");
		}
		liveScene.setCreatorId(userId);
		return liveSceneService.save(liveScene, titleFile, videoFile);
	}

	/**
	 * 修改
	 */
	@Login
	@PostMapping("/update")
	@ApiOperation(value = "现场更新接口")
	@ApiImplicitParams({@ApiImplicitParam(value = "记者id", name = "userId", paramType = "query",  dataType = "Integer"),
//			@ApiImplicitParam(value = "栏目ID", name = "channelIdList", dataType = "int", allowMultiple = true),
//			@ApiImplicitParam(value = "栏目名称", name = "channelNameList", dataType = "String", allowMultiple = true),
//			@ApiImplicitParam(value = "直播记者ID", name = "userIdList", dataType = "int", allowMultiple = true),
			@ApiImplicitParam(value = "标题图", name = "titleFile", dataType = "file"),
			@ApiImplicitParam(value = "视频文件", name = "videoFile", dataType = "file")})
	public R update(@ModelAttribute AppSceneEntityVo liveScene, Integer userId,
					@RequestParam(value = "titleFile", required = false) MultipartFile titleFile, @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) {

		if (userId == null || null == liveScene || liveScene.getId() == null || liveScene.getLiveEnd() == null
				|| liveScene.getLiveStart() == null || StringUtils.isBlank(liveScene.getLivePosition())
				|| StringUtils.isBlank(liveScene.getTitle()) || liveScene.getSceneType() == null) {
			return R.error("现场更新失败，缺少必要参数");
		}

		LiveSceneEntity entity = liveSceneService.selectById(liveScene.getId());

		if(entity == null || !SceneConstants.SCENE_DRAFT.equals(entity.getSceneState())) {
			return R.error("未找到现场或现场为非草稿状态");
		}
		liveScene.setUpdatorId(userId);
		return liveSceneService.update(liveScene, entity, titleFile, videoFile);
	}

	@Login
	@GetMapping("/channelList")
	@ApiOperation("APP获取栏目列表")
	public R channelList() {
		return R.ok().put("data", liveChannelService.getAllList());
	}

	@Login
	@GetMapping("/reporterList")
	@ApiOperation("APP获取记者列表")
	public R reporterList() {
		return R.ok().put("list", liveSceneService.reporterList(null));
	}

	@Login
	@GetMapping("/detail")
	@ApiOperation("APP获取现场详情")
	@ApiImplicitParams({@ApiImplicitParam(value = "现场id", name = "sceneId", paramType = "query", dataType = "int")})
	public R detail(Integer sceneId) {

		if (null == sceneId) {
			return R.error("参数错误");
		}
		return R.ok().put("scene", liveSceneService.info(sceneId));
	}

	@Login
	@GetMapping("/page")
	@ApiOperation("现场分页数据")
	@ApiImplicitParams({@ApiImplicitParam(value = "标题", name = "title", paramType = "query", dataType = "String"),
			@ApiImplicitParam(value = "记者id", name = "userId", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "现场类型，1:视频现场,2:音频现场,3:图片现场,4:文字现场,5:混合现场,6:精品现场", name = "sceneType", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "现场状态，draft：草稿；check：待审；prod：成品", name = "ltype", paramType = "query", dataType = "string"),
			@ApiImplicitParam(value = "页码", name = "pageNo", defaultValue = "1", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "页面大小", name = "pageSize", defaultValue = "15", paramType = "query", dataType = "int")})
	public R page(String title, Integer sceneType, Integer userId, String ltype, Integer pageNo, Integer pageSize) {
		// Page page = liveSceneService.page(title, sceneType, ltype,
		// null==pageNo?1:pageNo, null==pageSize?15:pageSize);
		if (userId == null) {
			return R.error("记者Id不能为空");
		}
		PageUtils page = liveSceneService.page(title, sceneType, userId, ltype, null == pageNo ? 1 : pageNo,null == pageSize ? 15 : pageSize);
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("/start")
	@ApiOperation("开播")
	@ApiImplicitParams({@ApiImplicitParam(value = "现场id", name = "sceneId", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "记者id", name = "userId", paramType = "query", dataType = "int")})
	public R start(Integer userId, Integer sceneId) {
		return liveSceneLiveInfoService.startScenePush(userId, sceneId);
	}

	@Login
	@PostMapping("/stop")
	@ApiOperation("关播")
	@ApiImplicitParams({@ApiImplicitParam(value = "现场id", name = "sceneId", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "记者id", name = "userId", paramType = "query", dataType = "int")})
	public R stop(Integer userId, Integer sceneId) {
		return liveSceneLiveInfoService.stopScenePush(userId, sceneId);
	}

	@Login
	@GetMapping("/request/{sceneId}/state")
	@ApiOperation("获取现场是否允许开播")
	public R getStartPermission(@PathVariable("sceneId") Integer sceneId) {
		LiveSceneEntity sceneEntity = liveSceneService.selectById(sceneId);
		if (sceneEntity == null) {
			return R.error("错误的现场id");
		}
		Integer liveState = sceneEntity.getLiveState();
		if (liveState.equals(SceneConstants.LIVE_STATE_REVIEW)) {
			return R.ok().put("allowStart", false);
		} else {
			return R.ok().put("allowStart", true);
		}
	}

	/**
	 * app现场审核列表
	 */
	@Login
	@AppPermission(permision = "app:live:scene:page")
	@ApiOperation("app现场审核列表")
	@GetMapping("/check/page")
	@ApiImplicitParams({@ApiImplicitParam(value = "标题", name = "title", paramType = "query", dataType = "String"),
			@ApiImplicitParam(value = "现场类型，1:视频现场,2:音频现场,3:图片现场,4:文字现场,5:混合现场,6:精品现场", name = "sceneType", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "直播状态，0：预告；1：正在直播；2回顾", name = "liveState", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(value = "页码", name = "pageNo", defaultValue = "1", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "页面大小", name = "pageSize", defaultValue = "15", paramType = "query", dataType = "int")})
	@ApiResponses({
			@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveSceneEntity.class, responseContainer = "List")})
	public R checkPage(String title, Integer sceneType, Integer liveState, Integer pageNo, Integer pageSize, Long tenantId) {
		Page page = liveSceneService.page(title, sceneType, "check", liveState, null == pageNo ? 1 : pageNo,null == pageSize ? 15 : pageSize, tenantId);
		return R.ok().put("page", page);
	}

	/**
	 * 现场审核
	 */
	@PostMapping("/check")
	@Login
	@AppPermission(permision = "app:live:scene:check")
	@ApiOperation("现场审核接口")
	@ApiImplicitParams({@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "审核状态，0:不通过;1:通过", name = "pass", paramType = "query", dataType = "int")})
	public R check(Integer id, Integer pass) {
		if (null == id) {
			return R.error("参数错误");
		}
		try {
			liveSceneService.check(id, pass, getUser());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统内部错误");
		}
		return R.ok("审核完成");
	}
	/**
	 * app现场核列表-评论数
	 */
	@Login
	@AppPermission(permision = "app:live:scene:comment:page")
	@ApiOperation("app现场核列表-评论数")
	@GetMapping("/comment/count/page")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", defaultValue = "1", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "limit", value = "显示行数", defaultValue = "15", paramType = "query", dataType = "Integer")})
	@ApiResponses({
			@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = SceneCommentCountVo.class, responseContainer = "List")})
	public R SceneCommentCountPage(Integer page, Integer limit) {
		Page<SceneCommentCountVo> sceneCommentCountVoPage = liveSceneService
				.querySceneCommentPage(null == page ? 1 : page, null == limit ? 15 : limit);
		return R.ok().put("page", sceneCommentCountVoPage);
	}

	/**
	 * app现场评论审核列表
	 */
	@Login
	@AppPermission(permision = "app:live:scene:comment:page")
	@ApiOperation("app现场评论审核列表")
	@GetMapping("/comment/check/page")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", defaultValue = "1", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "limit", value = "显示行数", defaultValue = "15", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "sceneId", value = "现场ID", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "source", value = "评论来源，不填为全选：<br> 1用户 2后台 3第三方", allowableValues = "1,2,3", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "text", value = "评论内容", paramType = "query", dataType = "String")})
	@ApiResponses({
			@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveCommentAppVo.class, responseContainer = "List")})
	public R commentCheckPage(Integer page, Integer limit, Integer sceneId, Integer source, String text) {
		Page<LiveCommentAppVo> commentAppVoPage = liveCommentService.queryCommentCheckPageByParams(
				null == page ? 1 : page, null == limit ? 15 : limit, sceneId, source, text);
		return R.ok().put("page", commentAppVoPage);
	}
	/**
	 * 评论审核
	 */
	@PostMapping("comment/check")
	@Login
	@AppPermission(permision = "app:live:scene:comment:check")
	@ApiOperation("现场审核接口")
	@ApiImplicitParams({@ApiImplicitParam(value = "评论id数组", name = "ids", paramType = "query", dataType = "Integer[]"),
			@ApiImplicitParam(value = "审核状态，0:不通过;1:通过；2：撤销同意", name = "pass", paramType = "query", dataType = "int")})
	public R commentCheck(Integer[] ids, Integer pass) {
		if (ids == null || ids.length == 0) {
			return R.error("评论ID不能为空");
		}
		if (pass == 1) {
			liveCommentService.releaseBatch(ids, getUserId());
		}
		if (pass == 0) {
			liveCommentService.deleteBatch(ids, getUserId());
		}
		if (pass == 2) {
			liveCommentService.cancelReleaseBatch(ids, getUserId());
		}
		return R.ok("审核完成");
	}
}
