package com.friday.custody.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.apache.poi.ss.usermodel.CellType.*;


/**
 * @Description
 * @Author wulifeng
 * @Date: 2022/3/11
 */
@Slf4j
public class ExcelUtils {

    private static final String[] CN_NUM = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"};

    private static DecimalFormat decimalFormat ;

    public static String getStringValue(Cell cell,boolean defaultDataFormat){
        if(!defaultDataFormat){
            getStringValue(cell);
        }

        if(cell==null){
            return null;
        }
        String returnStr = null;
        switch (cell.getCellTypeEnum()){
            case _NONE:
                returnStr = null;
                break;
            case NUMERIC: //数字
                String cellValue1 = "";
                CellStyle cellStyle = cell.getCellStyle();
                //20,32 是 时间格式， 14，31，57，58是日期格式
                short dataFormat = cellStyle.getDataFormat();
                if (DateUtil.isCellDateFormatted(cell)) {

                    SimpleDateFormat sdf = null;

                    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    }
                    try {
                        cellValue1 = sdf.format(cell.getDateCellValue());// 日期
                        returnStr = cellValue1;
                    } catch (Exception e) {
                        try {
                            throw new Exception("exception on get date data !".concat(e.toString()));
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }finally{
                        sdf = null;
                    }
                }else{
                    double numericCellValue = cell.getNumericCellValue();
                    log.info("cellValue={}",numericCellValue);
                    String dataFormatString = cellStyle.getDataFormatString();
                    log.info("dataFormatString:{}",dataFormatString);
                    if(StringUtils.isNotBlank(dataFormatString)){
                        String substring = new String();
                        String substring1 = new String();
                        try {
                            substring = dataFormatString.substring(0, 2);
                            substring1 = dataFormatString.substring(dataFormatString.length() - 2,dataFormatString.length() - 1);
                            char[] chars = dataFormatString.toCharArray();
                            StringBuilder format = new StringBuilder("#.");
                            if(substring.equals("0.") &&
                                    "_".equals(substring1)){
                                boolean rightFormat =true;
                                for (int i = 2; i < dataFormatString.length()-2; i++) {
                                    if(chars[i]=='0'){
                                        format.append("0");
                                    }else{
                                        rightFormat=false;
                                        log.error("异常格式={}",dataFormatString);
                                    }
                                }
                                if(rightFormat){
                                    decimalFormat = new DecimalFormat(format.toString());
                                    String cellValue = decimalFormat.format(numericCellValue);
                                    log.info("格式为：{},调整格式前：{}，调整格式后：{}",format.toString(),numericCellValue,cellValue);
                                    returnStr = cellValue;
                                    break;
                                }
                            }
                        }catch (Exception e){
                            log.error("getStringValue",e);
                        }
                    }

                    returnStr = NumberToTextConverter.toText(cell.getNumericCellValue());
                    log.info("returnStr:{}",returnStr);
                }

                break;
            case STRING: //字符串
                returnStr = String.valueOf(cell.getStringCellValue());
                break;
            case BOOLEAN: //Boolean
                returnStr = String.valueOf(cell.getBooleanCellValue());
                break;
            case FORMULA: //公式
                returnStr = String.valueOf(cell.getCellFormula());
                break;
            case BLANK: //空值
                returnStr = "";
                break;
            default:
                returnStr = "未知类型";
                break;
        }
        return returnStr;
    }

    public static String getStringValue(Cell cell){
        if(cell==null){
            return "";
        }
        String returnStr = null;
        switch (cell.getCellTypeEnum()){
            case _NONE:
                returnStr = null;
                break;
            case NUMERIC: //数字
                double numericCellValue1 = cell.getNumericCellValue();
                log.info("NUMRIC numericCellValue1 为{}",numericCellValue1);

                log.info("NUMRIC value 为{}",cell.getDateCellValue());
                String cellValue1 = "";
                CellStyle cellStyle = cell.getCellStyle();
//                DateFormatConverter.getJavaDateTimePattern();
                //20,32 是 时间格式， 14，31，57，58是日期格式
                if (DateUtil.isCellDateFormatted(cell)) {
                    String dataFormatString = cellStyle.getDataFormatString();
                    log.info("getStringValue_dataFormatString={}",dataFormatString);
                    Date dateCellValue = cell.getDateCellValue();
                    returnStr = transferDateFormatFromExcelToJava(dataFormatString, dateCellValue);
                }else{
                    double numericCellValue = cell.getNumericCellValue();
                    log.info("cellValue={}",numericCellValue);
                    String dataFormatString = cellStyle.getDataFormatString();
                    log.info("dataFormatString:{}",dataFormatString);

                    boolean isDateTimeFormat = judgeExcelFormatCompatible(dataFormatString);
                    if(isDateTimeFormat){
                        Date dateCellValue = cell.getDateCellValue();
                        returnStr = transferDateFormatFromExcelToJava(dataFormatString, dateCellValue);
                        if(returnStr == null){
                            return "";
                        }
                        return returnStr;
                    }else if(StringUtils.isNotBlank(dataFormatString)){
                        String substring = new String();
                        String substring1 = new String();
                        try {
                            substring = dataFormatString.substring(0, 2);
                            substring1 = dataFormatString.substring(dataFormatString.length() - 2,dataFormatString.length() - 1);
                            char[] chars = dataFormatString.toCharArray();
                            StringBuilder format = new StringBuilder("#.");
                            if(substring.equals("0.") &&
                                    "_".equals(substring1)){
                                boolean rightFormat =true;
                                for (int i = 2; i < dataFormatString.length()-2; i++) {
                                    if(chars[i]=='0'){
                                        format.append("0");
                                    }else{
                                        rightFormat=false;
                                        log.error("异常格式={}",dataFormatString);
                                    }
                                }
                                if(rightFormat){
                                    decimalFormat = new DecimalFormat(format.toString());
                                    String cellValue = decimalFormat.format(numericCellValue);
                                    log.info("格式为：{},调整格式前：{}，调整格式后：{}",format.toString(),numericCellValue,cellValue);
                                    returnStr = cellValue;
                                    break;
                                }
                            }
                        }catch (Exception e){
                            log.error("getStringValue",e);
                        }
                    }

                    returnStr = NumberToTextConverter.toText(cell.getNumericCellValue());
                    log.info("returnStr:{}",returnStr);
                }

                break;
            case STRING: //字符串
                returnStr = String.valueOf(cell.getStringCellValue());
                break;
            case BOOLEAN: //Boolean
                returnStr = String.valueOf(cell.getBooleanCellValue());
                break;
            case FORMULA: //公式
                returnStr = String.valueOf(cell.getCellFormula());
                break;
            case BLANK: //空值
                returnStr = "";
                break;
            case ERROR: //故障
                returnStr = "非法字符";
                break;
            default:
                returnStr = "未知类型";
                break;
//                throw new IllegalArgumentException("请输入字符串格式内容！");
        }
        if(returnStr == null){
            returnStr = "";
        }
        return returnStr;
    }

    public static CellStyle getPreferredCellStyle(Cell cell) {
        CellStyle cellStyle = cell.getCellStyle();
        return cellStyle;
    }



    public static boolean judgeExcelFormatCompatible(String excelFormat){
        if(StringUtils.isBlank(excelFormat)){
            return false;
        }
        if(excelFormat.contains("yyyy\"年\"m\"月\"d\"日\"")){
            return true;
        }else if(excelFormat.contains("yyyy\"年\"m\"月\"")){
            return true;
        }else if(excelFormat.contains("yyyy\"年\"mm\"月\"dd\"日\"")){
            return true;
        }else if(excelFormat.contains("m\"月\"d\"日\"")){
            return true;
        }else if(excelFormat.contains("yyyy/m/d\\ h:mm\\ AM/PM")){
            return true;
        }else if(excelFormat.contains("yyyy/m/d\\ h:mm")){
            return true;
        }else if(excelFormat.contains("yyyy/m/d")){
            return true;
        }else if(excelFormat.contains("yyyymmdd")){
            return true;
        }else if(excelFormat.contains("yyyy\\-mm\\-dd")){
            return true;
        }else if(excelFormat.contains("m/d/yy")){
            return true;
        }else if(excelFormat.contains("mm/dd/yy")){
            return true;
        }else if(excelFormat.contains("yy/m/d")){
            return true;
        }else if(excelFormat.contains("m/d")){
            return true;
        }else if(excelFormat.contains("dd\\-mmm\\-yy") || excelFormat.contains("dd-mmm-yy")){
            return true;
        }else if(excelFormat.contains("d\\-mmm\\-yy") || excelFormat.contains("d-mmm-yy")){
            return true;
        }else if(excelFormat.contains("d\\-mmm") || excelFormat.contains("d-mmm")){
            return true;
        }else if(excelFormat.contains("mmmmm")){
            return true;
        }else if(excelFormat.contains("mmmm\\-yy") || excelFormat.contains("mmmm-yy")){
            return true;
        }else if(excelFormat.contains("mmm\\-yy") || excelFormat.contains("mmm-yy")){
            return true;
        }else if(excelFormat.contains("mmmm")){
            return true;
        }else if(excelFormat.contains("mmm")){
            return true;
        }else if(excelFormat.contains("h:mm:ss\\ AM/PM")){
            return true;
        }else if(excelFormat.contains("h:mm:ss")){
            return true;
        }else if(excelFormat.contains("h:mm\\ AM/PM")){
            return true;
        }else if(excelFormat.contains("h:mm")){
            return true;
        }else if(excelFormat.contains("mm:ss.0")){
            return true;
        }else if(excelFormat.contains("mm:ss")){
            return true;
        }else if(excelFormat.contains("h\"时\"mm\"分\"ss\"秒\"_ AM/PM") || excelFormat.contains("h\"时\"mm\"分\"ss\"秒\" AM/PM")){
            return true;
        }else if(excelFormat.contains("h\"时\"mm\"分\"ss\"秒\"") ){
            return true;
        }else if(excelFormat.contains("上午/下午h\"时\"mm\"分\"") ){
            return true;
        }else if(excelFormat.contains("h\"时\"mm\"分\"\\ AM/PM") ){
            return true;
        }else if(excelFormat.contains("h\"时\"mm\"分\"")){
            return true;
        }else if(excelFormat.contains("aaaa")) {
            return true;
        }
        return false;
    }

    public static String transferDateFormatFromExcelToJava(String excelFormat,Date date){
        log.info("transferDateFormatFromExcelToJava_excelFormat={},date={}",excelFormat,date);
        String javaFormat = new String();
        //1.格式
        if(excelFormat.contains("yyyy\"年\"m\"月\"d\"日\"")){
            javaFormat = "yyyy年M月d日";
        }else if(excelFormat.contains("yyyy\"年\"m\"月\"")){
            javaFormat = "yyyy年M月";
        }else if(excelFormat.contains("yyyy\"年\"mm\"月\"dd\"日\"")){
            javaFormat = "yyyy年MM月dd日";
        }else if(excelFormat.contains("m\"月\"d\"日\"")){
            javaFormat = "M月d日";
        }else if(excelFormat.contains("yyyy/m/d\\ h:mm\\ AM/PM")){
            javaFormat = "yyyy/M/d h:mm a";
        }else if(excelFormat.contains("yyyy/m/d\\ h:mm")){
            javaFormat = "yyyy/M/d H:mm";
        }else if(excelFormat.contains("yyyy/m/d")){
            javaFormat = "yyyy/M/d";
        }else if(excelFormat.contains("yyyymmdd")){
            javaFormat = "yyyyMMdd";
        }else if(excelFormat.contains("yyyy\\-mm\\-dd")){
            javaFormat = "yyyy-MM-dd";
        }else if(excelFormat.contains("m/d/yy")){
            javaFormat = "M/d/yy";
        }else if(excelFormat.contains("mm/dd/yy")){
            javaFormat = "MM/dd/yy";
        }else if(excelFormat.contains("yy/m/d")){
            javaFormat = "yy/M/d";
        }else if(excelFormat.contains("m/d")){
            javaFormat = "M/d";
        }else if(excelFormat.contains("dd\\-mmm\\-yy") || excelFormat.contains("dd-mmm-yy")){
            javaFormat = "d-MMM-yy";
        }else if(excelFormat.contains("d\\-mmm\\-yy") || excelFormat.contains("d-mmm-yy")){
            javaFormat = "d-MMM-yy";
        }else if(excelFormat.contains("d\\-mmm") || excelFormat.contains("d-mmm")){
            javaFormat = "d-MMM";
        }else if(excelFormat.contains("mmmm\\-yy") || excelFormat.contains("mmmm-yy")){
            javaFormat = "MMMM-yy";
        }else if(excelFormat.contains("mmm\\-yy") || excelFormat.contains("mmm-yy")){
            javaFormat = "MMM-yy";
        }else if(excelFormat.contains("mmmm")){
            javaFormat = "MMMM";
        }else if(excelFormat.contains("mmm")){
            javaFormat = "MMM";
        }else if(excelFormat.contains("h:mm:ss\\ AM/PM")){
            javaFormat = "h:mm:ss a";
        }else if(excelFormat.contains("h:mm:ss")){
            javaFormat = "H:mm:ss";
        }else if(excelFormat.contains("h:mm\\ AM/PM")){
            javaFormat = "h:mm a";
        }else if(excelFormat.contains("h:mm")){
            javaFormat = "H:mm";
        }else if(excelFormat.contains("mm:ss.0")){
            javaFormat = "MM:ss:00";
        }else if(excelFormat.contains("mm:ss")){
            javaFormat = "MM:ss";
        }else if(excelFormat.contains("h\"时\"mm\"分\"ss\"秒\"_ AM/PM") || excelFormat.contains("h\"时\"mm\"分\"ss\"秒\" AM/PM")){
            javaFormat = "h时mm分ss秒 a";
        }else if(excelFormat.contains("h\"时\"mm\"分\"ss\"秒\"") ){
            javaFormat = "H时mm分ss秒";
        }else if(excelFormat.contains("上午/下午h\"时\"mm\"分\"") ){
            String tempExcelFormat = "h\"时\"mm\"分\"\\ AM/PM";
            String s = transferDateFormatFromExcelToJava(tempExcelFormat, date);
            log.info("上午/下午h\"时\"mm\"分\"格式第一次转化为{}",s);
            if(s.contains("AM")){
               s = s.replace(" AM", "");
               s = "上午"+ s;
            }
            log.info("transferDateFormatFromExcelToJava_return{}",s);
            return s;
        }else if(excelFormat.contains("h\"时\"mm\"分\"\\ AM/PM") ){
            javaFormat = "h时mm分 a";
        }else if(excelFormat.contains("h\"时\"mm\"分\"")){
            javaFormat = "H时mm分";
        }else if(excelFormat.contains("aaaa")){
            //星期几
            String weekOfDate = getWeekOfDate(date);
            log.info("transferDateFormatFromExcelToJava_return:{}",weekOfDate);
            return weekOfDate;
        }else {
            log.error("transferDateFormatFromExcelToJava未被兼容的格式：{}",excelFormat);
        }

        SimpleDateFormat format1 = new SimpleDateFormat(javaFormat,Locale.ENGLISH);
        String format = format1.format(date);

        //2.中英文等
        if(excelFormat.contains("[DBNum1]")){
            format = transferNumToSimpleChinese(format);
        }
        log.info("transferDateFormatFromExcelToJava_return:{}",format);
        return format;
    }

    public static String getWeekOfDate(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0){
            w = 0;
        }
        return weekDays[w];
    }

    public static String transferNumToSimpleChinese(String javaFormatedDateString){
        if(StringUtils.isBlank(javaFormatedDateString)){
            return javaFormatedDateString;
        }
        char[] chars = javaFormatedDateString.toCharArray();
        StringBuilder returnStringBuilder = new StringBuilder();
        for (char aChar : chars) {
            if(aChar>=48 && aChar <= 57){
                returnStringBuilder.append(CN_NUM[aChar - 48]);
            }else{
                returnStringBuilder.append(aChar);
            }
            if(aChar == '月' || aChar == '日' || aChar == '时' || aChar == '分' || aChar == '秒'){
                String tenString = new String();
                if(returnStringBuilder.length()>=3){
                    tenString= returnStringBuilder.substring(returnStringBuilder.length() - 3,returnStringBuilder.length() - 2);
                }
                String singleString = returnStringBuilder.substring(returnStringBuilder.length() - 2,returnStringBuilder.length() - 1);
                String replaceString;

                //十位数判断
                if(StringUtils.isNotBlank(tenString)){
                    if(tenString.equals("一")){
                        replaceString = "十";
                    }else if (tenString.equals("二") || tenString.equals("三") || tenString.equals("四")|| tenString.equals("五")){
                        replaceString = tenString + "十";
                    }else{ //不是数字，直接拿过来
                        replaceString = tenString;
                    }
                    //个位数判断 为零不写
                    if(singleString.equals("〇")){
                    }else{
                        replaceString = replaceString + singleString;
                    }
                    returnStringBuilder.replace(returnStringBuilder.length() - 3,returnStringBuilder.length() - 1,replaceString);
                }else{
                    //个位数判断 为零不写
                    replaceString =  singleString;

                    returnStringBuilder.replace(returnStringBuilder.length() - 2,returnStringBuilder.length() - 1,replaceString);
                }

            }
        }

        return returnStringBuilder.toString();
    }

    public static void main(String[] args) throws ParseException {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 12; i++) {
            String date1 ="2022-"+ (i+1) +"-01";
            list.add(date1);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd");
//        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MMM-dd",Locale.ENGLISH);
        SimpleDateFormat format1 = new SimpleDateFormat("MMM-yy",Locale.ENGLISH);
        for (String s : list) {
            Date parse = format.parse(s);
            String format2 = format1.format(parse);
            System.out.println(format2);
        }


        HashMap newMap = new HashMap<String,String>();
        String access_token = String.valueOf(newMap.get("access_token"));
        System.out.println(access_token);
//        String sdfStr = "hh:mm:ssa, E, MMM, dd, yyyy";
//        String sdfStr = "yyyy年M月d日";
        String sdfStr = "MMMM-yy";

        // E 星期几/英文模式下简写
        // EEE 星期几/英文模式下全名
        // M  数字月份 1-12 (en)
        // MM  数字月份 01-12 (en)
        // MMM  月份简写 Jan (en)
        // MMMM  月份全名 January (en)

        SimpleDateFormat sdf_en = new SimpleDateFormat(sdfStr, Locale.ENGLISH); //英文

        SimpleDateFormat sdf_chs = new SimpleDateFormat(sdfStr, Locale.SIMPLIFIED_CHINESE); //简体中文

        System.out.println(sdf_en.format(new Date()));
        String s = transferNumToSimpleChinese(sdf_en.format(new Date()));

        System.out.println(s);

        System.out.println(sdf_chs.format(new Date()));
    }
}
