import { Component, OnInit, OnDestroy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Subscription } from 'rxjs';
import { SnackNotifyService } from './snack-notify.service';
import { SnackNotifyComponent } from './snack-notify.component';
import { SnackNotifyItem, SnackNotifyPosition } from './snack-notify.interface';

@Component({
  selector: 'app-snack-notify-container',
  standalone: true,
  imports: [CommonModule, SnackNotifyComponent],
  templateUrl: './snack-notify-container.component.html',
  styleUrls: ['./snack-notify-container.component.scss']
})
export class SnackNotifyContainerComponent implements OnInit, OnDestroy {
  notifications: SnackNotifyItem[] = [];
  private subscription: Subscription = new Subscription();

  constructor(private snackNotifyService: SnackNotifyService) {}

  ngOnInit(): void {
    // Inscrever-se nas notificações
    this.subscription.add(
      this.snackNotifyService.getNotifications().subscribe(notifications => {
        this.notifications = notifications;
      })
    );
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }

  /**
   * Obtém as classes CSS para o container baseado na posição
   */
  getContainerClasses(): string {
    // Por enquanto, usar posição padrão
    // Futuramente pode ser configurável globalmente
    const position: SnackNotifyPosition = 'top-right';
    return `snack-notify-container ${position}`;
  }

  /**
   * Manipula o fechamento de uma notificação
   */
  onNotificationClose(notificationId: string): void {
    this.snackNotifyService.dismiss(notificationId);
  }

  /**
   * TrackBy function para otimizar o *ngFor
   */
  trackByNotificationId(index: number, notification: SnackNotifyItem): string {
    return notification.id;
  }
} 