package cn.com.ctop.common.module.material.service.impl;

import cn.com.ctop.common.module.material.entity.MaterialVideoFrame;
import cn.com.ctop.common.module.material.mapper.MaterialVideoFrameMapper;
import cn.com.ctop.common.module.material.service.IMaterialVideoFrameService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.CloudVideoProcessUtil;
import cn.com.ctop.common.module.utils.LoadFileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 视频素材截帧信息
 */
@Slf4j
@Service
public class MaterialVideoFrameServiceImpl extends ServiceImpl<MaterialVideoFrameMapper, MaterialVideoFrame> implements IMaterialVideoFrameService {

    @Autowired
    MaterialVideoFrameMapper  materialVideoFrameMapper;

    @Value("${jeecg.path.image-upload}")
    private String imageUpLoadPath;

    @Override
    public int getFrameCountByCode(String videoMd5) {
        return materialVideoFrameMapper.queryFrameCountByCode(videoMd5);
    }

    @Override
    public void getTencentCutFrame(String videoUrl, String videoMd5, String coverUrl) {
        try {
            String loadImage = "cutFrame/" + videoMd5 + "/${Number}.jpg";
            Map<String, Object> coverMap = CloudVideoProcessUtil.videoCutFrameHandle(videoUrl, loadImage);
            List<MaterialVideoFrame> batch = new ArrayList<>();
            for (int i = 0; i <= 13; i++) {
                String url = (String) coverMap.get(String.valueOf(i));
                if (!Check.isNull(url)) {
                    String cutFramePath = LoadFileUtil.downLoadFromUrl(url, imageUpLoadPath);
                    String cutFrameMd5 = LoadFileUtil.getMD5(cutFramePath);
                    MaterialVideoFrame materialVideoFrame = new MaterialVideoFrame();
                    materialVideoFrame.setMd5(cutFrameMd5);
                    materialVideoFrame.setUrl(url);
                    materialVideoFrame.setVideoMd5(videoMd5);
                    materialVideoFrame.setFrameIndex(i+1);
                    batch.add(materialVideoFrame);
                    this.save(materialVideoFrame);
                    LoadFileUtil.delFile(cutFramePath);
                }
            }
            insertFrames(batch,videoMd5,coverUrl);
        } catch (Exception e) {
            log.info("抽帧异常,code:{}", videoMd5);
            log.error("获取截帧图片MD5错误");
        }
    }

    private void insertFrames(List<MaterialVideoFrame> batch,  String videoMd5, String coverUrl){
        String cutFramePath = LoadFileUtil.downLoadFromUrl(coverUrl, imageUpLoadPath);
        String cutFrameMd5 = null;
        try {
            cutFrameMd5 = LoadFileUtil.getMD5(cutFramePath);
        } catch (IOException e) {
            log.error("获取截帧图片MD5错误");
        }
        MaterialVideoFrame materialVideoFrame = new MaterialVideoFrame();
        materialVideoFrame.setMd5(cutFrameMd5);
        materialVideoFrame.setUrl(coverUrl);
        materialVideoFrame.setVideoMd5(videoMd5);
        materialVideoFrame.setFrameIndex(0);
        batch.add(materialVideoFrame);
        this.saveBatch(batch);
    }
}