package com.platform.modules.live.controller;

import java.util.Map;

import com.platform.modules.live.aspect.LiveTenant;
import com.platform.modules.live.constants.LiveConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.common.annotation.SysLog;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.modules.live.service.LiveSceneRecordService;
import com.platform.modules.live.vo.LiveSceneRecordVo;

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 TONG
 * @email ${email}
 * @date 2019-02-20 12:40:50
 */
@RestController
@RequestMapping("live/livescenerecord")
@Api(value = "直播回顾接口", tags = "直播回顾接口")
public class LiveSceneRecordController {
    @Autowired
    private LiveSceneRecordService liveSceneRecordService;

    /**
     * 列表
     */
    @GetMapping("/page")
    @RequiresPermissions("live:livescenerecord:page")
    @ApiOperation(value = "回顾分页数据", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "回顾名称", name = "recordName", paramType = "query", dataType = "String"),
            @ApiImplicitParam(value = "状态，0:未审核；1：审核显示", name = "recordStatus", paramType = "query", dataType = "int"),
            @ApiImplicitParam(value = "现场ID", name = "sceneId", paramType = "query", dataType = "int"),
            @ApiImplicitParam(value = "页码", name = "page", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(value = "页面大小", name = "limit", defaultValue = "15",paramType = "query", dataType = "int")})
    public R list(@ApiIgnore @RequestParam(required = false) Map<String, Object> params){
        PageUtils page = liveSceneRecordService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @RequiresPermissions("live:livescenerecord:info")
    @ApiOperation(value = "回顾详情", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "回顾ID", name = "id", paramType = "query", dataType = "int")})
    public R info(@RequestParam("id") Integer id){
		
        return R.ok().put("liveSceneRecord", liveSceneRecordService.info(id));
    }

    /**
     * 保存
     */
    @SysLog("保存现场回顾信息")
    @PostMapping("/save")
    @ApiOperation(value = "保存回顾", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequiresPermissions("live:livescenerecord:save")
    @LiveTenant("sceneId")
    public R save(@ModelAttribute LiveSceneRecordVo liveSceneRecord){
        if (liveSceneRecord == null || liveSceneRecord.getSceneId() == null) {
            return R.error("缺少参数");
        }

        // TODO 20200318_Mxy 暂时默认设置类型为mp4
        liveSceneRecord.setRecordType(LiveConstants.RecordType.MP4_RECORD);

        try {
            liveSceneRecordService.save(liveSceneRecord);
        } catch (Exception e) {
            return R.error("系统内部错误");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改现场回顾信息")
    @PostMapping("/update")
    @RequiresPermissions("live:livescenerecord:update")
    @ApiOperation(value = "修改回顾", produces = MediaType.APPLICATION_JSON_VALUE)
    @LiveTenant("sceneId")
    public R update(@ModelAttribute LiveSceneRecordVo liveSceneRecord){
        if (liveSceneRecord == null || liveSceneRecord.getId() == null || liveSceneRecord.getSceneId() == null) {
            return R.error("缺少参数");
        }
        try {
            liveSceneRecordService.update(liveSceneRecord);
        } catch (Exception e) {
            return R.error("系统内部错误");
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除现场回顾信息")
    @PostMapping("/delete")
    @RequiresPermissions("live:livescenerecord:delete")
    @ApiOperation(value = "删除回顾", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "回顾ID", name = "ids", paramType = "query", dataType = "int")})
    @LiveTenant(value = "ids", tenantTableClazz = LiveSceneRecordService.class)
    public R delete(Integer[] ids){
        if (ids == null || ids.length <= 0) {
            return R.error("缺少参数");
        }
        liveSceneRecordService.deleteRecords(ids);

        return R.ok();
    }
    
    /**
	 * 排序
	 */
    @SysLog("现场回顾信息排序")
	@PostMapping("/priority")
	@RequiresPermissions("live:livescenerecord:priority")
	@ApiOperation("排序")
    @LiveTenant(value = "ids", tenantTableClazz = LiveSceneRecordService.class)
	public R priority(Integer[] ids,Integer[] prioritys){
		if(ids==null || ids.length==0){
			return R.error("报道ID不能为空");
		}
		liveSceneRecordService.priority(ids, prioritys);
		return R.ok();
	}

	/**
     * 快速剪辑
     * */
	@SysLog("快速剪辑")
    @PostMapping("/fastClip")
    @RequiresPermissions("live:livescenerecord:update")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "回顾id", name = "recordId", dataType = "int"),
            @ApiImplicitParam(value = "快速剪辑开始Unix时间", name = "clipStartTime", dataType = "int"),
            @ApiImplicitParam(value = "快速剪辑结束Unix时间", name = "clipStopTime", dataType = "int"),
            @ApiImplicitParam(value = "新的回顾名称", name = "recordName", dataType = "String")
    })
    @ApiOperation("快速剪辑")
    public R fastClip(Integer recordId, Integer clipStartTime, Integer clipStopTime, String recordName){

	    if (null == recordId)
	        return R.error("回顾ID不能为空");

	    return liveSceneRecordService.fastClip(recordId, clipStartTime, clipStopTime, recordName);
    }
	
	/**
     * 快速剪辑
     * */
	@SysLog("获取快速剪辑文件的各切片时长")
    @GetMapping("/getSlicingTime")
    @RequiresPermissions("live:livescenerecord:update")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "回顾id", name = "recordId", dataType = "int")
    })
    @ApiOperation("获取快速剪辑文件的各切片时长")
    public R getSlicingTime(Integer recordId){

	    if (null == recordId)
	        return R.error("回顾ID不能为空");

	    return liveSceneRecordService.getSlicingTime(recordId);
    }

    /**
     * 获取时间轴
     * */
    @GetMapping("/timeLine")
    @RequiresPermissions("live:livescenerecord:update")
    @ApiImplicitParam(value = "回顾ID", name = "recordId", dataType = "int")
    @ApiOperation("获取时间轴")
    public R getTimeLine(Integer recordId){

        if (null == recordId)
            return R.error("参数不能为空");

        return liveSceneRecordService.getTimeLine(recordId);
    }

}
