import { Component, OnInit } from '@angular/core';
import * as faker from 'faker';
import { ModalController } from '@ionic/angular';

@Component({
    selector: 'app-add-party',
    templateUrl: './add-party.page.html',
    styleUrls: ['./add-party.page.scss'],
})
export class AddPartyPage implements OnInit {
    contactList: Array<{
        name: string,
        phone: string
    }> = [];

    tempContactList: Array<{
        name: string,
        phone: string
    }> = [];

    searchInput: string = '';

    selectedContacts: Array<string> = [];

    constructor(
        private modalController: ModalController
    ) { }

    back() {
        this.modalController.dismiss({
            createParty: false
        });
    }

    createParty() {
        this.modalController.dismiss({
            createParty: true
        });
    }

    ngOnInit() {
        
        for (let i = 0; i < 12; i += 1) {
            this.contactList.push({
                name: faker.name.findName(),
                phone: faker.phone.phoneNumber()
            });
        }

        this.tempContactList = JSON.parse(JSON.stringify(this.contactList));
        

    }

    searchContact() {
        this.contactList = this.tempContactList.filter((contact) => {
            return contact.phone.toString().toLowerCase().includes(this.searchInput.toLowerCase().trim()) ||
            contact.name.toString().toLowerCase().includes(this.searchInput.toLowerCase().trim());
        });
    }

    selectContact(phone: string) {
        if (this.selectedContacts.includes(phone)) {
            this.selectedContacts.splice(this.selectedContacts.indexOf(phone), 1);
        } else {
            this.selectedContacts.push(phone);
        }
    }

}
