package com.engine.salary.enums.push;

import cn.hutool.core.util.NumberUtil;
import com.engine.salary.enums.BaseEnum;
import com.engine.salary.util.SalaryDateUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Objects;

import static com.engine.salary.util.SalaryDateUtil.DATE_FORMATTER_PATTERN;

/**
 * 推送数据来源
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public enum PushItemFieldEnum implements BaseEnum<Integer> {

    NUMBER(1, "数值", 87625) {
        @Override
        public Object convertValue(String value) {
            if (NumberUtil.isNumber(value)) {
                return new BigDecimal(value).toPlainString();
            }
            return "null";
        }
    },
    TEXT(2, "文本", 85393) {
        @Override
        public Object convertValue(String value) {
            return String.format("'%s'",value);
        }
    },
    //    DATE(3, "日期", 85393) {
//        @Override
//        public Object convertValue(String value) {
//
//            //to_date('2022-05-10 16:03:02','yyyy-MM-dd HH24:mi:ss')
//            return value;
//        }
//    },
    YEAR(4, "日期文本-年", 85393) {
        @Override
        public Object convertValue(String value) {
            if (StringUtils.isNotBlank(value) && SalaryDateUtil.parse(value, DATE_FORMATTER_PATTERN) != null) {
                value = value.substring(0, 4);
            }
            return String.format("'%s'",value);
        }
    },
    YEAR_MONTH(5, "日期文本-年月", 85393) {
        @Override
        public Object convertValue(String value) {
            if (StringUtils.isNotBlank(value) && SalaryDateUtil.parse(value, DATE_FORMATTER_PATTERN) != null) {
                value = value.substring(0, 7);
            }
            return String.format("'%s'",value);
        }
    },
    YEAR_MONTH_DAY(6, "日期文本-年月日", 85393) {
        @Override
        public Object convertValue(String value) {
            if (StringUtils.isNotBlank(value) && SalaryDateUtil.parse(value, DATE_FORMATTER_PATTERN) != null) {
                value = value.substring(0, 10);
            }
            return String.format("'%s'",value);
        }
    };

    private int value;

    private String defaultLabel;

    private int labelId;

    PushItemFieldEnum(int value, String defaultLabel, int labelId) {
        this.value = value;
        this.defaultLabel = defaultLabel;
        this.labelId = labelId;
    }

    @Override
    public Integer getValue() {
        return value;
    }

    @Override
    public String getDefaultLabel() {
        return defaultLabel;
    }

    @Override
    public Integer getLabelId() {
        return labelId;
    }

    public abstract Object convertValue(String value);

    public static PushItemFieldEnum parseByValue(int value) {
        for (PushItemFieldEnum salaryDataSourceEnum : PushItemFieldEnum.values()) {
            if (Objects.equals(salaryDataSourceEnum.getValue(), value)) {
                return salaryDataSourceEnum;
            }
        }
        return TEXT;
    }

    public static String getDefaultLabelByValue(Integer value) {
        if (value == null) {
            return "";
        }
        PushItemFieldEnum[] enumAry = PushItemFieldEnum.values();
        for (int i = 0; i < Arrays.asList(enumAry).size(); i++) {
            if (enumAry[i].getValue().equals(value)) {
                return enumAry[i].getDefaultLabel();
            }
        }
        return "";
    }
}
