import { Component, OnInit } from '@angular/core';
import { SharedDataService } from 'src/app/hmg-common/services/shared-data-service/shared-data.service';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { ModalController } from '@ionic/angular';
import { OptionModel } from './option.model';

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


  public options: OptionModel[] ;
  public selectedOption: OptionModel;
  public searchField: string;
  constructor(
    public cs: CommonService,
    public ts: TranslatorService,
    public modalController: ModalController,
    public sharedData: SharedDataService) { }

  ngOnInit() {
    this.options = this.sharedData.getSharedData(OptionModel.SHARED_DATA, true);
    this.cs.stopLoading();
  }

  public searchFieldChanged() {
    // this.selectedOption = null;
  }

  public matches(option: OptionModel): boolean {
    if (this.searchField) {
      if (option.label) {
        return option.label.toLowerCase().indexOf(this.searchField.toLowerCase()) >= 0;
      } else {
        return false;
      }
    } else {
      return true;
    }
  }

  public noMatch(): boolean {
    for (const option of this.options) {
      if (this.matches(option)) {
        return false;
      }
    }
    return true;
  }

  public optionSelected(option) {
    this.selectedOption = option;
  }
  public selectionChanged(event) {
    // console.log (event);
  }

  public cancelSelection() {
    // this.showList = false;
    this.modalController.dismiss({
      'selection': null
    });
  }
  public submitSelection() {
    this.modalController.dismiss({
      'selection': this.selectedOption
    });

  }


}
