import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { IFlightLegStation } from '@typings/flight/flight-station';

type ChangeType = 'city' | 'airport' | 'terminal' | 'noChange';

@Component({
    selector: 'station-change',
    templateUrl: './station-change.component.html',
    styleUrls: ['./station-change.component.scss'],
})
export class StationChangeComponent implements OnChanges {
    @Input() from: IFlightLegStation;
    @Input() to: IFlightLegStation;

    changeType: ChangeType;

    ngOnChanges(changes: SimpleChanges) {
        if ('from' in changes || 'to' in changes) {
            this.changeType = this.getChangeType();
        }
    }

    private getChangeType(): ChangeType {
        const from = this.from?.latest;
        const to = this.to?.latest;

        if (!from || !to) return 'noChange';

        if (from.cityCode !== to.cityCode) {
            return 'city';
        }

        if (from.airportCode !== to.airportCode) {
            return 'airport';
        }

        if (
            this.from.terminal &&
            this.to.terminal &&
            this.from.terminal != this.to.terminal
        ) {
            return 'terminal';
        }

        return 'noChange';
    }

    get stationForTerminalChange() {
        const latestAirport = this.to?.latest.airportCode;
        const scheduledAirport = this.to?.scheduled.airportCode;

        if (!latestAirport || !scheduledAirport) {
            return undefined;
        }

        return latestAirport !== scheduledAirport ? this.to : undefined;
    }
}
