package cn.com.ctop.kuaishou.modules.live.enums;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.ExportExcelUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Row;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class liveDataUtil {

    /**
     * 数组集转LIST 同时去除多余双引号
     */
    public static List<List<String>> getCsvList(List<String[]> records) {
        List<List<String>> list = new ArrayList<>();
        for (String[] record : records) {
            List<String> asList = Arrays.asList(record);
            asList.replaceAll(t -> t.replace("\"", ""));
            list.add(asList);
        }
        return list;
    }

    /**
     * ----------------------------------------获取CSV格式标题和字段对应关系-----------------------------------------------
     */

    // 素材
    public static JSONObject getCsvMaterialTitle(List<String> titles) {
        JSONObject obj = new JSONObject();
        String sign = "";
        Map<String, String> map = MaterialEnum.getMap();
        for (int i = 0; i < titles.size(); i++) {
            String code = map.get(titles.get(i));
            if (Check.isNull(code)) {
                //记录目前不支持导入的数据
                sign += titles.get(i) + "、";
            }
            obj.put(String.valueOf(i), code);
        }
        obj.put("sign", sign);
        return obj;
    }

    // 触达
    public static JSONObject getCsvTouchTitle(List<String> titles) {
        JSONObject obj = new JSONObject();
        String sign = "";
        Map<String, String> map = TouchEnum.getMap();
        for (int i = 0; i < titles.size(); i++) {
            String code = map.get(titles.get(i));
            if (Check.isNull(code)) {
                //记录目前不支持导入的数据
                sign += titles.get(i) + "、";
            }
            obj.put(String.valueOf(i), code);
        }
        obj.put("sign", sign);
        return obj;
    }

    // 广告
    public static JSONObject getCsvAdTitle(List<String> titles) {
        JSONObject obj = new JSONObject();
        String sign = "";
        Map<String, String> map = AdEnum.getMap();
        for (int i = 0; i < titles.size(); i++) {
            String code = map.get(titles.get(i));
            if (Check.isNull(code)) {
                //记录目前不支持导入的数据
                sign += titles.get(i) + "、";
            }
            obj.put(String.valueOf(i), code);
        }
        obj.put("sign", sign);
        return obj;
    }

    // 省份
    public static JSONObject getCsvProvinceTitle(List<String> titles) {
        JSONObject obj = new JSONObject();
        String sign = "";
        Map<String, String> map = ProvinceEnum.getMap();
        for (int i = 0; i < titles.size(); i++) {
            String code = map.get(titles.get(i));
            if (Check.isNull(code)) {
                //记录目前不支持导入的数据
                sign += titles.get(i) + "、";
            }
            obj.put(String.valueOf(i), code);
        }
        obj.put("sign", sign);
        return obj;
    }

    /**
     * ========================================获取XLSX、XLS标题和字段对应关系===============================================
     */

    //素材
    public static JSONObject getMaterialTitle(ExportExcelUtils eeu, Row titleRow, int lastCellNum) {
        JSONObject obj = new JSONObject();
        String sign = "";
        for (int i = 0; i < lastCellNum; i++) {
            String titleName = eeu.getCellValue(titleRow.getCell(i));
            String code = MaterialEnum.getCodeByName(titleName);
            if (Check.isNull(code)) {
                //记录目前不支持导入的数据
                sign += titleName + "、";
            }
            obj.put(String.valueOf(i), code);
        }
        obj.put("sign", sign);
        return obj;
    }

    // 触达
    public static JSONObject getTouchTitle(ExportExcelUtils eeu, Row titleRow, int lastCellNum) {
        JSONObject obj = new JSONObject();
        String sign = "";
        for (int i = 0; i < lastCellNum; i++) {
            String titleName = eeu.getCellValue(titleRow.getCell(i));
            String code = TouchEnum.getCodeByName(titleName);
            if (Check.isNull(code)) {
                //记录目前不支持导入的数据
                sign += titleName + "、";
            }
            obj.put(String.valueOf(i), code);
        }
        obj.put("sign", sign);
        return obj;
    }

    // 广告
    public static JSONObject getAdTitle(ExportExcelUtils eeu, Row titleRow, int lastCellNum) {
        JSONObject obj = new JSONObject();
        String sign = "";
        for (int i = 0; i < lastCellNum; i++) {
            String titleName = eeu.getCellValue(titleRow.getCell(i));
            String code = AdEnum.getCodeByName(titleName);
            if (Check.isNull(code)) {
                //记录目前不支持导入的数据
                sign += titleName + "、";
            }
            obj.put(String.valueOf(i), code);
        }
        obj.put("sign", sign);
        return obj;
    }

    // 省份
    public static JSONObject getProvinceTitle(ExportExcelUtils eeu, Row titleRow, int lastCellNum, String type, String name) {
        JSONObject obj = new JSONObject();
        String sign = "";
        for (int i = 0; i < lastCellNum; i++) {
            String titleName = eeu.getCellValue(titleRow.getCell(i));
            if (name.equals(titleName)) {
                obj.put(String.valueOf(i), type);
            } else {
                String code = ProvinceEnum.getCodeByName(titleName);
                if (Check.isNull(code)) {
                    //记录目前不支持导入的数据
                    sign += titleName + "、";
                }
                obj.put(String.valueOf(i), code);
            }
        }
        obj.put("sign", sign);
        return obj;
    }


    /**
     * 1、判断是否为数字类型（NUMBER）或数字计算公式（NUMBER_FORMULA）;
     * 2、获取解析后的值进行判断是否包含有（E、e、+等符号）;
     * 3、使用java自带数学类，将科学计算公式转换成所需类型。
     * @return
     */
    public static String NUMBER_FORMULA(String E_Value) {
        String value=null;

        // 判断是否为科学计数法（包含E、e、+等符号）
        if ((("" + E_Value).indexOf("E") != -1 || ("" + E_Value).indexOf("e") != -1 || ("" + E_Value).indexOf("+") != -1)&&!E_Value.contains("https")) {
            BigDecimal bd = new BigDecimal("" + E_Value);
            if (("" + E_Value).indexOf("E") != -1 || ("" + E_Value).indexOf("e") != -1 || ("" + E_Value).indexOf("+") != -1) {
                bd = new BigDecimal(Double.parseDouble(bd.toString()));
                value = bd.toString();
            } else {
                value = "" + E_Value;
            }
        } else {
            value = "" + E_Value;
        }
        return value;
    }
}