import * as keycode from '@angular/cdk/keycodes';
import { AfterViewInit, Component, EventEmitter, HostListener, Input, OnInit, Output } from '@angular/core';
import { Employee, EmployeeFullFilter } from '@app_models/employee';
import { EmployeeDataService } from '@app_modules/employee/employee-data.service';
import { AppBaseEntityWrapper } from '@app_services/app.baseentity.wrapper';
import { AppBaseEntityListWrapper } from '@app_services/app.baseentitylist.wrapper';
import { AppDataService } from '@app_services/app.data.service';
import * as Cnst from '@app_core/consts';
import { ClipboardService } from 'ngx-clipboard';
import { AuthorizeService } from '@app_core/services/authorize.service';
import { UINotifierService } from '@app_core/services/notification/uinotifier.service';
import { LoginPopupComponent } from '@app_core/components/login/login-popup/login-popup.component';
import { LocaleService } from '@app_core/services/localization/locale.service';

@Component({
  selector: 'app-employee-filter',
  templateUrl: './employee-filter.component.html',
  styleUrls: ['./employee-filter.component.scss'],
})
export class EmployeeFilterComponent implements OnInit, AfterViewInit {

  constructor(
    public employeeDataService: EmployeeDataService,
    public appDataService: AppDataService,
    public authService: AuthorizeService,
    public localeService: LocaleService,
    private clipboardService: ClipboardService,
    private notifierService: UINotifierService,
  ) { }
  @Input() public filterObject: EmployeeFullFilter;
  @Input() employeeDetails: AppBaseEntityWrapper<Employee>;
  @Input() employeeList: AppBaseEntityListWrapper<Employee>;
  @Output() public filterFetch = new EventEmitter();

  public dropDownAllCaption = Cnst.EN_DropDownConst4Filter.AllCaption;
  public dropDownAllValueString = Cnst.EN_DropDownConst4Filter.AllValueString;
  public dropDownAllValueInt = Cnst.EN_DropDownConst4Filter.AllValueInt;
  public allCaption = Cnst.EN_DropDownConst4Filter.AllCaption;
  public allValue: number = Cnst.EN_DropDownConst4Filter.AllValueInt;
  public noneCaption = Cnst.EN_DropDownConst4Filter.NoneCaption;
  public noneValue = Cnst.EN_DropDownConst4Filter.NoneValueInt;

  ngOnInit() {
  }
  ngAfterViewInit() { }

  onFilterClick() {
    if (this.employeeDetails.inViewMode) this.filterFetch.emit();
  }

  onClearClick() {
    this.filterObject.filterReset();
    this.filterObject.isActive = true;
    this.onFilterClick();
  }




  onEmailClick() {
    const listEmployeeIds = this.employeeList.items.filter((empl: Employee) => !!empl.email).map((empl: Employee) => empl.entityId);
    const listEmails = this.employeeList.items.sort((empl1, empl2) => empl1.lastName.localeCompare(empl2.lastName)).map((empl: Employee) => empl.email).filter(email => !!email);
    if (listEmails.length === 0) return;
    this.clipboardService.copy(listEmails.join('; '));
    const endung = (listEmails.length > 1 ? 'n' : '');
    this.notifierService.showMessage(`${listEmails.length} Emailadresse${endung} wurde${endung} in die Zwischenablage gespeichert`);
  }


  @HostListener('window:keydown', ['$event'])
  keyboardInput(event: any) {
    if (!LoginPopupComponent.LOGIN_IS_SHOWN && !this.employeeDetails.keyDownListenerStopped) {
      if (event.ctrlKey) {
        switch (event.which) {
          case keycode.F: //Ctrl + 'f'
            event.stopPropagation();
            this.onFilterClick();
            return false; //processed
        }
      }
    }
    return true; //default processing
  }

}
