import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
import { DateSelectorBaseComponent } from '../date-selector-base';
import { IDateTab } from '@toolkit/date-tabs/date-tabs.component';
import { DatesTranslationService } from '@shared/services/dates-translation.service';
import * as moment from 'moment';
import { FlightModel, getFirstLeg, Leg } from '../../../shared/models-legacy';

@Component({
    selector: 'day-tabs',
    templateUrl: './day-tabs.component.html',
})
export class DayTabsComponent extends DateSelectorBaseComponent<Date>
    implements OnInit, OnChanges {

    @Input() disabledDates: Date[];
    @Input() flight: FlightModel | null;
    departure: Leg;

    ngOnChanges(changes: SimpleChanges) {
        if ('flight' in changes && this.flight) {
            this.departure = getFirstLeg(this.flight);
        }

        super.ngOnChanges(changes);
    }

    constructor(protected translationService: DatesTranslationService) {
        super(translationService);
    }

    protected computeNextDate(date: Date): Date {
        return moment(date).add(1, 'day').toDate();
    }

    protected computeFirstDate(): Date {
        return this.dateFrom;
    }

    protected updateTabs() {
        this.tabs = this.tabs.map((tab) => {
            var dayForTab = moment(tab.value).format('YYYYMMDD');

            const isTabActive = this.isActive(tab.value);
            let isTabDisabled = !this.isEnabled(tab.value);

            if (this.departure?.daysForTabs) {
                isTabDisabled = !this.departure.daysForTabs.includes(dayForTab) || isTabDisabled;
            }

            if (this.disabledDates) {
                isTabDisabled ||= this.disabledDates.findIndex((x)=>moment(x).format('YYYYMMDD')==dayForTab) > -1;
            }

            return {
                ...tab,
                active: isTabActive,
                disabled: isTabDisabled,
            };
        });
    }

    protected generateTab(day: Date): IDateTab<Date> {
        var dayForTab = moment(day).format('YYYYMMDD');

        const isTabActive = this.isActive(day);
        let isTabDisabled = !this.isEnabled(day);

        if (this.departure?.daysForTabs) {
            isTabDisabled = !this.departure.daysForTabs.includes(dayForTab) || isTabDisabled;
        }

        if (this.disabledDates) {
            isTabDisabled ||= this.disabledDates.findIndex((x)=>moment(x).format('YYYYMMDD')==dayForTab) > -1;
        }

        return {
            label: this.getTabLabel(day, isTabActive),
            mobileLabel: this.getTabMobileLabel(day),
            value: day,
            active: isTabActive,
            disabled: isTabDisabled,
        };
    }

    private getTabMobileLabel(day: Date) {
        return this.translationService.getFullDateString(day, true);
    }

    private getTabLabel(day: Date, isTabActive: boolean) {
        const shortDateString = this.translationService.getShortDateString(day);
        const fullDateString = this.translationService.getFullDateString(day);

        return isTabActive ? fullDateString.toLowerCase() : shortDateString;
    }

    private isEnabled(day: Date) {
        return moment(day).isBetween(this.minDate, this.maxDate, 'day', '[]');
    }
}
