import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { EventPerson } from 'src/app/models/eventperson.model';
import { ModalController, NavParams } from '@ionic/angular';
import { ContactsService } from 'src/app/services/contacts.service';
import { ThemeService } from 'src/app/services/theme.service'

@Component({
  selector: 'app-attendees',
  templateUrl: './attendees.page.html',
  styleUrls: ['./attendees.page.scss'],
})
export class AttendeesPageModal implements OnInit {

  // Defined by the API
  contacts: EventPerson[];
  showLoader: boolean = false;
  selectedContact: EventPerson[] =[];
  eventPersons: EventPerson[];
  adding: "intervenient" | "CC";
  currentPath = window.location.pathname;
  
  taskParticipants:EventPerson[] = [];
  taskParticipantsCc:EventPerson[] = [];

  constructor(
    private modalCtrl: ModalController, 
    private contactsService: ContactsService,
    private navParams: NavParams,
    private modalController: ModalController,
    public ThemeService: ThemeService) {
      
      this.adding = this.navParams.get('adding');
      this.taskParticipants = this.navParams.get('taskParticipants');
      this.taskParticipantsCc = this.navParams.get('taskParticipantsCc');

    }

  ngOnInit() {
    this.fetchContacts("");
    
    if(this.taskParticipants == null || this.taskParticipants == undefined){
      this.taskParticipants = [];
    }

    if(this.taskParticipantsCc == null || this.taskParticipantsCc == undefined){
      this.taskParticipantsCc = [];
    }
    
  }
  
  ngOnChanges(event) {}

  save(){

    this.modalController.dismiss({
      'taskParticipants': this.taskParticipants,
      'taskParticipantsCc': this.taskParticipantsCc
    });
  }

  close() {
    this.modalController.dismiss(false);
  }

  onChange(evt: any) {
    this.fetchContacts(evt.detail.value);
  }

  filterSearchList(itm: EventPerson): boolean {

    const result =  this.taskParticipants.concat( this.taskParticipantsCc).find((contact, index)=>{
      
      if(contact.Name.toLocaleLowerCase() == itm.Name.toLocaleLowerCase() && contact.EmailAddress.toLocaleLowerCase() == itm.EmailAddress.toLocaleLowerCase()){
        index = index;
        return contact;
      }

    })
    
    return undefined == result;
    
  }

  remove(itm: EventPerson){

    if(this.adding == "intervenient"){

      this.taskParticipants = this.taskParticipants.filter((contact, index) =>{

        if(contact.Name.toLocaleLowerCase() != itm.Name.toLocaleLowerCase() && contact.EmailAddress.toLocaleLowerCase() != itm.EmailAddress.toLocaleLowerCase()){
          return contact;
        }
        return false;
  
      });

    } else if (this.adding == "CC") {

      this.taskParticipantsCc = this.taskParticipantsCc.filter((contact, index) =>{

        if(contact.Name.toLocaleLowerCase() != itm.Name.toLocaleLowerCase() && contact.EmailAddress.toLocaleLowerCase() != itm.EmailAddress.toLocaleLowerCase()){
          return contact;
        }
        return false;
  
      });
    }

  }

  async selectContact(itm: EventPerson){
    if(this.adding == "intervenient"){
      itm.IsRequired = true;
      this.taskParticipants.push(itm);
      
    } else if (this.adding == "CC") {
      itm.IsRequired = false;
      this.taskParticipantsCc.push(itm);
    }
  }

  async fetchContacts(filter: string) {
    this.showLoader = true;

    this.contactsService.getContacts(filter).subscribe(result =>
      {
        if (this.eventPersons != null)
        {
          this.eventPersons.forEach(attendee => {
            const index: number = result.findIndex((cont) => {
              return cont.EmailAddress.toLocaleLowerCase() == attendee.EmailAddress.toLocaleLowerCase()
            });
  
            result.splice(index, 1);
          });
        }

        this.contacts = result;
        this.showLoader = false;
      }
    );
  }

}