package com.xhkjedu.sstudy.utils;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sstudy.listener.MessageSender;
import com.xhkjedu.sstudy.mapper.resource.ResourceFileMapper;
import com.xhkjedu.sstudy.model.resource.TResource;
import com.xhkjedu.sstudy.vo.resource.ResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @classname ConvertUtil
 * @description 文件转换工具类
 * @date 2021/10/27 9:32
 **/
@Component
public class ConvertUtil {
    @Resource
    private ResourceFileMapper resourceFileMapper;
    @Autowired
    private MessageSender messageSender;
    private static Integer[] CLASS = {1,2,5,7};//资源分类1word,2ppt,3pdf,4图片,5视频,6音频,7压缩包,10其他

    //资源转换（word或ppt转为pdf，视频非h264编码转码，压缩包解压）
    public void convertResource(TResource model) {
        Integer resourceclass = model.getResourceclass();
        //flv的视频不转换
        if ("flv".equals(model.getSuffix())) return;
        if (Arrays.asList(CLASS).contains(resourceclass) && model.getConverted() == 0) {
            ResourceVo resource = new ResourceVo();
            resource.setResourceid(model.getResourceid());
            resource.setResourceclass(resourceclass);
            resource.setResourceurl(model.getResourceurl());
            resource.setSuffix(model.getSuffix());
            messageSender.convertResource(resource);
        }
    }

    //获取资源预览路径
    public String getResourceUrl(ResourceVo resource) {
        String pdfurl = resource.getPdfurl();
        Integer resourceclass = resource.getResourceclass();
        Integer converted = resource.getConverted();
        if ((resourceclass == 1 || resourceclass == 2) && converted == 0) {
            //需要把word或ppt转为pdf
            pdfurl = messageSender.convertResource(resource);
        } else if (resourceclass == 5) {
            pdfurl = videoTransfer(resource);
        } else if (resourceclass == 7) {
            pdfurl = unzip(resource);
        }
        resource.setPdfurl(pdfurl);
        return pdfurl;
    }

    //视频转换
    public String videoTransfer(ResourceVo resource) {
        String resourceurl = resource.getResourceurl();
        String suffix = resource.getSuffix();
        if (suffix.equals("mp4") && !resource.getMp4code().equals("h264")) {
            resourceurl = messageSender.convertResource(resource);
        }
        return resourceurl;
    }

    //解压zip
    public String unzip(ResourceVo resource) {
        String resourceid = resource.getResourceid();
        String pdfurl = null;
        Integer converted = resource.getConverted();
        if (converted == 0) {
            //获取压缩包文件
            pdfurl = messageSender.convertResource(resource);
        } else {
            List<Map> files = resourceFileMapper.listFile(resourceid);
            pdfurl = JSON.toJSONString(files);
        }
        return pdfurl;
    }

    /**
     * 学生答案图片合并mq
     *
     * @param [psqid, userAnswers, device(作业类型1题库作业2附件作业)]
     * @return void
     * @author ywx
     * @date 2022/5/9 14:57
     */
    public void imgMerge(Integer psqid, List<String> userAnswers, Integer device) {
        messageSender.imgMerge(psqid, userAnswers, device);
    }

}
