import { Component } 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 { OnlineBoardFiltersStateService } from '@shared/services/filters/online-board-filters-state.service';
import { ISimpleFlight } from '@typings/flight/flight';
import { OnlineBoardRequestBuilderService } from '@online-board/services/request/request-builder.service';
import { FlightRequestType } from '@app/shared/enumerators/flight-request-type.enum';
import { IOnlineBoardRouteData } from '@online-board/types';
import { OnlineBoardFilterService } from '@online-board/services/filter/filter.service';
import { OnlineBoardApiService } from '@online-board/services/api.service';
import { IFlightId, IParsedFlightId } from '@typings/flight/flight-id';
import FlightDateUtils from '@utils/flight/date';

@Component({
    selector: 'flight-number-search-page',
    templateUrl: './flight-number-search-page.component.html',
})
export class FlightNumberSearchPageComponent {
    FlightRequestType = FlightRequestType;
    private params: IParsedFlightId;
    disabledDates: Date[];

    constructor(
        public dataSource: OnlineBoardDataSourceService,
        private apiService: OnlineBoardApiService,
        private route: ActivatedRoute,
        private navigationService: OnlineBoardNavigationService,
        private requestBuilder: OnlineBoardRequestBuilderService,
        private filtersService: OnlineBoardFilterService,
        private filtersStateService: OnlineBoardFiltersStateService,
    ) {}

    ngOnInit(): void {
        this.filtersStateService.selectedTab = 'flight';

        this.route.data.subscribe({
            next: (data: IOnlineBoardRouteData) => {
                if (data.urlParams?.flightNumber) {
                    this.params = data.urlParams.flightNumber;

                    this.dataSource.loadFlightsByFlightNumber(this.params);
                }
                this.updateCalendar();
            },
        });
    }

    async updateCalendar() {
        if (this.params.flightNumber.length == 4) {
            var date = new Date();
            date.setUTCHours(0,0,0,0);
            date.setDate(date.getDate() - 1);
            this.apiService
                .getFlightDaysByNumber(date, this.params.flightNumber)
                .then((res) => {
                    this.disabledDates = new Array();
                    for(var i=0;i<res.days.length;i++) {
                        if (res.days[i] == '0') {
                            this.disabledDates.push(new Date(date));
                        }
                        date.setDate(date.getDate() + 1);
                    }
                })
                .finally(() => {
                });
        } else {
            this.disabledDates = new Array();
        }
    }

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

    public handleRequestCancellation() {
        this.dataSource.stopLoading();
    }

    public handleSameUrlNavigation() {
        this.dataSource.loadFlightsByFlightNumber(this.params);
    }

    public handleDateChange(date: Date) {
        this.navigationService.toFlightNumberPage({
            ...this.params,
            date,
        });
    }

    public navigateToDetails(flight: ISimpleFlight) {
        const request = this.requestBuilder.buildFlightNumberRequest(
            this.flightId,
            FlightDateUtils.getFlightDate(flight),
        );

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

    private get flightId(): IFlightId {
        return {
            ...this.params,
            date: '',
        };
    }
}
