package de.stebo.filter;

import de.stebo.provider.IHolidayProvider;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Checks if a day is a "bridge day".
 */
public class BridgeDayFilter extends ADayFilter {

    private final IHolidayProvider holidayProvider;

    public BridgeDayFilter(IHolidayProvider holidayProvider) {
        this.holidayProvider = holidayProvider;
    }

    @Override
    protected boolean checkWorkday(Calendar cal) {
        Calendar dayBefore = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
        dayBefore.add(Calendar.DATE, -1);

        Calendar dayAfter = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
        dayAfter.add(Calendar.DATE, 1);

        // if day before cal is a holiday and the day after cal is saturday
        // then cal is a bridge day (means: no work day)
        if(this.holidayProvider.isHoliday(dayBefore) && dayAfter.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            return false;
        }

        // if day before cal is a sunday and the day after cal is a holiday
        // then cal is a bridge day (means: no work day)
        if(dayBefore.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY && this.holidayProvider.isHoliday(dayAfter)) {
            return false;
        }

        return true;
    }
}