package com.zhongshu.card.client.type.oss;

public enum DocumentType {

    Image(new String[]{"png", "jpg", "jpeg", "gif", "bmp"}),//图片
    Office(new String[]{"doc", "docx", "ppt", "pptx", "xls", "xlsx", "mpp"}),//办公
    Pdf(new String[]{"pdf"}),//pdf
    Cad(new String[]{"dwg", "dwf", "dwt", "dx", "ifc"}),//cad
    Video(new String[]{"mp4", "avi"}),//视频
    Archive(new String[]{"zip", "rar"}),//压缩文件
    Txt(new String[]{"txt", "log", "json"}),//文本类型
    Other(new String[]{}),//其他

    ;

    //扩展名
    private String[] extNames;


    DocumentType(String[] extName) {
        this.extNames = extName;
    }


    /**
     * 转换为图片
     *
     * @param extName
     * @return
     */
    public static String toPng(final String extName) {
        final DocumentType documentType = findDocumentType(extName);
        if (documentType == null) {
            return null;
        }

        if (documentType == Image) {
            return String.format("image", extName);
        }

        if (documentType == Cad) {
            return String.format("cad2png", extName);
        }
        return null;
    }


    /**
     * 转换为pdf文档
     *
     * @param extName
     * @return
     */
    public static String toPdf(final String extName) {
        final DocumentType documentType = findDocumentType(extName);
        if (documentType == null) {
            return null;
        }

        if (documentType == Office) {
            return String.format("%s2pdf", extName);
        }
        return null;
    }


    /**
     * 转换为文本
     *
     * @param extName
     * @return
     */
    public static String toTxt(final String extName) {
        final DocumentType documentType = findDocumentType(extName);
        if (documentType == null) {
            return null;
        }

        if (documentType == Office || documentType == Pdf) {
            return String.format("%s2txt", extName);
        } else if (documentType == Cad) {
            return "cad2txt";
        } else if (documentType == Archive) {
            return String.format("%s2list", extName);
        } else if (documentType == Image) {
            return "image2txt";
        }
        return null;
    }

    public static String toVideo(final String extName) {
        final DocumentType documentType = findDocumentType(extName);
        if (documentType == null) {
            return null;
        }
        if (documentType == Video) {
            return String.format("%s2mp4", extName);
        }
        return null;
    }


    public static String toPano(final String extName) {
        final DocumentType documentType = findDocumentType(extName);
        if (documentType == null) {
            return null;
        }
        if (documentType == Cad) {
            return "cad2pano";
        }
        return null;
    }

    /**
     * 查询文档类型
     *
     * @param extName
     * @return
     */
    public static DocumentType findDocumentType(String extName) {
        final String target = extName.toLowerCase();
        for (DocumentType documentType : DocumentType.values()) {
            for (String documentTypeExtName : documentType.extNames) {
                if (documentTypeExtName.equals(target)) {
                    return documentType;
                }
            }
        }
        return Other;
    }


}
