package com.xunmei.common.core.utils;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.EnumSerializer;
import com.xunmei.common.core.utils.snowId.DateHelper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;

/**
 * 履职类型
 */
@JsonSerialize(using = EnumSerializer.class)
public enum ResumptionType {
    DAY(
            "每日履职",
            new FormType[]{
                    //FormType.MRLZ_YYQ,
                    //FormType.MRLZ_YYZ,
                    //FormType.MRLZ_YYH,
                    FormType.WDAQY_MRLZYYQ,
                    FormType.WDAQY_MRLZYYH,
                    //FormType.WDBA_MXSLZ,
                    FormType.WDBA_MRLZYYQ,
                    FormType.WDBA_MRLZYYH,
            },
            new Overview[]{
                    new Overview("网点安全员每日履职（营业前）", true),
                    new Overview("网点安全员每日履职（营业后）", true),
                    //new Overview("网点保安每小时履职", true),
                    new Overview("网点保安每日履职（营业前）", true),
                    new Overview("网点保安每日履职（营业后）", true)
                    //new Overview("款包交接", false),
                    //new Overview("自助设备巡查", false),
                    //new Overview("监控设备巡查", false)
            }
    ),

    WEEK("每周履职",
            new FormType[]{
                    //FormType.MZLZ_WDFZR,
                    //FormType.MZLZ_BWZQQT,
                    //FormType.MZLZ_BWZQYYH,
                    //FormType.MZLZ_BWZQYYQ,
                    //FormType.MZLZ_BWZQYYZ
            },
            new Overview[]{
                    //new Overview("网点负责人", true),
                    //new Overview("保安履职营业前", true),
                    //new Overview("保安履职每小时", true),
                    //new Overview("保安履职营业后", true)
            }
    ),

    MONTH(
            "每月履职",
            new FormType[]{
                    //FormType.MYLZ
                    FormType.WDAQY_MYLZ,
                    FormType.WDFZR_MYLZ,
                    FormType.YJZHBWGB_MYLZ
            },
            new Overview[]{
                    //new Overview("每月履职", true),
                    new Overview("网点安全员每月履职", true),
                    new Overview("网点负责人每月履职", true),
                    new Overview("一级支行保卫干部每月履职", true)
            }),
    QUARTER(
            "每季履职",
            new FormType[]{
                    //FormType.MJLZ
                    FormType.WDFZR_MJLZ,
                    FormType.YJZHBWGB_MJLZ,
                    FormType.EJFHBWGB_MJLZ,
                    FormType.YJFHBWGB_MJLZ
            },
            new Overview[]{
                    new Overview("网点负责人每季履职", true),
                    new Overview("一级支行保卫干部每季履职", true),
                    new Overview("二级分行保卫干部每季履职", true),
                    new Overview("一级分行保卫干部每季履职", true)
            }
    ),
    HALFYEAR(
            "半年履职",
            new FormType[]{
                    FormType.WDFZR_BNLZ,
                    FormType.YJZHBWGB_BNLZ,
                    FormType.EJFHBWGB_BNLZ,
                    FormType.YJFHBWGB_BNLZ
            },
            new Overview[]{
                    new Overview("网点负责人半年履职", true),
                    new Overview("一级支行保卫干部半年履职", true),
                    new Overview("二级分行保卫干部半年履职", true),
                    new Overview("一级分行保卫干部半年履职", true)
            }
    ),
    YEAR(
            "每年履职",
            new FormType[]{
                    FormType.WDFZR_MNLZ,
                    FormType.YJZHBWGB_MNLZ,
                    FormType.EJFHBWGB_MNLZ,
                    FormType.YJFHBWGB_MNLZ,
                    FormType.YJFHFGHZ_MNLZ,
                    FormType.EJFHFGHZ_MNLZ,
                    FormType.YJZHFGHZ_MNLZ
            },
            new Overview[]{
                    new Overview("网点负责人每年履职", true),
                    new Overview("一级支行保卫干部每年履职", true),
                    new Overview("二级分行保卫干部每年履职", true),
                    new Overview("一级分行保卫干部每年履职", true),
                    new Overview("一级分行分管行长每年履职", true),
                    new Overview("二级分行分管行长每年履职", true),
                    new Overview("一级支行分管行长每年履职", true)
            }
    ),
    HOUR(
            "每小时履职",
            new FormType[]{
                    FormType.WDBA_MXSLZ
            },
            new Overview[]{
                    new Overview("网点保安每小时履职", true),
            }
    ),
    ;


    @Setter
    @Getter
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Overview {
        private String name;
        private Boolean isResumption;
    }

    /*
    public static class ResumptionTypeListConverter implements AttributeConverter<List<ResumptionType>, String> {
        @Override
        public String convertToDatabaseColumn(final List<ResumptionType> attribute) {
            return attribute == null ? "[]" : JSON.toJSONString(attribute);
        }
        @Override
        public List<ResumptionType> convertToEntityAttribute(final String dbData) {
            return JSON.parseArray(dbData, ResumptionType.class);
        }
    }
    */

    @Getter
    private final String text;

    @Getter
    private final FormType[] subType;

    @Getter
    private final Overview[] overview;

    ResumptionType(final String text, final FormType[] subType, final Overview[] overview) {
        this.text = text;
        this.subType = subType;
        this.overview = overview;
    }

    /**
     * 根据表单类型获取总览, 下标一致
     *
     * @param formType
     * @return
     */
    public Overview getOverview(final FormType formType) {
        for (int i = 0; i < this.getSubType().length; i++) {
            if (this.getSubType()[i].equals(formType)) {
                return this.overview[i];
            }
        }
        return null;
    }

    /**
     * @param formType
     * @param date
     * @param orgId
     * @return
     */
    public String toId(final FormType formType, final Date date, final Long orgId) {
        return this.toId(date, orgId) + "|" + formType.ordinal();
    }

    /**
     * @param date
     * @param orgId
     * @return
     */
    public String toId(final Date date, final Long orgId) {
        switch (this) {
            case DAY:
                return IDHelper.date(date, orgId);
            case WEEK:
                return IDHelper.week(date, orgId);
            case MONTH:
                return IDHelper.month(date, orgId);
            case QUARTER:
                return IDHelper.quarter(date, orgId);
            case HALFYEAR:
                return IDHelper.halfyear(date, orgId);
            case HOUR:
                return IDHelper.hour(date, orgId);
            default:
                return IDHelper.year(date, orgId);
        }
    }

    /**
     * @param date
     * @return
     */
    public Ymd toYmd(final Date date) {
        DateHelper dateHelper = new DateHelper(date);
        switch (this) {
            case DAY:
                return Ymd.builder()
                          .year(dateHelper.getYear())
                          .month(dateHelper.getMonth())
                          .day(dateHelper.getDay())
                          .build();
            case WEEK:
                return Ymd.builder()
                          .year(DateUtil.year(date))
                          .week(DateUtil.weekOfYear(date))
                          .build();
            case MONTH:
                return Ymd.builder()
                          .year(dateHelper.getYear())
                          .month(dateHelper.getMonth())
                          .build();
            case QUARTER:
                return Ymd.builder()
                        .year(dateHelper.getYear())
                        .quarter(dateHelper.getQuarter())
                        .build();
            case HALFYEAR:
                return Ymd.builder()
                        .year(dateHelper.getYear())
                        .halfyear(dateHelper.getHalfyear())
                        .build();
            case HOUR:
                return Ymd.builder()
                        .year(dateHelper.getYear())
                        .month(dateHelper.getMonth())
                        .day(dateHelper.getDay())
                        .hour(dateHelper.getHour())
                        .minute(dateHelper.getMin())
                        .build();
            default:
                return Ymd.builder()
                          .year(dateHelper.getYear())
                          .build();
        }
    }

    /**
     * 当前日期上半年还是下半年
     * @param date
     * @return
     */
    private Integer halfYear(final Date date){
        Integer half = date.getMonth()+1;
        if(half >= 0 && half <= 6){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     * 是否过期
     *
     * @param ymd
     * @return
     */
    public boolean isExpire(final Ymd ymd) {
        final Date now = new Date();
        final Date lastDate;
        switch (this) {
            case DAY:
                lastDate = ymd.getDate();
                break;
            //这周的最后一天
//            case WEEK:
//                lastDate = DateHelper.getLastDayOfWeek(ymd.getYear(), ymd.getWeek());
//                break;
            //这月的最后一天
            case MONTH:
                lastDate = DateHelper.getLastDayOfMonth(ymd.getYear(), ymd.getMonth());
                break;
            //这季的最后一天
            case QUARTER:
                lastDate = DateHelper.getLastDayOfQuarter(ymd.getYear(), ymd.getQuarter());
                break;
            //这半年的最后一天
            case HALFYEAR:
                lastDate = DateHelper.getLastDayOfHalfyear(ymd.getYear(), ymd.getHalfyear());
                break;
            //这年的最后一天
            default:
                lastDate = DateHelper.getLastDayOfYear(ymd.getYear());
                break;
        }
        // 不是同一天并且在今天之前
        return !DateUtil.isSameDay(lastDate, now) && lastDate.before(now);
    }

}
