import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { ErrorSeverity } from '@msfa-enums/error-severity.enum';
import { SortOrder } from '@msfa-enums/sort-order.enum';
import { DeltagareCompact } from '@msfa-models/deltagare.model';
import { PaginationMeta } from '@msfa-models/pagination-meta.model';
import { Sort } from '@msfa-models/sort.model';
import { UiIconSize } from '@ui/icon/icon-size.enum';
import { UiIconType } from '@ui/icon/icon-type.enum';
import { BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
  selector: 'msfa-deltagare-list-table',
  templateUrl: './deltagare-list-table.component.html',
  styleUrls: ['./deltagare-list-table.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DeltagareListTableComponent {
  @Input() deltagare: DeltagareCompact[];
  @Input() paginationMeta: PaginationMeta;
  @Input() deltagareLoading: boolean;
  @Input() sort: Sort<keyof DeltagareCompact>;
  @Output() sorted = new EventEmitter<keyof DeltagareCompact>();
  @Output() paginated = new EventEmitter<number>();
  handelserDialogDeltagare$ = new BehaviorSubject<DeltagareCompact | null>(null);

  handelserDialogIsOpen$ = this.handelserDialogDeltagare$.pipe(map(deltagare => !!deltagare));

  IconType = UiIconType;
  IconSize = UiIconSize;
  errorSeverity = ErrorSeverity;

  columnHeaders: { label: string; key: keyof DeltagareCompact }[] = [
    { label: 'Namn', key: 'fullName' },
    {
      label: 'Tjänst',
      key: 'tjanst',
    },
    {
      label: 'Utförande verksamhet',
      key: 'utforandeVerksamhet',
    },
    {
      label: 'Startdatum',
      key: 'startdatumAvrop',
    },
    {
      label: 'Slutdatum',
      key: 'slutdatumAvrop',
    },
    {
      label: 'Genomförande-referens',
      key: 'genomforandeReferens',
    },
    {
      label: 'Status',
      key: 'genomforandeReferens',
    },
  ];

  orderType = SortOrder;

  get currentPage(): number {
    return this.paginationMeta.page;
  }

  get totalPage(): number {
    return this.paginationMeta?.totalPage;
  }

  get count(): number {
    return this.paginationMeta.count;
  }

  get currentResultStart(): number {
    return (this.currentPage - 1) * this.paginationMeta.limit + 1;
  }

  get currentResultEnd(): number {
    const end = this.currentResultStart + this.paginationMeta.limit - 1;
    return end < this.count ? end : this.count;
  }

  getAvropDate(deltagare: DeltagareCompact, avropDate?: string): Date {
    return avropDate === 'startdatumAvrop' ? new Date(deltagare.startdatumAvrop) : new Date(deltagare.slutdatumAvrop);
  }

  handleSort(key: keyof DeltagareCompact): void {
    this.sorted.emit(key);
  }

  setNewPage(page: number): void {
    this.paginated.emit(page);
  }

  openHandelser(deltagareCompact: DeltagareCompact): void {
    this.handelserDialogDeltagare$.next(deltagareCompact);
  }

  closeHandelser(): void {
    this.handelserDialogDeltagare$.next(null);
  }
}
