import { Component, OnInit } from '@angular/core';
import { ModalDialogParams, RouterExtensions } from '@nativescript/angular';
import { formattedGenEventEventRel, formattedGenEventSchedule, ScheduleType } from '~/pages/event/types/GenEventSearch';
import { GenEventSearch } from '~/pages/event/state-mgmt/gen/gen-event-search-list.state';
import { Picker, getPickerFormattedYearsAfter, formatPickerValues } from '~/pages/event/util';



const currentYear = new Date().getFullYear()
const startYear = 1980
const numberOfYears = currentYear - startYear + 11
@Component({
    selector: 'GenEventSearchModalComponent',
    moduleId: module.id,
    templateUrl: './gen-event-search-modal.component.html',
})
export class GenEventSearchModalComponent implements OnInit {

    searchParams: GenEventSearch;

    formattedTypes: Array<Picker> = [{ key: null, label: 'vše' }]
    formattedScopes: Array<Picker> = [{ key: null, label: 'vše' }]
    formattedStates: Array<Picker> = [{ key: null, label: 'vše' }]
    formattedYears: Array<Picker> = getPickerFormattedYearsAfter(startYear, numberOfYears);
    formattedEventRel: Array<Picker> = formattedGenEventEventRel;
    formattedSchedule: Array<Picker> = formattedGenEventSchedule;

    constructor(
        private routerExtensions: RouterExtensions,
        private params: ModalDialogParams,
    ) {
    }
    yearHidden = true;

    onFormChange(e) {
        this.yearHidden = this.searchParams.schedule !== ScheduleType.YEAR;
    }
    ngOnInit(): void {
        const { searchParams, types, states, scopes } = this.params.context;

        this.searchParams = { ...searchParams };

        this.yearHidden = this.searchParams.schedule !== ScheduleType.YEAR;
        this.formattedTypes = formatPickerValues(types);
        this.formattedStates = formatPickerValues(states, 'všechny nezružené');
        this.formattedScopes = formatPickerValues(scopes);
    }
    close() {
        this.params.closeCallback();
    }

    submit() {
        const { type, scope, state, year, schedule, relation } = this.searchParams
        this.params.closeCallback({
            year, type,
            scope,
            state,
            schedule, relation

        });
    }

    onBackButton(): void {
        this.routerExtensions.back();
    }
}