import { ChangeDetectionStrategy, Component, OnDestroy } from '@angular/core';
import { DeltagareCompact, DeltagareCompactData } from '@msfa-models/deltagare.model';
import { Sort } from '@msfa-models/sort.model';
import { DeltagareService } from '@msfa-services/deltagare.service';
import { Observable } from 'rxjs';

@Component({
  selector: 'msfa-deltagare-list',
  templateUrl: './deltagare-list.component.html',
  styleUrls: ['./deltagare-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DeltagareListComponent implements OnDestroy {
  allDeltagareData$: Observable<DeltagareCompactData> = this.deltagareService.allDeltagareData$;
  sort$: Observable<Sort<keyof DeltagareCompact>> = this.deltagareService.sort$;
  onlyMyDeltagare$: Observable<boolean> = this.deltagareService.onlyMyDeltagare$;
  deltagareLoading$: Observable<boolean> = this.deltagareService.deltagareLoading$;
  showUnauthorizedError$: Observable<boolean> = this.deltagareService.showUnauthorizedError$;

  constructor(private deltagareService: DeltagareService) {}

  ngOnDestroy(): void {
    this.deltagareService.resetParams();
  }

  handleDeltagareSort(key: keyof DeltagareCompact): void {
    this.deltagareService.setSort(key);
  }

  setOnlyMyDeltagare(checked: boolean): void {
    this.deltagareService.setOnlyMyDeltagare(checked);
  }

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