package com.fdkankan.common.util;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.RuntimeUtil;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OBJToGLBUtil {

    /**
     *
     * @param objPath obj文件的目录
     * @param glbPath glb文件的最对路径
     */
    public static void objToGlb(String objPath, String glbPath) throws Exception {
        OBJToGLBUtil.checkObj(objPath);
        objPath = OBJToGLBUtil.getObj(objPath);
        String command = "obj2gltf -i " + objPath + " -o " + glbPath;
        log.info("开始执行obj转换gbl命令-{}", command);
//        Process exec = RuntimeUtil.exec(command);
        CmdUtils.callLine(command);
        log.info("结束执行obj转换gbl命令-{}", command);
    }

    static String getObj(String objPath) {
        List<File> files = FileUtil.loopFiles(objPath);
        if (objPath.toLowerCase(Locale.ROOT).contains(".obj")) {
            return objPath;
        }
        for (File file2 : files) {
            if (file2.isDirectory()) {
                continue;
            }
            if (FileUtil.extName(file2.getName()).toLowerCase(Locale.ROOT).equals("obj")) {
                return file2.getAbsolutePath();
            }
        }
        return "";
    }


    private static boolean checkObj(String objPath) {
        if (objPath.contains(".obg")) {
            objPath = FileUtil.file(objPath).getParent();
        }
        File file1 = new File(objPath);
        File[] files = file1.listFiles();
        if (files == null || files.length <= 0) {
            throw new BusinessException(ErrorCode.FAILURE_CODE_7014);
        }
        File mtlFile = null;
        File objFile = null;
        for (File file2 : files) {
            if (file2.getName().endsWith(".obj")) {
//                if (StringUtils.isChinese(file2.getName())) {
//                    throw new ServiceException(HttpStatus.e7008.getMsg(), HttpStatus.e7008.getCode());
//                }
                objFile = file2;
            }
            if (file2.getName().endsWith(".mtl")) {
//                if (StringUtils.isChinese(file2.getName())) {
//                    throw new ServiceException(HttpStatus.e7008.getMsg(), HttpStatus.e7008.getCode());
//                }
                mtlFile = file2;
            }
            if (FileUtil.getType(file2).equals("jpg") || FileUtil.getType(file2).equals("png")) {
                BufferedImage read = ImgUtil.read(file2);
                int widthImg = read.getWidth();
                int heightImg = read.getHeight();
                if (widthImg > 2048 && heightImg > 2048) {
                    OBJToGLBUtil.log.info("尺寸大2k,执行压缩");
                    ImgUtil.scale(file2,
                            file2,
                            2048, 2048, null);
                }
            }
        }
        if (mtlFile == null || objFile == null) {
            throw new BusinessException(ErrorCode.FAILURE_CODE_5059);
        }
        return OBJToGLBUtil.checkMtl(file1, mtlFile);
    }

    private static boolean checkMtl(File allFile, File file) {
        if (!file.getName().endsWith(".mtl")) {
            return false;
        }
        LinkedHashSet<String> imgName = new LinkedHashSet<>();
        if (allFile == null || allFile.length() <= 0) {
            return false;
        }
        File[] files = allFile.listFiles();
        if (files == null || files.length <= 0) {
            return false;
        }
        for (File listFile : files) {
            String modelName = listFile.getName();
            imgName.add(modelName);
        }
        LinkedHashSet<String> imgMtl = OBJToGLBUtil.readMtlFile(file.getPath());
        for (String mtlName : imgMtl) {
            if (!imgName.contains(mtlName)) {
                throw new BusinessException(ErrorCode.FAILURE_CODE_5065);
            }
        }
        return true;
    }

    private static LinkedHashSet<String> readMtlFile(String mtlPath) {
        LinkedHashSet<String> imgName = new LinkedHashSet<>();
        FileReader fileReader = new FileReader(mtlPath);
        try {
            List<String> lines = fileReader.readLines();
            for (String line : lines) {
                String[] tempsa = line.split("[ ]+");
                if (tempsa[0].trim().equals("map_Ka")) {
                    imgName.add(tempsa[1]);
                }
                if (tempsa[0].trim().equals("map_Kd")) {
                    imgName.add(tempsa[1]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgName;
    }


    public static void main(String[] args) {
        System.out.println(OBJToGLBUtil.checkObj("F:\\test\\新建文件夹\\police模型"));
    }
}
