import { Component, EventEmitter, Output, ViewChild, Input } from '@angular/core';
import { PersonFormResult, SelectPersonFormComponent } from '../select-person-form/select-person-form.component';
import { Picker } from '~/pages/event/util';
import { FunctionTypeAllOutput } from '~/pages/search-person/entities/output/functionTypeAllOutput';
import { QualificationTypeAllOutput } from '~/pages/search-person/entities/output/qualificationTypeAllOutput';
import { EventData } from '@nativescript/core/ui/page';
import { Switch } from '@nativescript/core/ui/switch';


export interface PersonExtendedFormResult extends PersonFormResult {
    qualificationId: string;
    functionId: string;
    unitRegistrationNumber: string;
    onlyDirectUnits: boolean;
}

enum UnitSearchType {
    DIRECT = "",
    UNDER = "under",
    BYNUMBER = "byNumber",

}
@Component({
    selector: 'SelectPersonExtendedForm',
    moduleId: module.id,
    templateUrl: './select-person-extended-form.component.html',
})
export class SelectPersonExtendedFormComponent {
    @Output()
    onSearch = new EventEmitter<PersonExtendedFormResult>();

    @Input()
    personList: Picker[]

    formattedUnitTypes: Picker[] = [
        { key: UnitSearchType.DIRECT, label: 'Aktuální jednotce' },
        { key: UnitSearchType.UNDER, label: 'Podřízených jednotkách' },
        { key: UnitSearchType.BYNUMBER, label: 'Zadané jednotce' },
    ]
    regNumberHidden: boolean;

    @Input()
    set qualifications(val: QualificationTypeAllOutput[]) {
        this.formattedQualifications = [this.defaultValue, ...val.map(q => ({ key: q.ID.toString(), label: q.DisplayName }))]
    }
    @Input()
    set functions(val: FunctionTypeAllOutput[]) {
        this.formattedFunctions = [this.defaultValue, ...val.map(f => ({ key: f.ID.toString(), label: f.DisplayName }))]
    };

    extendedSearchForm = {
        qualificationId: null,
        functionId: null,
        unitRegistrationNumber: '',
        unitTypes: UnitSearchType.UNDER,
    }
    defaultValue: Picker = { key: '', label: 'Všechny' }

    formattedFunctions: Picker[] = [];
    formattedQualifications: Picker[] = [];

    @ViewChild('personForm', { static: false })
    personForm: SelectPersonFormComponent;

    showExtended = false;
    constructor(
    ) {
        this.regNumberHidden = this.extendedSearchForm.unitTypes !== UnitSearchType.BYNUMBER
    }

    handleSearch(event: PersonFormResult) {
        const { qualificationId, functionId, unitTypes, unitRegistrationNumber } = this.extendedSearchForm
        this.onSearch.emit({
            ...event, qualificationId, functionId,
            onlyDirectUnits: unitTypes === UnitSearchType.DIRECT || unitTypes === UnitSearchType.BYNUMBER,
            unitRegistrationNumber: unitTypes === UnitSearchType.BYNUMBER && unitRegistrationNumber

        })
    }
    get selectedPersonId(): string {
        return this.personForm.selectedPersonId
    }
    set selectedPersonId(id: string) {
        this.personForm.selectedPersonId = id
    }
    onFormChange(e) {
        this.regNumberHidden = this.extendedSearchForm.unitTypes !== UnitSearchType.BYNUMBER;
    }
    onCheckedChange(args: EventData) {
        const sw = args.object as Switch;
        this.showExtended = sw.checked;
    }
    clear() {
        this.personForm.clear();
    }
}