package com.ruoyi.common.importExcel.excel;


import com.ruoyi.common.exception.ServiceErrorException;
import com.ruoyi.common.utils.StringUtils;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import lombok.AllArgsConstructor;
import lombok.Getter;


/**
 * 费用类型
 *
 * @author CAC
 * @date 2020-04-01 12:00:00
 */
@Getter
@AllArgsConstructor
public enum TableTypeEnum {

    /**
     * 不用国际化的模板，直接放在模板根路径下即可；枚举中不用定义path 涉及到国际化的模板，需要在模板根路径下定义文件夹，文件夹名称为枚举的path，文件夹内部放置对应的国际化文件，可以不指定枚举name。参考线索导入模板
     */

    ACCOUNT_EXCEL(1, "客户信息导入模板.xlsx", "", "", "", ""),
    ParaLibrary_Excel(2, "参数信息导入模板.xlsx", "", "", "", ""),
    Elevator_Model_Excel(3, "区域电梯参数信息导入模板.xlsx", "", "", "", ""),
    LEAD_IMPORT_EXCEL(4, "线索导入模板.xlsx", "LeadImportTemplate.xlsx", "ШаблонИмпортаЛидов.xlsx", "PlantillaDeImportaciónDeClientesPotenciales.xlsx", "lead"),
    PRICE_FORM_EXCEL(5, "价格表信息导入模板.xlsx", "", "", "", ""),
    TRAIN_QUESTION_EXCEL(6, "培训试题导入模板.xlsx", "", "", "", ""),
    Elevator_Model_Date(7, "参数信息导出模板.xlsx", "", "", "", ""),
    PARA_VALIDATION_EXCEL(8, "参数校验规则导入模板.xlsx", "", "", "", ""),
    PARA_RELATED_CASE_EXCEL(9, "参数联动设定导入模板.xlsx", "", "", "", ""),
    PROJECT_IMPORT_EXCEL(10, "项目导入模板.xlsx", "项目导入模板.xlsx", "项目导入模板.xlsx", "项目导入模板.xlsx",
            "project"),
    ;


    private static final Map<String, Function<TableTypeEnum, String>> FILE_LOCAL_NAME_FUN = new HashMap<String, Function<TableTypeEnum, String>>() {{
        put("zh", TableTypeEnum::getName);
        put("en", TableTypeEnum::getNameUS);
        put("es", TableTypeEnum::getNameES);
        put("ru", TableTypeEnum::getNameRU);
    }};

    private final Integer value;
    private final String name;
    private final String nameUS;
    private final String nameRU;
    private final String nameES;
    private final String path;

    public static TableTypeEnum getEnum(Integer value) {
        for (TableTypeEnum logicEnum : TableTypeEnum.values()) {
            if (logicEnum.getValue().equals(value)) {
                return logicEnum;
            }
        }
        throw new ServiceErrorException("Invalid TableTypeEnum type value: " + value);
    }

    public static Integer valueOfName(String name) {
        for (TableTypeEnum logicEnum : TableTypeEnum.values()) {
            if (logicEnum.getName().equals(name)) {
                return logicEnum.getValue();
            }
        }
        return null;
    }


    //public static String getFileNames(TableTypeEnum tableTypeEnum, Locale locale) {
    //    String language = locale.getLanguage();
    //
    //    String path = tableTypeEnum.getPath();
    //
    //    // 获取文件名
    //    try {
    //
    //        File directory;
    //        String returnPath = "";
    //        try {
    //            directory = ResourceUtils.getFile("classpath:" + ExcelTemplatePath + path + "/" + language);
    //            
    //            if (directory.isDirectory() && ArrayUtils.isEmpty(directory.listFiles())) {
    //                directory = ResourceUtils.getFile("classpath:" + ExcelTemplatePath);
    //                returnPath = "";
    //            } else {
    //                returnPath = path + "/" + language;
    //            }
    //        } catch (FileNotFoundException e) {
    //            // 没有国际化文件夹时，可能不需要国际化，直接查找当前路径
    //            //directory = ResourceUtils.getFile("classpath:" + ExcelTemplatePath + path);
    //            //returnPath = path;
    //            // 没有国际化文件夹时，可能不需要国际化，直接在设定根路径下查找
    //            directory = ResourceUtils.getFile("classpath:" + ExcelTemplatePath);
    //        }
    //
    //        if (directory.isDirectory()) {
    //            // 获取目录中的所有文件和子目录
    //            File[] files = directory.listFiles();
    //
    //            if (ArrayUtils.isNotEmpty(files)) {
    //                for (File file : files) {
    //                    if (file.isFile()) {
    //                        System.out.println("File: " + file.getName());
    //                        //return returnPath + "/" + file.getName();
    //                        if (StringUtils.isEmpty(returnPath)) {
    //                            // 文件名去掉后缀
    //                            String fName = file.getName().substring(0, file.getName().lastIndexOf("."));
    //                            if (tableTypeEnum.getName().equals(fName)) {
    //                                return file.getName();
    //                            }
    //                        } else {
    //                            return returnPath + "/" + file.getName();
    //                        }
    //                    }
    //                }
    //            }
    //            throw new IOException("Expected a file, found a directory: " + directory.getName());
    //        } else {
    //            throw new IOException("Expected a directory, found a file: " + directory.getName());
    //        }
    //
    //    } catch (FileNotFoundException e) {
    //        e.printStackTrace();
    //        throw new RuntimeException("当前文件路径下没有文件: " + ExcelTemplatePath + path + "/" + language + "||" + e.getMessage());
    //    } catch (IOException e) {
    //        throw new RuntimeException(e);
    //    }
    //}
    public static String getFileNames(TableTypeEnum tableTypeEnum, Locale locale) {
        String language = locale.getLanguage();

        String path = tableTypeEnum.getPath();

        if (StringUtils.isEmpty(path)) {
            return tableTypeEnum.getName();
        } else {
            //ClassPathResource classPathResource = new ClassPathResource(ExcelTemplatePath + path + "/" + language);
            //classPathResource.getFile();
            //path + "/" + language + "/" + tableTypeEnum.getName();
            return path + "/" + language + "/" + FILE_LOCAL_NAME_FUN.get(language).apply(tableTypeEnum);
        }
    }

}
