package com.engine.salary.transmethod;

import com.engine.salary.enums.SalaryCycleTypeEnum;
import com.engine.salary.enums.SalaryRoundingModeEnum;
import com.engine.salary.enums.SalaryValueTypeEnum;
import com.engine.salary.enums.datacollection.AttendQuoteFieldSourceTypeEnum;
import com.engine.salary.enums.datacollection.AttendQuoteFieldTypeEnum;
import com.engine.salary.enums.datacollection.AttendQuoteSourceTypeEnum;
import com.engine.salary.enums.salaryitem.SalaryDataTypeEnum;
import com.engine.salary.util.SalaryI18nUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

public class TransMethod {

    public static String timeToDate(String time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = "";
        try {
            Date parse = timeFormat.parse(time);
            format = dateFormat.format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format;
    }

    public static String timeToMoth(String time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = "";
        try {
            Date parse = timeFormat.parse(time);
            format = dateFormat.format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format;
    }


    /**
     * 字段来源类型
     *
     * @param sourceType
     * @return
     */
    public static String fieldSourceType(String sourceType) {
        return AttendQuoteFieldSourceTypeEnum.getDefaultLabelByValue(Integer.valueOf(sourceType));
    }

    /**
     * 字段类型
     *
     * @param sourceType
     * @return
     */
    public static String fieldType(String sourceType) {
        return AttendQuoteFieldTypeEnum.getDefaultLabelByValue(Integer.valueOf(sourceType));
    }

    /**
     * 考勤来源类型
     *
     * @param sourceType
     * @return
     */
    public static String sourceType(String sourceType) {
        return AttendQuoteSourceTypeEnum.getDefaultLabelByValue(Integer.valueOf(sourceType));
    }

    /**
     * 薪资项目进位规则
     *
     * @param roundingMode
     * @return
     */
    public static String roundingMode(String roundingMode) {
        return SalaryRoundingModeEnum.getDefaultLabelByValue(Integer.valueOf(roundingMode));
    }

    /**
     * 取值方式
     * @param datasource
     * @return
     */
    public static String datasource(String datasource) {
        return SalaryValueTypeEnum.getDefaultLabelByValue(Integer.valueOf(datasource));
    }

    /**
     * 字段类型
     * @return
     */
    public static String dataType(String dataType) {
        return SalaryDataTypeEnum.getDefaultLabelByValue(dataType);
    }

    public static String buildSalaryCycle(String salaryCycle,String salaryCycleFromDay) {
        String salaryCycleStr;
        SalaryCycleTypeEnum salaryCycleTypeEnum = SalaryCycleTypeEnum.parseByValue(Integer.parseInt(salaryCycle));
        if (salaryCycleTypeEnum == null) {
            return StringUtils.EMPTY;
        }
        switch (salaryCycleTypeEnum) {
            case BEFORE_LAST_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, "1") ? SalaryI18nUtil.getI18nLabel(98395, "上上月1号——上上月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98399, "上上月{0}号——上月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (Integer.parseInt(salaryCycleFromDay) - 1));
                break;
            case LAST_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, "1") ? SalaryI18nUtil.getI18nLabel(98396, "上月1号——上月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98400, "上月{0}号——本月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (Integer.parseInt(salaryCycleFromDay) - 1));
                break;
            case THIS_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, "1") ? SalaryI18nUtil.getI18nLabel(98397, "本月1号——本月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98401, "本月{0}号——下月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (Integer.parseInt(salaryCycleFromDay) - 1));
                break;
            case NEXT_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, "1") ? SalaryI18nUtil.getI18nLabel(98398, "下月1号——下月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98402, "下月{0}号——下下月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (Integer.parseInt(salaryCycleFromDay) - 1));
                break;
            default:
                salaryCycleStr = "";
        }
        return salaryCycleStr;
    }

}
