import { ChangeDetectionStrategy, Component } from '@angular/core';
import { AvropCompact, AvropCompactData } from '@msfa-models/avrop.model';
import { Handledare } from '@msfa-models/handledare.model';
import { AvropService } from '@msfa-services/avrop.service';
import { Observable } from 'rxjs';

@Component({
  selector: 'msfa-avrop',
  templateUrl: './avrop.component.html',
  styleUrls: ['./avrop.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AvropComponent {
  readonly totalAmountOfSteps = 3;
  currentStep$: Observable<number> = this.avropService.currentStep$;

  error$: Observable<string> = this.avropService.error$;
  avropData$: Observable<AvropCompactData> = this.avropService.avropData$;
  selectedAvrop$: Observable<AvropCompact[]> = this.avropService.selectedAvrop$;
  availableHandledare$: Observable<Handledare[]> = this.avropService.availableHandledare$;
  selectedHandledare$: Observable<Handledare> = this.avropService.selectedHandledare$;
  avropIsLocked$: Observable<boolean> = this.avropService.avropIsLocked$;
  handledareConfirmed$: Observable<boolean> = this.avropService.handledareIsConfirmed$;
  avropIsSubmitted$: Observable<boolean> = this.avropService.avropIsSubmitted$;
  avropLoading$: Observable<boolean> = this.avropService.avropLoading$;

  constructor(private avropService: AvropService) {}

  updateSelectedAvrop(deltagareList: AvropCompact[], currentStep: number): void {
    if (currentStep !== 1 && !deltagareList.length) {
      this.avropService.goToStep1();
    } else {
      this.avropService.setSelectedAvrop(deltagareList);
    }
  }

  lockSelectedAvrop(): void {
    this.avropService.lockSelectedAvrop();
  }

  unlockSelectedAvrop(): void {
    this.avropService.unlockSelectedAvrop();
  }

  confirmHandledare(): void {
    this.avropService.confirmHandledare();
  }

  unconfirmHandledare(): void {
    this.avropService.unconfirmHandledare();
  }

  async save(): Promise<void> {
    return this.avropService.save();
  }

  changeHandledare(handledareId: string): void {
    this.avropService.assignHandledare(handledareId);
  }

  returnToStep1(): void {
    this.avropService.goToStep1();
  }

  resetError(): void {
    this.avropService.resetError();
  }

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