import { Injectable } from '@angular/core';
import { FlightModel, ListItem } from '@shared/models-legacy';
import { CreateLegacyFlightsService } from '@shared/services/create-legacy-flights.service';
import { ISimpleFlight } from '@typings/flight/flight';
import { IParsedFlightId } from '@typings/flight/flight-id';
import { OnlineBoardApiService } from './api.service';
import { IOnlineBoardRoutePageUrlParams } from './url';

@Injectable()
export class OnlineBoardDataSourceService {
    private _loading = false;
    private _partners: string[] = [];
    private _flights: ISimpleFlight[] = [];
    private _flight: ISimpleFlight;

    private _flightsLegacy: ListItem<FlightModel>[] = [];
    private _flightLegacy: ListItem<FlightModel>;

    constructor(
        private apiService: OnlineBoardApiService,
        private createLegacyFlightsService: CreateLegacyFlightsService,
    ) {}

    public loadFlightsByFlightNumber(params: IParsedFlightId) {
        this._loading = true;

        this.apiService
            .getFlightsByFlightNumber(params)
            .then((res) => {
                var legacy = this.createLegacyFlightsService.createFlights(
                        res.data.routes as any,
                    );
                this._flights = res.data.routes;
                this._partners = res.data.partners;
                this._flightsLegacy = legacy;
            })
            .finally(() => {
                this._loading = false;
            });
    }

    public loadFlightsByRoute(params: IOnlineBoardRoutePageUrlParams, silent = false) {
        if (!silent) {
            this._loading = true;
        }

        this.apiService
            .getFlightsByRoute(params)
            .then((res) => {
                // this is slow lets precalculate it before updating datasource
                var legacy = this.createLegacyFlightsService.createFlights(
                        res.data.routes as any,
                    );

                this._flights = res.data.routes;
                this._partners = res.data.partners;

                this._flightsLegacy = legacy;
            })
            .catch(() => {
                console.log('catch');
                this._flights = this._flightsLegacy = [];
            })
            .finally(() => {
                if (!silent) {
                    this._loading = false;
                }
            });
    }

    public loadFlightByFlightNumber(params: IParsedFlightId, silent = false) {
        if (!silent) {
            this._loading = true;
        }

        return this.apiService
            .getFlightDetails(params)
            .then((res) => {
                this._flight = res.data.routes[0];
                this._flightLegacy =
                    this.createLegacyFlightsService.createFlights(
                        res.data.routes as any,
                    )[0];

                return res;
            })
            .finally(() => {
                if (!silent) {
                    this._loading = false;
                }
            });
    }

    public stopLoading() {
        this._loading = false;
    }

    get flights() {
        return this._flights;
    }

    get flight() {
        return this._flight;
    }

    get flightsLegacy() {
        return this._flightsLegacy;
    }

    get flightLegacy() {
        return this._flightLegacy;
    }

    get loading() {
        return this._loading;
    }

    get partners() {
        return this._partners;
    }
}
