import {
    ChangeDetectionStrategy,
    Component,
    Input,
    OnChanges,
    SimpleChanges,
} from '@angular/core';
import {
    FlightModel,
    getFirstLeg,
    getLastLeg,
    Leg,
} from '@app/shared/models-legacy';
import * as moment from 'moment';

@Component({
    selector: 'flight-schedule',
    templateUrl: './flight-schedule.component.html',
    styleUrls: ['./flight-schedule.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FlightScheduleComponent implements OnChanges {
    departureLeg: Leg;
    arrivalLeg: Leg;
    selected = true;
    @Input() flight: FlightModel;

    isDayActive(day: string) {
        return this.departureLeg?.daysOfWeek?.flight?.includes(day);
    }

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

    replaceDates(replacement: string) {
        let currDate = moment(this.departureLeg.departure.times.scheduled.local.toString());

        let weekStart = currDate.clone().isoWeekday(1);
        let weekEnd = currDate.clone().isoWeekday(7);

        replacement = replacement.replace('{START_DATE}', weekStart.format('DD.MM.YYYY'));
        replacement = replacement.replace('{END_DATE}', weekEnd.format('DD.MM.YYYY'));

        return replacement;
    }
}
