import {
  ChangeDetectionStrategy,
  Component,
  EventEmitter,
  Input,
  OnChanges,
  OnDestroy,
  Output,
  SimpleChanges,
} from '@angular/core';
import {
  DeltagareHandelse,
  DeltagareHandelseData,
  DeltagareHandelseMotivation,
} from '@msfa-models/deltagare-handelse.model';
import { DeltagareCompact } from '@msfa-models/deltagare.model';
import { PaginationMeta } from '@msfa-models/pagination-meta.model';
import { DeltagareHandelserService } from '@msfa-services/deltagare-handelser.service';
import { UiIconSize } from '@ui/icon/icon-size.enum';
import { UiIconType } from '@ui/icon/icon-type.enum';
import { BehaviorSubject, Observable } from 'rxjs';

@Component({
  selector: 'msfa-deltagare-list-handelser-dialog',
  templateUrl: './deltagare-list-handelser-dialog.component.html',
  styleUrls: ['./deltagare-list-handelser-dialog.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DeltagareListHandelserDialogComponent implements OnChanges, OnDestroy {
  @Input() deltagare: DeltagareCompact;
  @Output() closeDialog = new EventEmitter<void>();
  IconType = UiIconType;
  IconSize = UiIconSize;
  deltagareHandelserData$: Observable<DeltagareHandelseData>;
  private _activeHandelseMotivation$ = new BehaviorSubject<DeltagareHandelseMotivation>(null);

  constructor(private deltagareHandelserService: DeltagareHandelserService) {}

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

  ngOnChanges(changes: SimpleChanges): void {
    if (changes.deltagare && changes.deltagare.currentValue) {
      this.deltagareHandelserService.setGenomforandeReferens$(this.deltagare.genomforandeReferens);
      this.deltagareHandelserData$ = this.deltagareHandelserService.deltagareHandelserData$;
    }
  }

  get activeHandelseMotivation(): DeltagareHandelseMotivation {
    return this._activeHandelseMotivation$.getValue();
  }

  get heading(): string {
    return this.activeHandelseMotivation
      ? `Motivering till beslut: ${this.activeHandelseMotivation.title}`
      : `Händelser för ${this.deltagare.fullName}`;
  }

  getHandelseDate(handelse: DeltagareHandelse, handelseDate?: string): Date {
    return handelseDate === 'receivedDate' ? new Date(handelse.receivedDate) : new Date(handelse.effectDate);
  }

  currentResultStart(paginationMeta: PaginationMeta): number {
    return (paginationMeta?.page - 1) * paginationMeta?.limit + 1;
  }

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

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

  openMotivation(handelse: DeltagareHandelse): void {
    this._activeHandelseMotivation$.next({
      title: handelse.description,
      motivation:
        handelse.motivation ||
        'Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sed nemo magni in nihil dolores eum harum obcaecati, nam enim illo iusto aut expedita, animi, itaque voluptatibus. Nisi reprehenderit animi facilis!',
    });
  }

  closeMotivation(): void {
    this._activeHandelseMotivation$.next(null);
  }

  emitCloseDialog(): void {
    this._activeHandelseMotivation$.next(null);
    this.closeDialog.emit();
  }
}
