import { Component, Inject, OnDestroy, OnInit } from '@angular/core';
import { OnlineBoardFilterService } from '@online-board/services/filter/filter.service';
import { IOnlineBoardRoutePageUrlParams } from '@online-board/services/url';
import { AppSettings } from '@shared/models-legacy';
import { APP_SETTINGS } from '@shared/services';
import {
    IOnlineBoardFilterSelectedTab,
    OnlineBoardFiltersStateService,
} from '@shared/services/filters/online-board-filters-state.service';
import { IArrowIconColor } from '@toolkit/icons/arrow-down/arrow-down-icon.component';
import { IParsedFlightId } from '@typings/flight/flight-id';
import { StateService } from '@shared/services/state.service';

type IAccordionToggleEvent = {
    index: number;
};

@Component({
    selector: 'online-board-filter',
    templateUrl: './online-board-filter.component.html',
})
export class OnlineBoardFilterComponent implements OnInit, OnDestroy {
    constructor(
        @Inject(APP_SETTINGS) public settings: AppSettings,
        private filtersService: OnlineBoardFilterService,
        private state: OnlineBoardFiltersStateService,
        private stateService: StateService,
    ) {
    }

    ngOnInit() {
        //this.settings.startValidationDatesUpdatingInterval();
        if (this.state.selectedTab == 'flight') {
            var saved_params = this.stateService.get("boardnumber");
            if (saved_params) {
                this.filtersService.toFlightNumberPage(saved_params);
            }
        }
        if (this.state.selectedTab == 'route') {
            var saved_params = this.stateService.get("boardroute");
            if (saved_params) {
                this.filtersService.toRoutePage(saved_params);
            }
        }
    }

    ngOnDestroy() {
        //this.settings.stopValidationDatesUpdatingInterval();
    }

    get selectedTab() {
        return this.state.selectedTab;
    }

    getIconColor(value: IOnlineBoardFilterSelectedTab): IArrowIconColor {
        return this.isSelected(value) ? 'gray' : 'blue';
    }

    isSelected(value: IOnlineBoardFilterSelectedTab) {
        return this.selectedTab === value;
    }

    handleOpen(event: IAccordionToggleEvent) {
        switch (event.index) {
            case 0: {
                this.state.selectedTab = 'flight';
                return;
            }
            case 1: {
                this.state.selectedTab = 'route';
                return;
            }
        }
    }

    handleClose() {
        this.state.clearSelectedTab();
    }

    handleFlightNumberSearch(params: IParsedFlightId) {
        return this.filtersService.toFlightNumberPage(params);
    }

    handleRouteSearch(params: IOnlineBoardRoutePageUrlParams) {
        return this.filtersService.toRoutePage(params);
    }
}
