import { Component, OnInit } from '@angular/core';
import { ModalDialogParams } from '@nativescript/angular';
import { dismissSoftKeybaord } from '~/core/utils/dismiss-keyboard';
import { EduEventMySearchFormData, EduEventSortFilter, formattedEduEventMyEventRel } from '~/pages/event/types/EduEventSearch';
import { formattedGenEventSchedule, ScheduleType } from '~/pages/event/types/GenEventSearch';
import { Picker, getPickerFormattedYearsAfter, formatPickerValues } from '~/pages/event/util';

const currentYear = new Date().getFullYear()
const startYear = 1980
const numberOfYears = currentYear - startYear + 11;


@Component({
    selector: 'EduEventSearchMyModal',
    moduleId: module.id,
    templateUrl: './edu-event-my-search-modal.component.html',
})
export class EduEventMySearchModalComponent implements OnInit {
    eventSearch: EduEventMySearchFormData;

    formattedEventTypes: Array<Picker> = [{ key: null, label: 'vše' }];
    formattedYears: Array<Picker> = getPickerFormattedYearsAfter(startYear, numberOfYears);
    formTouched: boolean = false;
    formattedEventRel: Array<Picker> = formattedEduEventMyEventRel;
    formattedSchedule: Array<Picker> = formattedGenEventSchedule;

    sortFilter: EduEventSortFilter;
    constructor(
        private params: ModalDialogParams,
    ) {
    }

    ngOnInit(): void {
        const { searchParams: { unit, year, type, relation, schedule },
            types
        } = this.params.context;

        this.eventSearch = {
            year, unit, relation, schedule,
            type: type && type.id,
        };

        this.formattedEventTypes = formatPickerValues(types);
    }

    yearHidden = true;

    onFormChange() {
        this.yearHidden = this.eventSearch.schedule !== ScheduleType.YEAR;
    }

    onSearch() {
        dismissSoftKeybaord();
        const { unit, schedule, relation, year, type } = this.eventSearch
        const selType = type && this.formattedEventTypes.find((t) => t.key === type.toString());

        const formRes: EduEventMySearchFormData = {
            year,
            type: selType && { id: selType.key, displayName: selType.label },
            relation,
            schedule,
            unit

        };
        this.params.closeCallback(formRes)
    }

    close() {
        this.params.closeCallback();
    }

    onBackButton(): void {
        this.params.closeCallback();
    }
}
