import { Injectable } from '@angular/core';
import { ScheduleApiService } from '@schedule/services/api.service';
import { IScheduleRouteDirectionParams } from '@schedule/services/url/types';
import ScheduleLegacyFactories from '@shared/factories/schedule.factory';
import { ScheduleItemModel } from '@shared/models-legacy';
import { ScheduleDateValidationService } from '@schedule/services/date-validation.service';

@Injectable()
export class ScheduleSearchDataSourceService {
    protected _scheduleLegacy: ScheduleItemModel[] = [];
    protected _returnScheduleLegacy: ScheduleItemModel[] = [];

    protected _loading = false;

    constructor(
        protected apiService: ScheduleApiService,
        protected dateValidator: ScheduleDateValidationService,
    ) {}

    get loading() {
        return this._loading;
    }

    get scheduleLegacy() {
        return this._scheduleLegacy;
    }

    get returnScheduleLegacy() {
        return this._returnScheduleLegacy;
    }

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

    public async loadFlightsByRoute(
        outbound: IScheduleRouteDirectionParams,
        inbound?: IScheduleRouteDirectionParams,
    ) {
        this._loading = true;

        outbound.dateFrom = await this.dateValidator.getMinValidDate(
            outbound.dateFrom,
        );

        outbound.dateTo = await this.dateValidator.getMaxValidDate(
            outbound.dateTo,
        );

        if (inbound) {
            inbound.dateFrom = await this.dateValidator.getMinValidDate(
                inbound.dateFrom,
            );

            inbound.dateTo = await this.dateValidator.getMaxValidDate(
                inbound.dateTo,
            );
        }

        const routePromise = this.apiService.getFlights(
            outbound,
            inbound ? 1 : undefined,
        );
        const returnRoutePromise = inbound
            ? this.apiService.getFlights(inbound, 2)
            : Promise.resolve([]);

        return Promise.all([routePromise, returnRoutePromise])
            .then((res) => {
                const [flights, returnFlights] = res;

                this._scheduleLegacy =
                    ScheduleLegacyFactories.createScheduleFromRoutes(
                        flights as any,
                        outbound.dateFrom,
                        outbound.dateTo,
                    );

                if (inbound) {
                    this._returnScheduleLegacy =
                        ScheduleLegacyFactories.createScheduleFromRoutes(
                            returnFlights as any,
                            inbound.dateFrom,
                            inbound.dateTo,
                        );
                } else {
                    this._returnScheduleLegacy = [];
                }
            })
            .catch(() => {
                this._scheduleLegacy = [];
                this._returnScheduleLegacy = [];
            })
            .finally(() => {
                this._loading = false;
            });
    }
}
