import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { OnlineBoardDataSourceService } from '@online-board/services/data-source.service';
import { OnlineBoardNavigationService } from '@online-board/services/navigation.service';
import { IOnlineBoardRequest } from '@online-board/services/request/request-parser.service';
import { IOnlineBoardRouteData } from '@online-board/types';
import { ISimpleFlight } from '@typings/flight/flight';
import { IFlightId, IParsedFlightId } from '@typings/flight/flight-id';
import { RefreshService } from './services/refresh.service';
import FlightDateUtils from '@utils/flight/date';

@Component({
    selector: 'online-board-flight-details-page',
    templateUrl: './flight-details-page.component.html',
    providers: [RefreshService],
})
export class OnlineBoardFlightDetailsPageComponent
    implements OnInit, OnDestroy
{
    params: IParsedFlightId;
    requestQueryParam: string;

    constructor(
        public dataSource: OnlineBoardDataSourceService,
        private route: ActivatedRoute,
        private refreshService: RefreshService,
        private navigationService: OnlineBoardNavigationService,
    ) {}

    ngOnInit(): void {
        this.route.data.subscribe({
            next: (data: IOnlineBoardRouteData) => {
                if (data.urlParams?.details) {
                    this.handleUrlParamsChange(data.urlParams.details);
                }

                if (data.requestData) {
                    this.requestQueryParam = data.requestData.raw;

                    this.loadFlightsByRequest(data.requestData.parsed);
                }
            },
        });
    }

    ngOnDestroy() {
        this.refreshService.stop();
    }

    get searchDate() {
        return this.params.date;
    }

    handleDateChange(date: Date) {
        this.navigationService.toDetailsPage({
            flightId: this.dataSource.flight.flightId,
            date,
            request: this.requestQueryParam,
        });
    }

    handleOpenEvent(flight: ISimpleFlight) {
        this.navigationService.toDetailsPage({
            flightId: flight.flightId,
            date: FlightDateUtils.getFlightDate(flight),
            request: this.requestQueryParam,
        });
    }

    private handleUrlParamsChange(params: IParsedFlightId) {
        this.params = params;

        this.dataSource
            .loadFlightByFlightNumber(this.params)
            .then((res) => {
                if (!res.data.routes.length) {
                    return this.navigationService.toNotFound();
                }
                this.refreshService.start(res.data.routes[0].flightId);
            })
            .catch(() => {
                return this.navigationService.toNotFound();
            });
    }

    private loadFlightsByRequest(request: IOnlineBoardRequest) {
        switch (request.type) {
            case 'route': {
                this.dataSource.loadFlightsByRoute(request.params);
                return;
            }
            case 'flight-number': {
                this.dataSource.loadFlightsByFlightNumber(request.params);
                return;
            }
        }
    }
}
