import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ListViewEventData } from 'nativescript-ui-listview';
import { PhoneContactService } from '~/core/services/phone-contact.service';
import { MySearchPerson } from '~/pages/search-person/entities/my-search-person';

@Component({
    selector: 'SearchPersonListView',
    moduleId: module.id,
    templateUrl: './search-person-list-view.component.html',
    styleUrls: ['./search-person-list-view.component.scss']
})
export class SearchPersonListViewComponent {
    @Input() searchPersons: MySearchPerson[];
    @Output() showPerson = new EventEmitter<MySearchPerson>();

    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);
        }
    }

    templateSelector(item: any, index: number, items: any): string {
        return 'default';
    }

    onItemTap(event: ListViewEventData) {
        this.showPerson.emit(this.searchPersons[event.index]);
    }
}
