import { Component, Input } from '@angular/core';
import { PhoneContactService } from '~/core/services/phone-contact.service';
import { PersonContactAllOutput } from '~/pages/search-person/entities/output/personContactAllOutput';

@Component({
    selector: 'SearchPersonDetailContacts',
    moduleId: module.id,
    templateUrl: './search-person-detail-contacts.component.html',
    styleUrls: ['./search-person-detail-contacts.component.scss']
})
export class SearchPersonDetailContactsComponent {
    _personContacts: PersonContactAllOutput[];

    get personContacts() {
        return this._personContacts;
    }

    @Input('personContacts')
    set personContacts(value: PersonContactAllOutput[]) {
        this._personContacts = value.filter(x => this.allowedContactTypes.indexOf(x.ID_ContactType) !== -1);
    }
    private allowedEmails = [
        // 'email_hlavni',
        'email',
        'email_matka',
        'email_otec'
    ];
    private allowedPhones = [
        // 'telefon_hlavni',
        // 'mobil',
        'telefon_jinam',
        'telefon_prace',
        'telefon_matka',
        'telefon_otec'
    ];
    private allowedContactTypes = [
        'banka',
        ...this.allowedEmails,
        ...this.allowedPhones,
        'facebook',
        'fax',
        'jabber',
        'ostatni',
        'skype',
        'twitter'
    ];

    constructor(
        private phoneContactSrv: PhoneContactService
    ) {
    }

    onCreateCall(phoneNumber: string) {
        this.phoneContactSrv.createCall(phoneNumber);
    }

    async onCreateSms(phoneNumber: string) {
        try {
            await this.phoneContactSrv.createSms(phoneNumber);
        } catch (e) {
            console.error(e);
        }
    }

    async onCreateEmail(emailAddress: string) {
        try {
            await this.phoneContactSrv.createEmail(emailAddress);
        } catch (e) {
            console.error(e);
        }
    }
}
