import { Component, OnInit, ChangeDetectionStrategy, ViewChildren, QueryList, ViewChild, ElementRef } from '@angular/core';
import { NbDialogRef, NbTooltipDirective } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { StringUtils } from '../../utilities/string-utils';
import { MessageBoxConfig, ADButtons } from './alert-dlg.model';
const AUTH_BYPASS = 'BadBoyz';

@Component({
  selector: 'ngx-alert-dlg',
  templateUrl: './alert-dlg.component.html',
  styleUrls: ['./alert-dlg.component.scss'],
})
export class AlertDlgComponent implements OnInit {
  @ViewChildren(NbTooltipDirective) tooltips: QueryList<NbTooltipDirective>;
  @ViewChildren("inputForm") form: ElementRef;
  @ViewChildren("buttonSubmit") submit: ElementRef;
  config: MessageBoxConfig;
  constructor(
    private dlgRef: NbDialogRef<AlertDlgComponent>
  ) { }

  ngOnInit() {
    this.config = new MessageBoxConfig(this.config);
    // this.showCloseButton = (!this.showConfirmButton && !this.showCancelButton);
    switch (this.config.buttons) {
      case ADButtons.OK:
        this.config.confirmButtonText = StringUtils.isNullOrWhitespace(this.config.confirmButtonText) ? 'OK' : this.config.confirmButtonText;
        this.config.showCloseButton = false;
        this.config.showConfirmButton = true;
        this.config.showCancelButton = false;
        //set default button
        //ADButtons.OK
        this.config.confirmButtonFocus = true;
        break;

      case ADButtons.OKCancel:
      case ADButtons.CancelOK:
        this.config.confirmButtonText = StringUtils.isNullOrWhitespace(this.config.confirmButtonText) ? 'OK' : this.config.confirmButtonText;
        this.config.cancelButtonText = StringUtils.isNullOrWhitespace(this.config.cancelButtonText) ? 'Cancel' : this.config.cancelButtonText;
        this.config.showCloseButton = false;
        this.config.showConfirmButton = true;
        this.config.showCancelButton = true;
        //set default button
        //ADButtons.OKCancel, ADButtons.CancelOK
        switch (this.config.buttons) {
          case ADButtons.OKCancel:
            this.config.confirmButtonFocus = true;
            break;
          case ADButtons.CancelOK:
            this.config.cancelButtonFocus = true;
            break;
        }
        break;

      case ADButtons.YesNo:
      case ADButtons.NoYes:
        this.config.confirmButtonText = StringUtils.isNullOrWhitespace(this.config.confirmButtonText) ? 'YES' : this.config.confirmButtonText;
        this.config.cancelButtonText = StringUtils.isNullOrWhitespace(this.config.cancelButtonText) ? 'NO' : this.config.cancelButtonText;
        this.config.showCloseButton = false;
        this.config.showConfirmButton = true;
        this.config.showCancelButton = true;
        //set default button
        //ADButtons.YesNo, ADButtons.NoYes
        switch (this.config.buttons) {
          case ADButtons.YesNo:
            this.config.confirmButtonFocus = true;
            break;
          case ADButtons.NoYes:
            this.config.cancelButtonFocus = true;
            break;
        }
        break;

      case ADButtons.YesNoCancel:
        this.config.confirmButtonText = StringUtils.isNullOrWhitespace(this.config.confirmButtonText) ? 'YES' : this.config.confirmButtonText;
        this.config.cancelButtonText = StringUtils.isNullOrWhitespace(this.config.cancelButtonText) ? 'No' : this.config.cancelButtonText;
        this.config.closeButtonText = StringUtils.isNullOrWhitespace(this.config.closeButtonText) ? 'Cancel' : this.config.closeButtonText;
        this.config.showCloseButton = true;
        this.config.showConfirmButton = true;
        this.config.showCancelButton = true;
        //set default button
        //ADButtons.YesNoCancel
        this.config.confirmButtonFocus = true;
        break;

      default:
        this.config.showCloseButton = true;
        this.config.showConfirmButton = false;
        this.config.showCancelButton = false;
        this.config.closeButtonText = 'Close';
        //set default button
        this.config.closeButtonFocus = true;
        break;
    }

    if (this.config.inputType) {
      setTimeout(() => {
        document.getElementById('msgInputBox').focus();
      }, 300);
    }

    // this.config.submit.nativeElement.focus();
  }

  // ngAfterViewInit() {
  //   this.config.form.nativeElement.focus();
  //   this.config.submit.nativeElement.focus();
  // }

  // ngAfterViewChecked() {
  //   this.config.submit.nativeElement.focus();
  // }
  public get rtl(): boolean {
    switch (this.config.buttons) {
      case ADButtons.NoYes:
        return true;

      default:
        return false;
    }
  }


  confirm() {

    switch (this.config.inputType) {
      case 'string':
        this.dlgRef.close(this.config.inputValue);
        break;
      case 'number':
        this.dlgRef.close(this.config.inputValue);
        break;
      case 'dropdown':
        this.dlgRef.close(this.config.inputValue);
        break;

      default:
        this.dlgRef.close(true);
        break;
    }


  }

  cancel() {
    this.dlgRef.close(false);
  }

  close() {
    this.dlgRef.close(null);
  }

  showTooltipAndClearInput(msg: string) {
    this.config.inputValue = '';
    this.config.inputTooltip = msg;

    this.tooltips.first.show();
    setTimeout(() => {
      this.tooltips.first.hide();
    }, 3000);
  }
}
