import { ChangeDetectionStrategy, Component, HostBinding } from '@angular/core';
import { DomainTaskCalendarService } from '@domain/task-calendar';
import { DisplayInfoDTO } from '@swagger/eis';
import { UiModalRef } from '@ui/modal';
import { combineLatest, ReplaySubject } from 'rxjs';
import { map, shareReplay, tap } from 'rxjs/operators';

@Component({
  selector: 'page-preinfo-modal',
  templateUrl: 'preinfo-modal.component.html',
  styleUrls: ['preinfo-modal.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PreInfoModalComponent {
  info: DisplayInfoDTO;

  info$ = new ReplaySubject<DisplayInfoDTO>();

  processingStatus$ = this.info$.pipe(
    map((info) => this.domainTaskCalendarService.getProcessingStatusList(info)),
    shareReplay()
  );

  showOpenSuccessorCta$ = combineLatest([this.processingStatus$, this.info$]).pipe(
    map(([processingStatus, info]) => processingStatus.includes('Removed') && !!info.successor?.id)
  );

  showUpdateIcon$ = combineLatest([this.processingStatus$, this.info$]).pipe(
    map(([processingStatus, info]) => (info.successor && processingStatus.includes('Removed')) || info.predecessor)
  );

  @HostBinding('class')
  get statusClass() {
    return this.domainTaskCalendarService.getProcessingStatusList(this.info);
  }

  constructor(
    private modalRef: UiModalRef<{ successorId: number }, DisplayInfoDTO>,
    private domainTaskCalendarService: DomainTaskCalendarService
  ) {
    this.info = this.modalRef.data;
    this.info$.next(this.info);
  }

  close(successorId: number = undefined) {
    this.modalRef.close({ successorId });
  }
}
