package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.entity.MaterialInfo;
import cn.com.ctop.common.module.entity.VideoMerge;
import cn.com.ctop.common.module.entity.VideoMergeTask;
import cn.com.ctop.common.module.entity.VideoPart;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.service.IVideoMergeTaskService;
import cn.com.ctop.common.module.service.IVideoPartService;
import cn.com.ctop.common.module.utils.MpsUtils;
import com.alibaba.fastjson.JSONArray;
import com.aliyuncs.mts.model.v20140618.QueryJobListResponse;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "视频片段")
@RestController
@RequestMapping("/ctop/materialpart")
public class MaterialPartController {
    @Autowired
    private IVideoMergeTaskService videoMergeTaskService;
    @Autowired
    private IVideoPartService videoPartService;
    @Autowired
    private IMaterialInfoService materialInfoService;

    @ApiOperation(value = "片段合成", notes = "片段合成")
    @PostMapping(value = "/merge")
    public Result<Object> merge(@RequestBody JSONArray jsonArray, HttpServletRequest req) {
        Result<Object> result = new Result<>();
        List<String> jobIdList = new ArrayList<>();
        List<VideoMerge> videoMergeList = jsonArray.toJavaList(VideoMerge.class);
        try {
            for (VideoMerge videoMerge : videoMergeList) {
                MaterialInfo start = materialInfoService.getMaterialInfoByCode(videoMerge.getStartPart());
                MaterialInfo end = materialInfoService.getMaterialInfoByCode(videoMerge.getEndPart());
                List<String> partUrlList = new ArrayList<>();
                List<String> parts = videoMerge.getParts();
                for (String part : parts) {
                    MaterialInfo materialInfo = materialInfoService.getMaterialInfoByCode(part);
                    String url = materialInfo.getUrl();
                    int i = url.indexOf('/', 10);
                    partUrlList.add(url.substring(i + 1, url.length()));
                }
                String startUrl = start.getUrl();
                String startPath = startUrl.substring(startUrl.indexOf('/', 10) + 1, startUrl.length());
                String endUrl = end.getUrl();
                String endPath = endUrl.substring(endUrl.indexOf('/', 10) + 1, endUrl.length());
                String jobId = MpsUtils.mergeOneVideo(startPath, partUrlList, endPath);
                QueryJobListResponse.Job job = MpsUtils.getJobStatus(jobId);
                VideoMergeTask videoMergeTask = new VideoMergeTask();
                LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                videoMergeTask.setUserId(sysUser.getId());
                videoMergeTask.setJobId(jobId);
                videoMergeTask.setStatus(job.getState());
                videoMergeTaskService.save(videoMergeTask);
                parts.add(0, videoMerge.getStartPart());
                parts.add(videoMerge.getStartPart());
                for (String part : parts) {
                    VideoPart videoPart = new VideoPart();
                    videoPart.setJobId(jobId);
                    videoPart.setPartId(part);
                    videoPartService.save(videoPart);
                }
                jobIdList.add(jobId);
            }
            result.success("合成中，可在任务列表中查询状态，任务ID:" + new Gson().toJson(jobIdList));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }
}
