import { DatePreFilter } from '../../../model/common-model';
import moment from "moment";

export class DateComputation {
    public GetDateIntervalFromFilter(filter: any): { dateFrom: moment.Moment, dateTo: moment.Moment } {
        let isDateFromLocked: boolean = false, isDateToLocked: boolean = false;
        let dateFrom: moment.Moment, dateTo: moment.Moment;
        let today: moment.Moment = moment();
        if (filter?.datePreFilter != null) {
            switch (filter.datePreFilter as DatePreFilter) {
                case DatePreFilter.Today:
                    dateFrom = moment(today).startOf("day");
                    dateTo = moment(today).endOf("day");
                    break;
                case DatePreFilter.Past:
                    if (filter.includeToday) {
                        dateTo = moment(today).endOf("day");
                    } else {
                        dateTo = moment(today).subtract(1, "day").endOf("day");
                    }
                    isDateFromLocked = true;
                    break;
                case DatePreFilter.Futur:
                    if (filter.includeToday) {
                        dateFrom = moment(today).startOf("day");
                    } else {
                        dateFrom = moment(today).add(1, "day").startOf("day");
                    }
                    isDateToLocked = true;
                    break;
            }
        } else if (filter?.dateFrom != null || filter?.dateTo != null) {
            if (filter.dateFrom != null) {
                dateFrom = moment(filter.dateFrom).startOf("day");
            }
            if (filter.dateTo != null) {
                dateTo = moment(filter.dateTo).endOf("day");
            }
        }

        if (filter?.isThisMonth != null || filter?.isThisWeek != null || filter?.isLastMonth != null ||
            filter?.isLastWeek != null || filter?.isNextMonth != null || filter?.isNextWeek != null) {
            if (filter.isThisMonth) {
                dateFrom = this.addMomentToMoment(moment(today).startOf("month"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment(moment(today).endOf("month"), dateTo, false, isDateToLocked);
            }
            else if (filter.isThisWeek) {
                dateFrom = this.addMomentToMoment(moment(today).startOf("week"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment(moment(today).endOf("week"), dateTo, false, isDateToLocked);
            }

            if (filter.isLastMonth) {
                dateFrom = this.addMomentToMoment(moment(today).subtract(1, "month").startOf("month"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment(moment(today).subtract(1, "month").endOf("month"), dateTo, false, isDateToLocked);
            }
            else if (filter.isLastWeek) {
                dateFrom = this.addMomentToMoment(moment(today).subtract(1, "week").startOf("week"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment(moment(today).subtract(1, "week").endOf("week"), dateTo, false, isDateToLocked);
            }

            if (filter.isNextMonth) {
                dateFrom = this.addMomentToMoment(moment(today).add(1, "month").startOf("month"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment(moment(today).add(1, "month").endOf("month"), dateTo, false, isDateToLocked);
            }
            else if (filter.isNextWeek) {
                dateFrom = this.addMomentToMoment(moment(today).add(1, "week").startOf("week"), dateFrom, true, isDateFromLocked);
                dateTo = this.addMomentToMoment(moment(today).add(1, "week").endOf("week"), dateTo, false, isDateToLocked);
            }
        }
        return { dateFrom: dateFrom != null ? dateFrom.add(dateFrom.utcOffset(), "minutes") : dateFrom, dateTo: dateTo };
    }

    private addMomentToMoment(dateToAdd: moment.Moment, date: moment.Moment, isBefore: boolean, isDateLocked: boolean = false): moment.Moment {
        if (!isDateLocked) {
            if (date == null) {
                date = dateToAdd;
            }
            else {
                if (isBefore) {
                    if (dateToAdd < date) {
                        date = dateToAdd;
                    }
                }
                else {
                    if (dateToAdd > date) {
                        date = dateToAdd;
                    }
                }
            }
        }
        return date;
    }
}