import { Component, EventEmitter, Output, Input } from '@angular/core';
import { action } from '@nativescript/core/ui/dialogs';
import { ParticipantSearchCriteria, ParticipantSearchOptions } from '~/pages/event/types/ParticipantSearchCriteria';
import { Picker } from '~/pages/event/util';

export interface PersonFormResult {
    criteria: string,
    text: string
}

@Component({
    selector: 'SelectPersonForm',
    moduleId: module.id,
    templateUrl: './select-person-form.component.html',
})
export class SelectPersonFormComponent {
    @Output()
    onSearch = new EventEmitter<PersonFormResult>();
    empty = { key: '', label: 'Žádná data' }
    hasNoData = true;

    @Input()
    set personList(val: Picker[]) {
        if (this.personForm.person && val.length === 0) {
            this.formattedPeople = [{ key: '', label: 'Nebyla nalezena žádná osoba.' }]
            this.hasNoData = true;
        } else if (val.length > 0) {
            this.isPersonPickerHidden = false;
            this.formattedPeople = val;
            this.hasNoData = false;
        } else {
            this.hasNoData = true;
            this.formattedPeople = [this.empty]
        }
    }

    personId = null;
    personForm = { person: '' }

    formattedPeople = []

    isPersonPickerHidden = true
    clear() {
        this.personForm = { person: '' }
    }
    onSearchClick() {
        let options = {
            title: "Vyhledat podle",
            message: "Vyber",
            cancelButtonText: "Storno",
            actions: ParticipantSearchCriteria.map(v => v.label)
        };

        action(options).then((result) => {
            if (result) {
                const selectedCrit = ParticipantSearchCriteria.find(c => c.label === result)
                const criteria = ParticipantSearchOptions[selectedCrit.key]
                this.onSearch.emit({ text: this.personForm.person, criteria })
            }

        });

    }
    get selectedPersonId(): string {
        return this.personId;
    }
    set selectedPersonId(id: string) {
        this.personId = id;
    }


}