import {
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  Input,
  OnDestroy,
  Output,
  ViewChild,
  inject,
} from '@angular/core';
import { ModalAnimationService } from '@shared/services/modal-animation.service';
import gsap from 'gsap';

@Component({
  selector: 'app-confirmation',
  standalone: true,
  imports: [],
  templateUrl: './confirmation.component.html',
})
export class ConfirmationComponent implements AfterViewInit, OnDestroy {
  @Input() successful = true;
  @Output() closeConfirmation = new EventEmitter<void>();
  @ViewChild('modalBg') modalBg!: ElementRef;
  @ViewChild('modalCard') modalCard!: ElementRef;

  private modalAnimationService = inject(ModalAnimationService);

  ngAfterViewInit() {
    if (this.successful) {
      this.openModal();
    }
  }

  ngOnDestroy() {
    gsap.killTweensOf([this.modalBg?.nativeElement, this.modalCard?.nativeElement]);
  }

  private openModal() {
    this.modalAnimationService.openModal(this.modalCard.nativeElement, this.modalBg.nativeElement);
  }

  public closeModal() {
    this.modalAnimationService.closeModal(
      this.modalCard.nativeElement,
      this.modalBg.nativeElement,
      () => this.closeConfirmation.emit()
    );
  }
}
