package org.jeecg.modules.orderplatform.service.impl;

import cn.com.ctop.common.module.entity.*;
import cn.com.ctop.common.module.material.entity.MaterialVideoCover;
import cn.com.ctop.common.module.material.service.IMaterialVideoCoverService;
import cn.com.ctop.common.module.service.IMaterialTagInfoService;
import cn.com.ctop.common.module.service.ITagInfoService;
import cn.com.ctop.common.module.utils.CloudVideoProcessUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.jeecg.modules.orderplatform.entity.PlatformVideo;
import org.jeecg.modules.orderplatform.mapper.PlatformMaterialMapper;
import org.jeecg.modules.orderplatform.mapper.PlatformVideoMapper;
import org.jeecg.modules.orderplatform.service.IPlatformVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLDecoder;
import java.util.Map;


/**
 * 下单平台关联视频
 */
@Service
public class PlatformVideoServiceImpl extends ServiceImpl<PlatformVideoMapper, PlatformVideo> implements IPlatformVideoService {

    @Autowired
    IMaterialVideoCoverService materialVideoCoverService;

    @Autowired
    IMaterialTagInfoService materialTagInfoService;

    @Autowired
    private ITagInfoService tagInfoService;

    @Autowired
    PlatformMaterialMapper platformMaterialMapper;

    @Autowired
    PlatformVideoMapper platformVideoMapper;

    @Override
    public String uploadVideo(JSONObject params) {
        String md5 = params.getString("md5");
        if (null == md5 || md5.isEmpty()) {
            return "";
        }
        //入库下单制视频库
        boolean result = videoHandle(params);
        if (result) {
            //封面入库
            coverHandler(params);
            //标签相关入库
            tagHandler(params);
            return md5;
        }
        return "";
    }

    /**
     * 视频入库
     */
    private boolean videoHandle(JSONObject params) {

        PlatformVideo platformVideo = new PlatformVideo();
        //查询视频关联的物料信息
        platformVideo.setMaterialCode(params.getString("materialCode"));
        platformVideo.setName(params.getString("name"));
        platformVideo.setMd5(params.getString("md5"));
        platformVideo.setUrl(params.getString("url") == null ? "" : "https:" + params.getString("url"));
        platformVideo.setCoverUrl(getFirstFrame(platformVideo.getUrl(), params.getString("md5")));
        platformVideo.setDescription(params.getString("description"));
        platformVideo.setFormat(params.getString("format"));
        platformVideo.setDuration(params.getInteger("duration"));
        platformVideo.setSize(params.getInteger("size"));
        platformVideo.setHeight(params.getInteger("height"));
        platformVideo.setWidth(params.getInteger("width"));

        //查看是否已有
        platformVideoMapper.deleteByMd5(params.getString("md5"));

        return this.save(platformVideo);
    }

    /**
     * 封面处理
     */
    private void coverHandler(JSONObject params) {
        //删除以重复
        materialVideoCoverService.deleteByVideoMd5(params.getString("md5"));
        JSONArray covers = params.getJSONArray("covers");
        String videoMd5 = params.getString("md5");
        covers.forEach(cover -> {
            insertCover((Map)cover, videoMd5);
        });
    }

    private void insertCover(Map<String,Object> cover, String videoMd5) {
        MaterialVideoCover materialVideoCover = new MaterialVideoCover();
        materialVideoCover.setVideoMd5(videoMd5);
        materialVideoCover.setName(cover.get("name").toString());
        materialVideoCover.setMd5(cover.get("md5").toString());
        materialVideoCover.setUrl(cover.get("url").toString());
        materialVideoCover.setFormat(cover.get("format").toString());
        materialVideoCover.setSize((int) cover.get("size"));
        materialVideoCover.setHeight((int) cover.get("height"));
        materialVideoCover.setWidth((int)cover.get("width"));
        materialVideoCoverService.save(materialVideoCover);
    }

    /**
     * 标签处理
     */
    private void tagHandler(JSONObject params) {
        //处理标签。延续之前的逻辑，每次上传视频先删后加
        materialTagInfoService.deleteByCode(params.getString("md5"));
        insertTagList(params.getString("md5"), params.getString("userId"), params.getJSONArray("modalityTagList"));
        insertTagList(params.getString("md5"), params.getString("userId"), params.getJSONArray("contentTagList"));
        insertTagList(params.getString("md5"), params.getString("userId"), params.getJSONArray("senceTagList"));
        insertTagList(params.getString("md5"), params.getString("userId"), params.getJSONArray("modTagList"));
    }

    private void insertTagList(String code, String userId, JSONArray tagList) {
        if (null != tagList && !tagList.isEmpty()) {
            for (int m = 0; m < tagList.size(); m++) {
                Long tagId = tagList.getLong(m);
                TagInfo tagInfo = tagInfoService.getById(tagId);
                MaterialTagInfo setTag = new MaterialTagInfo(code, tagInfo, userId);
                setTag.setCategoryId(tagInfo.getTagCategoryId());
                materialTagInfoService.save(setTag);
            }
        }
    }

    /**
     * 获取视频首帧封面
     */
    private String getFirstFrame(String initialUrl, String videoMd5) {
        String decoderUrl = URLDecoder.decode(initialUrl).replace("https://media-1301855440.cos.ap-chongqing.myqcloud.com/", "");
        String loadImage = "cutFrame/" + videoMd5 + "/zero.jpg";
        return CloudVideoProcessUtil.videoCutPictureHandle(decoderUrl, loadImage);
    }
}
