/**
 * Toast Manager
 * JavaScript implementation of the Blade toast/flash message system
 */

type ToastType = "success" | "error" | "info" | "warning";

interface ToastOptions {
    message: string;
    type: ToastType;
    duration?: number;
}

export class ToastManager {
    private static instance: ToastManager;
    private toastGroupEl: HTMLElement | null = null;

    private constructor() {
        // Initialize toast group container or create one if it doesn't exist
        document.addEventListener("DOMContentLoaded", () => {
            this.toastGroupEl = document.getElementById("toast-group");

            if (!this.toastGroupEl) {
                this.toastGroupEl = document.createElement("div");
                this.toastGroupEl.id = "toast-group";
                this.toastGroupEl.className = "fixed top-22 right-5 z-20";
                document.body.appendChild(this.toastGroupEl);
            }
        });
    }

    public static getInstance(): ToastManager {
        if (!ToastManager.instance) {
            ToastManager.instance = new ToastManager();
        }
        return ToastManager.instance;
    }

    public show({ message, type, duration = 5000 }: ToastOptions): void {
        // If toast container doesn't exist yet, create it
        if (!this.toastGroupEl) {
            this.toastGroupEl = document.getElementById("toast-group");
            if (!this.toastGroupEl) {
                this.toastGroupEl = document.createElement("div");
                this.toastGroupEl.id = "toast-group";
                this.toastGroupEl.className = "fixed top-22 right-5 z-20";
                document.body.appendChild(this.toastGroupEl);
            }
        }

        const toastId = `toast-${type}-${Date.now()}`;
        const iconName = this.getIconName(type);
        const bgColor = this.getBackgroundColor(type);

        const toastEl = document.createElement("div");
        toastEl.id = toastId;
        toastEl.className =
            "flex items-center w-full max-w-xs p-3 mb-4 text-foreground bg-white border-2 border-gray-200 rounded-lg shadow-sm dark:bg-gray-800 dark:border-gray-400";
        toastEl.setAttribute("role", "alert");

        toastEl.innerHTML = `
      <div class="inline-flex items-center justify-center shrink-0 w-8 h-8 rounded-lg text-white ${bgColor}">
        <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true">
          ${this.getIconSvgPath(iconName)}
        </svg>
      </div>
      <div class="ms-3 mr-2 text-sm font-medium">${message}</div>
      <button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg p-1.5 inline-flex items-center justify-center h-8 w-8 dark:hover:bg-gray-700 dark:hover:text-white" aria-label="Close">
        <svg class="size-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
        </svg>
      </button>
    `;

        // Add the toast to the container
        this.toastGroupEl.appendChild(toastEl);

        // Set up close button
        const closeButton = toastEl.querySelector("button");
        if (closeButton) {
            closeButton.addEventListener("click", () => {
                toastEl.remove();
            });
        }

        // Auto remove after duration
        if (duration > 0) {
            setTimeout(() => {
                if (toastEl.parentNode) {
                    toastEl.remove();
                }
            }, duration);
        }
    }

    private getIconName(type: ToastType): string {
        switch (type) {
            case "success":
                return "check-circle";
            case "error":
                return "close-circle";
            case "info":
                return "info-circle";
            case "warning":
                return "exclamation-circle";
            default:
                return "info-circle";
        }
    }

    private getBackgroundColor(type: ToastType): string {
        switch (type) {
            case "success":
                return "bg-green-500";
            case "error":
                return "bg-red-500";
            case "info":
                return "bg-blue-500";
            case "warning":
                return "bg-yellow-500";
            default:
                return "bg-blue-500";
        }
    }

    private getIconSvgPath(iconName: string): string {
        switch (iconName) {
            case "check-circle":
                return '<path fill-rule="evenodd" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 14.5l-4-4 1.4-1.4 2.6 2.6 6.6-6.6 1.4 1.4-8 8z" clip-rule="evenodd"></path>';
            case "close-circle":
                return '<path fill-rule="evenodd" d="M12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm4.3 14.3c-.39.39-1.02.39-1.41 0L12 13.41l-2.89 2.89c-.39.39-1.02.39-1.41 0-.39-.39-.39-1.02 0-1.41L10.59 12 7.7 9.11c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0L12 10.59l2.89-2.89c.39-.39 1.02-.39 1.41 0 .39.39.39 1.02 0 1.41L13.41 12l2.89 2.89c.38.38.38 1.02 0 1.41z" clip-rule="evenodd"></path>';
            case "info-circle":
                return '<path fill-rule="evenodd" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 15c-.55 0-1-.45-1-1v-4c0-.55.45-1 1-1s1 .45 1 1v4c0 .55-.45 1-1 1zm1-8h-2V7h2v2z" clip-rule="evenodd"></path>';
            case "exclamation-circle":
                return '<path fill-rule="evenodd" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 15v-2h2v2h-2zm0-10v6h2V7h-2z" clip-rule="evenodd"></path>';
            default:
                return '<path fill-rule="evenodd" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 15c-.55 0-1-.45-1-1v-4c0-.55.45-1 1-1s1 .45 1 1v4c0 .55-.45 1-1 1zm1-8h-2V7h2v2z" clip-rule="evenodd"></path>';
        }
    }
}

// Export a singleton instance
export const toastManager = ToastManager.getInstance();

export default toastManager;
