package com.platform.modules.app.controller;

import java.util.Date;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.modules.app.annotation.Login;
import com.platform.modules.live.entity.LiveReportEntity;
import com.platform.modules.live.service.LiveReportService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import springfox.documentation.annotations.ApiIgnore;

/**
 * APP现场报道接口
 *
 * @author weiyan
 */
@Api(value = "APP现场报道模块接口", tags = "APP现场报道模块接口")
@RestController
@RequestMapping("/app/live/report")
public class AppLiveSceneReportController extends AppAbstractController {

	@Autowired
	private LiveReportService liveReportService;

	@Login
	@GetMapping("/page")
	@ApiOperation("现场报道分页数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", defaultValue = "1", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "limit", value = "显示行数", defaultValue = "10", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "userId", value = "记者id", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "sceneId", value = "现场ID", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "isRelease", value = "报道状态 -1草稿  0 未发布 1发布",  allowableValues = "-1,0,1", paramType = "query",dataType = "Integer"),
			@ApiImplicitParam(name = "reportType", value = "报道类型，不填为全选：<br>报道类型 1视频 2音频 3图文 4文字", allowableValues = "1,2,3,4", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "content", value = "报道内容", paramType = "query", dataType = "String")})
	@ApiResponses({
			@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveReportEntity.class, responseContainer = "List")})
	public R page(@ApiIgnore @RequestParam(required = false) Map<String, Object> params) {
		if (params.get("sceneId") == null) {
			return R.error("现场Id不能为空");
		}
		if (params.get("userId") == null && params.get("isRelease")!=null && params.get("isRelease").equals("-1")) {
			return R.error("用户Id不能为空");
		}
		params.put("isDelete", 0);
		PageUtils page = liveReportService.queryPageByParams(params, true);
		return R.ok().put("page", page);
	}
	@Login
	@PostMapping("/save")
	@ApiOperation("保存现场报道数据")
	@ApiImplicitParams({@ApiImplicitParam(name = "sceneId", value = "现场ID", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "reportDate", value = "报道时间", paramType = "query", dataType = "Date"),
			@ApiImplicitParam(name = "reportType", value = "报道类型，不填为全选：<br>报道类型 1视频 2音频 3图文 4文字", allowableValues = "1,2,3,4", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "fileDuration", value = "音视频时长", paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "fileSize", value = "文件大小", paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "materialFile", value = "视频、音频文件", paramType = "query", dataType = "File"),
			@ApiImplicitParam(name = "imgFiles", value = "图片集", paramType = "query", dataType = "File[]"),
			@ApiImplicitParam(name = "isRelease", value = "报道状态", paramType = "报道状态 -1草稿  0 未发布 1发布", dataType = "Integer"),
			@ApiImplicitParam(name = "content", value = "报道内容", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "relatedReport", value = "相关报道", paramType = "query", dataType = "String")
	})
	@ApiResponses({
			@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveReportEntity.class, responseContainer = "List")})
	public R save(Integer sceneId, Date reportDate, Integer reportType, String content, Integer isRelease,
			Long fileDuration, Long fileSize, MultipartFile materialFile, MultipartFile[] imgFiles, String relatedReport) {
		if (sceneId == null) {
			return R.error("现场Id不能为空");
		}
		if (reportType == null) {
			return R.error("报道类型不能为空");
		}
		Long userId = getUserId();
		LiveReportEntity liveReportEntity = new LiveReportEntity();
		liveReportEntity.setReportDate(reportDate);
		liveReportEntity.setSceneId(sceneId);
		liveReportEntity.setUserId(userId);
		liveReportEntity.setContent(content);
		liveReportEntity.setReportType(reportType);
		liveReportEntity.setIsRelease(isRelease);
		liveReportEntity.setFileDuration(fileDuration);
		liveReportEntity.setFileSize(fileSize);
		// 20200430_Mxy 相关报道
		liveReportEntity.setRelatedReport(relatedReport);

		liveReportService.save(reportType, materialFile, imgFiles, userId, liveReportEntity, userId);
//		reportMediasUpload(reportType, materialFile, imgFiles, userId, liveReportEntity, null);
//
//		liveReportService.save(liveReportEntity, userId);
		return R.ok();
	}

	@Login
	@GetMapping("/info")
	@ApiOperation("现场报道信息")
	@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "报道ID", paramType = "query", dataType = "Integer")})
	@ApiResponses({
			@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveReportEntity.class, responseContainer = "List")})
	public R info(Integer id) {
		if (id == null) {
			return R.error("报道Id不能为空");
		}
		return R.ok().put("info", liveReportService.info(id));
	}

	@Login
	@PostMapping("/update")
	@ApiOperation("修改现场报道")
	@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "现场报道ID", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "sceneId", value = "现场ID", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "reportDate", value = "报道时间", paramType = "query", dataType = "Date"),
			@ApiImplicitParam(name = "reportType", value = "报道类型，不填为全选：<br>报道类型 1视频 2音频 3图文 4文字", allowableValues = "1,2,3,4", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "imgList", value = "已经上传过的图片集", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "materialFile", value = "视频、音频文件", paramType = "query", dataType = "MultipartFile"),
			@ApiImplicitParam(name = "fileDuration", value = "音视频时长", paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "fileSize", value = "文件大小", paramType = "query", dataType = "Long"),
			@ApiImplicitParam(name = "imgFiles", value = "图片集", paramType = "query", dataType = "MultipartFile[]"),
			@ApiImplicitParam(name = "isRelease", value = "报道状态", paramType = "报道状态 -1草稿  0 未发布 1发布", dataType = "Integer"),
			@ApiImplicitParam(name = "content", value = "报道内容", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "imgFilesIndex", value = "图片集数组", paramType = "query", dataType = "Integer[]"),
			@ApiImplicitParam(name = "relatedReport", value = "相关报道", paramType = "query", dataType = "String")
	})
	@ApiResponses({
			@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveReportEntity.class, responseContainer = "List")})
	public R update(Integer id, Integer sceneId, Date reportDate, String imgList, Integer reportType, String content,
			Integer isRelease, Long fileDuration, Long fileSize, MultipartFile materialFile, MultipartFile[] imgFiles,
			Integer[] imgFilesIndex, String relatedReport) {
		if (id == null) {
			return R.error("报道Id不能为空");
		}

		if (reportType == null) {
			return R.error("报道类型不能为空");
		}
		Long userId = getUserId();

		LiveReportEntity liveReportEntity = new LiveReportEntity();
		liveReportEntity.setId(id);
		liveReportEntity.setSceneId(sceneId);
		liveReportEntity.setContent(content);
		liveReportEntity.setReportType(reportType);
		liveReportEntity.setIsRelease(isRelease);
		liveReportEntity.setReportDate(reportDate);
		liveReportEntity.setFileDuration(fileDuration);
		liveReportEntity.setFileSize(fileSize);
		liveReportEntity.setImgList((imgList == null || imgList == "" || imgList.equals("")) ? null : imgList);
		// 20200430_Mxy 现场报道
		liveReportEntity.setRelatedReport(relatedReport);
		try {
			liveReportService.update(reportType, materialFile, imgFiles, userId, liveReportEntity, imgFilesIndex, userId);
//			reportMediasUpload(reportType, materialFile, imgFiles, userId, liveReportEntity, imgFilesIndex);
//
//			liveReportService.update(liveReportEntity, userId);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("报道更新失败");
		}
		return R.ok();
	}



	@Login
	@PostMapping("/delete")
	@ApiOperation("删除现场报道信息")
	@ApiImplicitParams({@ApiImplicitParam(value = "报道Id数组", name = "ids", paramType = "query", dataType = "Integer[]")})
	public R delete(Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return R.error("报道ID不能为空");
		}
		liveReportService.deleteBatch(ids, getUserId());
		return R.ok();
	}
}
