package jnpf.easyexcel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.StringUtils;
import org.apache.poi.ss.usermodel.DateUtil;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;


public class StaffDateStringConverter implements Converter<Date> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Date.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                  GlobalConfiguration globalConfiguration) throws ParseException {
        String format = switchDateFormat(cellData.getStringValue());
        if(StringUtils.isEmpty(format)){
            BigDecimal numberValue = cellData.getNumberValue();
            if (numberValue == null){
                return null;
            }
            double v = numberValue.doubleValue();
            return  DateUtil.getJavaDate(v,
                    globalConfiguration.getUse1904windowing(), null);
        }
        if (contentProperty == null || format.isEmpty()) {
            return null;
        } else {
            return DateUtils.parseDate(cellData.getStringValue(), format);
        }
    }

    @Override
    public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
            return new WriteCellData<>(DateUtils.format(value, null));
        } else {
            return new WriteCellData<>(DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat()));
        }
    }

    private static final String param2 = "yyyy-MM-dd";
    private static final String param5 = "yyyy/MM/dd";
    private static final String param6 = "yyyyMMdd";
    private static final String param7 = "yyyy.MM.dd";
    private static final String MINUS = "-";
    private static final String MINUSD = ".";

    private String switchDateFormat(String dateString) {
        if (dateString == null) {
            return "";
        }
        int length = dateString.length();
        switch (length) {
            case 10:
                if (dateString.contains(MINUS)) {
                    return param2;
                }else if (dateString.contains(MINUSD)){
                    return param7;
                }else {
                    return param5;
                }
            case 8:
                return param6;
            default:
                return "";
        }
    }


}
