import { ConnectingFlightStrategy } from './connected-flight.strategy';
import { DirectFlightStrategy } from './direct-flight.strategy';
import { MultiLegFlightStrategy } from './multileg-fligh.strategy';
import { IFlightStrategy } from './flight.strategy';
import { RouteTypeLegacy } from '../enumerators';
import { RouteModel } from './route.model';
import { FallbackFlightStrategy } from './fallback-flight.strategy';
import { FlightModel, IArrivalStation, IDepartureStation } from '.';

const direct = new DirectFlightStrategy();
const connecting = new ConnectingFlightStrategy(getStrategyByType);
const multiLeg = new MultiLegFlightStrategy();
const fallback = new FallbackFlightStrategy();

export function getStrategy(flight: RouteModel): IFlightStrategy {
    return getStrategyByType(flight?.routeType);
}

export function getStrategyByType(type?: RouteTypeLegacy): IFlightStrategy {
    switch (type) {
        case RouteTypeLegacy.Connecting:
            return connecting;
        case RouteTypeLegacy.Direct:
            return direct;
        case RouteTypeLegacy.MultiLeg:
            return multiLeg;
        default:
            return fallback;
    }
}
export function getArrival(
    flight: FlightModel,
    property: keyof Pick<IArrivalStation, 'scheduled' | 'latest'> = 'scheduled',
) {
    return getLastLeg(flight).arrival[property]?.cityCode;
}
export function getDeparture(
    flight: FlightModel,
    property: keyof Pick<
        IDepartureStation,
        'scheduled' | 'latest'
    > = 'scheduled',
) {
    return getFirstLeg(flight).departure[property]?.cityCode;
}
export function getAllFlights(flights: RouteModel[]) {
    return (flights = flights
        .map((f) => getFlights(f))
        .reduce((a, c) => a.concat(c)));
}
export function getAllLegs(flights: RouteModel[]) {
    return flights.reduce((a, c) => a.concat(getLegs(c)), []);
}
export function getLegs(flight: RouteModel) {
    return getStrategy(flight).getLegs(flight);
}
export function getFlights(flight: RouteModel) {
    return getStrategy(flight).getFlights(flight);
}
export function getFirstLeg(flight: RouteModel) {
    return getStrategy(flight).getFirstLeg(flight);
}
export function getCurrentLeg(flight: RouteModel) {
    return getStrategy(flight).getCurrentLeg(flight);
}
export function getLastLeg(flight: RouteModel) {
    return getStrategy(flight).getLastLeg(flight);
}
export function getFirstFlight(flight: RouteModel) {
    return getStrategy(flight).getFirstFlight(flight);
}
export function getLastFlight(flight: RouteModel) {
    return getStrategy(flight).getLastFlight(flight);
}
export function getFlightId(flight: RouteModel) {
    return getStrategy(flight).getFlightId(flight);
}

const DeprecatedFlightUtils = {
    getFirstLeg,
};

export default DeprecatedFlightUtils;
