import {
    ChangeDetectionStrategy,
    Component,
    Input,
    OnChanges,
    SimpleChanges,
} from '@angular/core';
import { LocalizationService } from '@shared/services/localization.service';
import { RouterHandlerService } from '@app/shared/services/router-handler.service';
import {
    FlightModel,
    IAircraft,
    IFlightId,
    IPrevFlight,
    Leg,
    Seat,
    SeatType,
} from '@app/shared/models-legacy';
import { RouteTypeLegacy } from '@app/shared/enumerators';
import { ViewType } from '@app/shared/enumerators/flight-request-type.enum';

@Component({
    selector: 'flight-details-airplane',
    templateUrl: './flight-details-airplane.component.html',
    styleUrls: ['./flight-details-airplane.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FlightDetailsAirplaneComponent implements OnChanges {
    @Input() leg: Leg;
    @Input() viewType: ViewType;
    previous?: FlightModel;
    previousUrl?: string;

    showPrevious: boolean;

    ViewType = ViewType;

    constructor(
        private localizationService: LocalizationService,
        private navigate: RouterHandlerService,
    ) {}

    ngOnChanges(changes: SimpleChanges) {
        if (this.leg && 'leg' in changes) {
            this.previous = this.getPreviousFlight();
            this.previousUrl = this.getPreviousFlightUrl();

            this.showPrevious = this.shoudShowPreviosFlight();
        }
    }

    getPlaneLink() {
        return (
            'http://www.aeroflot.ru/cms/' +
            this.localizationService.Language +
            '/flight/plane_park'
        );
    }

    get seats(): Seat[] {
        return this.aircraftInfo?.configuration?.seats;
    }

    getPreviousFlight() {
        const prevFlight: IPrevFlight =
            this.leg?.equipment?.aircraft?.previousFlight;
        if (!prevFlight) return;
        return {
            flightId: prevFlight as IFlightId,
            dateToSearchBy: new Date(prevFlight.localDate),
            routeType: RouteTypeLegacy.Direct,
        } as FlightModel;
    }

    getPreviousFlightUrl() {
        const flight = this.getPreviousFlight();
        if (!flight) return;
        return this.navigate.getBoardDetailsUrl(flight);
    }

    get aircraftInfo(): IAircraft {
        return this.leg?.equipment?.aircraft;
    }

    get aircraft(): any {
        return this.aircraftInfo?.actual ?? this.aircraftInfo?.scheduled;
    }

    // TODO: 7350
    get economy(): number {
        return this.getTypeSeatsValue(SeatType.Economy);
    }

    // TODO: 7350
    get comfort(): number {
        return this.getTypeSeatsValue(SeatType.Comfort);
    }

    // TODO: 7350
    get business(): number {
        return this.getTypeSeatsValue(SeatType.Business);
    }

    // TODO: 7350
    get all(): number {
        return this.seats
            ? this.seats.map((t) => t.count).reduce((a, b) => a + b, 0)
            : 0;
    }

    get title() {
        // Cannot use this.aircraft.title because there are cases when
        // there is leg.equipment.aircraft.actual with empty title.
        // In this case we should show scheduled title.
        if (!this.leg?.equipment?.aircraft) {
            return '';
        }

        const { actual, scheduled } = this.leg.equipment.aircraft;

        return actual?.title || scheduled?.title;
    }

    // TODO: 7350
    // TODO: 7350
    // TODO: 7350
    private getTypeSeatsValue(type: SeatType): number {
        return this.seats
            ? this.seats.find((t) => t.type == type)?.count
            : null;
    }

    private shoudShowPreviosFlight(): boolean {
        const scheduled = new Date(this.leg.departure.times.scheduled.local);

        const scheduledDateOnly = new Date(
            scheduled.getFullYear(),
            scheduled.getMonth(),
            scheduled.getDate()
        );

        const now = new Date();
        const todayDateOnly = new Date(
            now.getFullYear(),
            now.getMonth(),
            now.getDate()
        );

        todayDateOnly.setDate(todayDateOnly.getDate() - 2);

        return scheduledDateOnly > todayDateOnly;
    }
}
