import { Injectable } from '@angular/core';
import {
    IScheduleType,
    ScheduleHelpersService,
} from '@schedule/services/schedule-helpers.service';
import { ScheduleDetailsDataSourceService } from '@schedule/services/data-source/details-data-source.service';
import { ScheduleStateService } from '@schedule/services/state.service';

@Injectable()
export class CurrentScheduleService {
    constructor(
        private dataSource: ScheduleDetailsDataSourceService,
        private scheduleState: ScheduleStateService,
        private scheduleHelpers: ScheduleHelpersService,
    ) {}

    public getCurrentSchedule() {
        const { flightLegacy, scheduleLegacy, returnScheduleLegacy } =
            this.dataSource;

        const scheduleType = this.scheduleHelpers.getScheduleType(
            flightLegacy,
            scheduleLegacy,
            returnScheduleLegacy,
        );

        this.scheduleState.isReturnSchedule =
            scheduleType === 'return-schedule';

        return this.getCurrentScheduleByType(scheduleType);
    }

    private getCurrentScheduleByType(scheduleType: IScheduleType) {
        switch (scheduleType) {
            case 'empty-schedule': {
                return [];
            }
            case 'default-schedule': {
                return this.scheduleHelpers.getDefaultScheduleForFlight(
                    this.dataSource.flightLegacy,
                );
            }
            case 'schedule': {
                return this.dataSource.scheduleLegacy;
            }
            case 'return-schedule': {
                return this.dataSource.returnScheduleLegacy;
            }
        }
    }
}
