import {
    Component,
    HostBinding,
    Input,
    OnChanges,
    SimpleChanges,
} from '@angular/core';
import { RouteTypeLegacy } from '@app/shared/enumerators';
import {
    FlightModel,
    Transfer,
    TransferLogic,
} from '@app/shared/models-legacy';

type ITransferIcon =
    | 'intermediate-landing'
    | 'flight-transfer'
    | 'multi-landings';

@Component({
    selector: 'transfer-inline',
    templateUrl: './transfer-inline.component.html',
    styleUrls: ['./transfer-inline.component.scss'],
    host: {
        class: 'flight-options-icon',
    },
})
export class TransferInlineComponent implements OnChanges {
    constructor(private logic: TransferLogic) {}

    @Input() flights: FlightModel[];

    @HostBinding('class.intermediate-landing') get isMultileg() {
        return this.routeType === RouteTypeLegacy.MultiLeg;
    }
    @HostBinding('class.flight-transfer') get isConnecting() {
        return this.routeType === RouteTypeLegacy.Connecting;
    }

    RouteType = RouteTypeLegacy;
    routeType: RouteTypeLegacy.Connecting | RouteTypeLegacy.MultiLeg | null;
    transfers: Transfer[];
    primaryTransfer: Transfer;

    airportChange: Transfer;
    terminalChange: Transfer;
    transfersChanges: Transfer[];

    showDash = false;

    ngOnChanges(changes: SimpleChanges): void {
        if (!changes.flights || !this.flights) {
            return;
        }

        this.routeType = this.getRouteType();
        this.transfers = this.logic.getTransfers(this.flights, this.routeType);
        this.primaryTransfer = this.logic.getPrimaryTransfer(this.transfers);

        this.transfersChanges = this.getTransferChanges();

        this.airportChange = this.transfers.find(
            (t) => t.stationChange === 'airport',
        );
        this.terminalChange = this.transfers.find(
            (t) => t.stationChange === 'terminal',
        );

        this.showDash = this.shouldShowDash();
    }

    private getTransferChanges() {
        const stationChangesMap = new Map(
            this.transfers.map((transfer) => {
                return [transfer.stationChange, transfer];
            }),
        );

        return Array.from(stationChangesMap.values()).filter(
            (transfer) => transfer.stationChange !== 'noChange',
        );
    }

    private getRouteType():
        | RouteTypeLegacy.Connecting
        | RouteTypeLegacy.MultiLeg
        | null {
        if (!this.flights?.length) return null;

        return this.flights.length > 1
            ? RouteTypeLegacy.Connecting
            : RouteTypeLegacy.MultiLeg;
    }

    private shouldShowDash() {
        // show <transfer-section-wrapper ... >
        const canShowTransferSection = this.transfers.length === 1;
        // show <div class="multi-transfers" ... >
        const canShowFlightChanges =
            this.transfers.length > 1 &&
            (this.airportChange || this.terminalChange);
        return canShowTransferSection || !!canShowFlightChanges;
    }

    get shouldHideTransferDeparture() {
        return this.primaryTransfer.stationChange === 'noChange';
    }

    get transferIcon(): ITransferIcon {
        if (this.transfers.length > 1) {
            return 'multi-landings';
        }

        if (
            this.routeType === this.RouteType.MultiLeg &&
            this.transfers.length === 1
        ) {
            return 'intermediate-landing';
        }

        if (
            this.routeType === this.RouteType.Connecting &&
            this.transfers.length === 1
        ) {
            return 'flight-transfer';
        }
    }
}
