package com.capgemini.reports.periodicity;

import com.capgemini.reports.database.javabean.enumeration.SchedulingEnum;
import com.capgemini.reports.database.manager.MngSchedulingCriteriaVal.SchedulingCriteriaValue;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Specific behaviors for the ALL_MONTH period.
 */
public enum WeekendPeriod implements SchedulingPeriod {

    /**
     * The report will be executed the first Sunday of the month.
     */
    FIRST_WEEK_END("first_we"),
    /**
     * The report will be executed the second Sunday of the month.
     */
    SECOND_WEEK_END("second_we"),
    /**
     * The report will be executed the third Sunday of the month.
     */
    THIRD_WEEK_END("third_we"),
    /**
     * The report will be executed the fourth Sunday of the month.
     */
    FOURTH_WEEK_END("fourth_we"),
    /**
     * The report will be executed the fifth Sunday of the month.
     */
    FIFTH_WEEK_END("fifth_we"),
    /**
     * The report will be executed the last day of the month.
     */
    END_MONTH("end_of_month");

    /**
     * Enumeration cache for conversions.
     **/
    private static final Map<String, WeekendPeriod> enumMap;

    static {
        enumMap = new HashMap<>(WeekendPeriod.values().length);
        for (WeekendPeriod currentEnum : WeekendPeriod.values()) {
            enumMap.put(currentEnum.toString(), currentEnum);
        }
    }

    @Getter
    private final String strMonthType;

    WeekendPeriod(final String strMonthType) {
        this.strMonthType = strMonthType;
    }

    /**
     * Give the appropriate {@link WeekendPeriod} associate to the string value.<br/>
     * Else return null.
     *
     * @param value String - the string value
     * @return the appropriate {@link WeekendPeriod}. Return null if the string value doesn't exist.
     */
    public static WeekendPeriod getEnum(final String value) {
        return enumMap.get(value);
    }

    @Override
    public SchedulingEnum getPeriodicity() {
        return SchedulingEnum.WEEKEND;
    }

    @Override
    public List<SchedulingCriteriaValue> asCriteriaValues() {
        List<SchedulingCriteriaValue> list = new ArrayList<>(2);
        list.add(new SchedulingCriteriaValue(SchedulingEnum.PERIOD, SchedulingEnum.WEEKEND.getCriteriaName()));
        list.add(new SchedulingCriteriaValue(SchedulingEnum.WEEKEND, String.valueOf(this.getStrMonthType())));
        return list;
    }

    /**
     * Getter for object string value.
     *
     * @return String value with readable format
     **/
    @Override
    public String toString() {
        return strMonthType;
    }

    @Override
    public <T> T accept(final PeriodVisitor<T> visitor) {
        return visitor.visit(this);
    }
}
