import { Component, OnInit } from '@angular/core';
import { ModalDialogParams, RouterExtensions } from '@nativescript/angular';
import { EventRel, ScheduleType } from '~/pages/event/types/GenEventSearch';
import { CampEventSearch } from '~/pages/event/state-mgmt/camp/camp-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: 'CampEventSearchModalComponent',
    moduleId: module.id,
    templateUrl: './camp-event-search-modal.component.html',
})
export class CampEventSearchModalComponent implements OnInit {

    searchParams: CampEventSearch;

    formattedStates: Array<Picker> = [{ key: null, label: 'vše' }]
    formattedYears: Array<Picker> = getPickerFormattedYearsAfter(startYear, numberOfYears);
    formattedEventRel: Array<Picker> = [
        { key: EventRel.ALL, label: 'vše' },
        { key: EventRel.ME_AND_MY_UNIT, label: 'Týkající se mě a mého střediska' },
        { key: EventRel.UNIT_PLACE, label: 'Střediska přímo podřízené mé jednotce' },
        { key: EventRel.ALL_UNIT_PLACE, label: 'Všechna střediska pod mou jednotkou ' },
    ];
    formattedSchedule: Array<Picker> = [
        { key: ScheduleType.ALL, label: 'vše' },
        { key: ScheduleType.YEAR, label: 'Rok konání' },
        { key: ScheduleType.PAST, label: 'Již proběhla' },
        { key: ScheduleType.FUTURE, label: 'Teprve proběhne' },
    ];

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

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

        this.searchParams = { ...searchParams };

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

    submit() {
        const { state, year, schedule, relation, location,
            name, registrationNumber } = this.searchParams
        this.params.closeCallback({
            state,
            year, schedule, relation, location, name, registrationNumber,
        });
    }

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