import { ChangeDetectionStrategy, Component } from '@angular/core';
import { IconType } from '@msfa-enums/icon-type.enum';
import { EmployeeCompactResponse } from '@msfa-models/api/employee.response.model';
import { Employee, EmployeesData } from '@msfa-models/employee.model';
import { Sort } from '@msfa-models/sort.model';
import { EmployeeService } from '@msfa-services/api/employee.service';
import { BehaviorSubject, Observable } from 'rxjs';

@Component({
  selector: 'msfa-employees',
  templateUrl: './employees.component.html',
  styleUrls: ['./employees.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class EmployeesComponent {
  private _searchValue$ = new BehaviorSubject<string>('');
  onlyEmployeesWithoutAuthorization$: Observable<boolean> = this.employeeService.onlyEmployeesWithoutAuthorization$;
  employeesData$: Observable<EmployeesData> = this.employeeService.employeesData$;
  employeesLoading$: Observable<boolean> = this.employeeService.employeesLoading$;
  sort$: Observable<Sort<keyof EmployeeCompactResponse>> = this.employeeService.sort$;
  iconType = IconType;

  constructor(private employeeService: EmployeeService) {}

  get searchValue(): string {
    return this._searchValue$.getValue();
  }

  setSearchFilter(): void {
    this.employeeService.setSearchFilter(this.searchValue);
  }

  setSearchValue($event: CustomEvent<{ target: { value: string } }>): void {
    this._searchValue$.next($event.detail.target.value);
  }

  handleEmployeesSort(key: keyof EmployeeCompactResponse): void {
    this.employeeService.setSort(key);
  }

  setNewPage(page: number): void {
    this.employeeService.setPage(page);
  }

  setOnlyEmployeesWithoutAuthorization(checked: boolean): void {
    this.employeeService.setOnlyEmployeesWithoutAuthorization(checked);
  }

  setEmployeeToDelete(employee: Employee): void {
    this.employeeService.setEmployeeToDelete(employee);
  }
}
