package com.platform.modules.live.controller;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.annotation.SysLog;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.modules.live.constants.SceneConstants;
import com.platform.modules.live.entity.LiveMultimediaEntity;
import com.platform.modules.live.service.LiveMultimediaService;
import com.platform.modules.live.service.LiveMultimediaSyncService;
import com.platform.modules.plm.util.MateriaFileProperties;
import com.platform.modules.plm.util.MaterialUtils;
import com.platform.modules.sys.controller.AbstractController;

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


/**
 * 素材信息
 * 
 * @author weiyan
 * @email 
 * @date 2019-03-12 10:09:03
 */
@RestController
@RequestMapping("/live/multimedia")
@Api(value = "素材管理", tags = "素材管理")
public class LiveMultimediaController extends AbstractController{
	@Autowired
	private LiveMultimediaService liveMultimediaService;
	@Autowired
	private LiveMultimediaSyncService liveMultimediaSyncService;
	@Autowired
	private MateriaFileProperties materiaFileProperties;

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("live:multimedia:list")
	@ApiOperation("素材列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页数", defaultValue = "1", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "limit", value = "显示行数", defaultValue = "10", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "mediaType", value = "素材类型 1视频  2语音  3图片", allowableValues = "1,2,3", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "title", value = "素材标题", paramType = "query", dataType = "String")})
	public R list(@ApiIgnore @RequestParam(required = false) Map<String, Object> params){
		params.put("isDelete", 0);
		PageUtils page =  liveMultimediaService.queryPageByParams(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 列表
	 */
	@GetMapping("/common/list")
//	@RequiresPermissions("live:multimedia:common:list")
	@ApiOperation("弹窗素材列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页数", defaultValue = "1", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "limit", value = "显示行数", defaultValue = "10", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "mediaType", value = "素材类型 1视频  2语音  3图片", allowableValues = "1,2,3", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "title", value = "素材标题", paramType = "query", dataType = "String")})
	public R commonList(@ApiIgnore @RequestParam(required = false) Map<String, Object> params){
		params.put("isDelete", 0);
		// 20190527 TROY 只选择上传成功的素材
		params.put("state", SceneConstants.MediaState.Normal.value());
		PageUtils page =  liveMultimediaService.queryPageByParams(params);
		return R.ok().put("page", page);
	}
	
	@GetMapping("/common/saveToLocal")
	@ApiOperation(value = "保存到本地")
	public R saveToLocal(String videoUrl,String title, HttpServletResponse response) {
		if(StringUtils.isBlank(videoUrl)){
			return R.error("请选择需要下载的资源！");
		}
		liveMultimediaService.saveToLocal(videoUrl,title,response);
		return R.ok();
	}
	
	/**
	 * 信息
	 */
	@GetMapping("/info")
	@RequiresPermissions("live:multimedia:info")
	@ApiOperation("素材信息")
	public R info(@RequestParam("id") Integer id){
		return R.ok().put("info", liveMultimediaService.info(id));
	}
	
	/**
	 * 根据mediaId查询素材信息
	 */
	@GetMapping("/getInfoByMediaId")
//	@RequiresPermissions("live:multimedia:getInfoByMediaId")
	@ApiOperation("根据mediaId查询素材信息")
	public R getInfoByMediaId(@RequestParam("mediaId") String mediaId){
		return R.ok().put("info", liveMultimediaService.getInfoByMediaId(mediaId));
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存素材")
	@PostMapping("/save")
	@RequiresPermissions("live:multimedia:save")
	@ApiOperation("保存素材")
	public R save(@ModelAttribute LiveMultimediaEntity liveMultimedia, MultipartFile mediaFile){
		return R.ok().put("info", liveMultimediaService.save(liveMultimedia, mediaFile, getUserId()));
	}

	/**
	 * 保存
	 */
	@SysLog("保存素材")
	@PostMapping("/saveSync")
	@RequiresPermissions("live:multimedia:save")
	@ApiOperation("保存素材")
	public R save_sync(@ModelAttribute LiveMultimediaEntity liveMultimedia){
		// 视频文件合并
		String uploadFolder = materiaFileProperties.getMateriaPath() + materiaFileProperties.getMediaPath() + "/" +
				MaterialUtils.getDateDir();
		String filename = liveMultimedia.getOriFileName();
		String identifier = liveMultimedia.getOriFileIdentifier();
		String filepath = uploadFolder + "/" + identifier + "/" + filename;
		// 素材保存
		Map<String, Object> data = liveMultimediaService.save(liveMultimedia, getUserId());
		// 线程处理
		liveMultimediaSyncService.uploadMediaSync(identifier, filepath, (Integer) data.get("id"));
		return R.ok().put("info", data);
		
	}

	/**
	 * 修改
	 */
	@SysLog("修改素材")
	@PostMapping("/update")
	@RequiresPermissions("live:multimedia:update")
	@ApiOperation("修改素材")
	public R update(@ModelAttribute LiveMultimediaEntity liveMultimedia){
		liveMultimediaService.update(liveMultimedia,  getUserId());
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除素材")
	@PostMapping("/delete")
	@RequiresPermissions("live:multimedia:delete")
	@ApiOperation("删除素材")
	public R delete(@RequestBody Integer[] ids){
		liveMultimediaService.deleteBatch(ids,getUserId());
		return R.ok();
	}

	/**
	 * 重新转码
	 * */
	@SysLog("重新转码")
	@PostMapping("/reTranscode")
	@RequiresPermissions("live:multimedia:save")
	@ApiOperation("重新转码")
	public R reTranscode(@RequestParam("videoId") String videoId){
		if (StringUtils.isBlank(videoId))
			return R.error("视频id不能为空");
		return liveMultimediaService.reTranscode(videoId) ? R.ok() : R.error("重新转码失败");
	}

}
