//
//  ToastView.swift
//  lichunWebsocket
//
//  Toast notification component
//

import SwiftUI

enum ToastType {
    case success
    case error
    case info
    case warning
    case message

    var color: Color {
        switch self {
        case .success: return AppColors.success
        case .error: return AppColors.error
        case .info: return AppColors.info
        case .warning: return AppColors.warning
        case .message: return AppColors.primary
        }
    }

    var icon: String {
        switch self {
        case .success: return "checkmark.circle.fill"
        case .error: return "xmark.circle.fill"
        case .info: return "info.circle.fill"
        case .warning: return "exclamationmark.triangle.fill"
        case .message: return "bubble.left.fill"
        }
    }
}

struct ToastView: View {
    let message: String
    let type: ToastType
    @Binding var isShowing: Bool
    var duration: Double = 2.0

    var body: some View {
        VStack {
            if isShowing {
                HStack(spacing: AppSpacing.sm) {
                    // Icon with soft glow background
                    ZStack {
                        Circle()
                            .fill(type.color.opacity(0.15))
                            .frame(width: 32, height: 32)

                        Image(systemName: type.icon)
                            .foregroundColor(type.color)
                            .font(.system(size: 16, weight: .semibold))
                    }

                    Text(message)
                        .font(.appBody)
                        .foregroundColor(AppColors.primaryText)
                        .lineLimit(3)

                    Spacer()

                    Button(action: { isShowing = false }) {
                        Image(systemName: "xmark.circle.fill")
                            .foregroundColor(AppColors.secondaryText.opacity(0.5))
                            .font(.system(size: 20))
                    }
                }
                .padding(AppSpacing.md)
                .background(
                    ZStack {
                        // Soft warm background with gradient
                        RoundedRectangle(cornerRadius: AppSpacing.cornerRadius)
                            .fill(AppColors.cardBackground)

                        // Subtle gradient overlay
                        RoundedRectangle(cornerRadius: AppSpacing.cornerRadius)
                            .fill(
                                LinearGradient(
                                    colors: [
                                        Color.white.opacity(0.8),
                                        Color(hex: 0xFFF8F3).opacity(0.9)
                                    ],
                                    startPoint: .topLeading,
                                    endPoint: .bottomTrailing
                                )
                            )
                    }
                )
                .overlay(
                    RoundedRectangle(cornerRadius: AppSpacing.cornerRadius)
                        .stroke(type.color.opacity(0.2), lineWidth: 1.5)
                )
                .shadow(color: type.color.opacity(0.15), radius: 12, x: 0, y: 4)
                .shadow(color: .black.opacity(0.08), radius: 8, x: 0, y: 2)
                .padding(.horizontal, AppSpacing.md)
                .padding(.top, AppSpacing.xl)
                .transition(.move(edge: .top).combined(with: .opacity).combined(with: .scale(scale: 0.95, anchor: .center)))
                .onAppear {
                    DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
                        withAnimation(.spring(response: 0.4, dampingFraction: 0.8)) {
                            isShowing = false
                        }
                    }
                }
            }

            Spacer()
        }
        .animation(.spring(response: 0.4, dampingFraction: 0.8), value: isShowing)
    }
}

// MARK: - Preview
#if DEBUG
struct ToastView_Previews: PreviewProvider {
    static var previews: some View {
        VStack(spacing: AppSpacing.xl) {
            ToastView(message: "Success! Your changes have been saved.", type: .success, isShowing: .constant(true))
            ToastView(message: "Error: Something went wrong.", type: .error, isShowing: .constant(true))
            ToastView(message: "Info: Please check your connection.", type: .info, isShowing: .constant(true))
            ToastView(message: "Warning: Low energy!", type: .warning, isShowing: .constant(true))
        }
        .background(Color.black.edgesIgnoringSafeArea(.all))
    }
}
#endif
