//
//  SideBetToastView.swift
//  Blackjack
//
//  Animated toast notifications for side bet results that appear after dealing.
//

import SwiftUI
import CasinoKit

/// Toast notification for a single side bet result with built-in animation.
struct SideBetToastView: View {
    let title: String
    let result: String
    let isWin: Bool
    let amount: Int
    let showOnLeft: Bool
    
    @State private var isShowing = false
    
    @ScaledMetric(relativeTo: .caption) private var titleFontSize: CGFloat = Design.Size.sideBetToastTitleFontSize
    @ScaledMetric(relativeTo: .callout) private var resultFontSize: CGFloat = Design.Size.sideBetToastResultFontSize
    @ScaledMetric(relativeTo: .body) private var amountFontSize: CGFloat = Design.Size.sideBetToastAmountFontSize
    
    private var backgroundColor: Color {
        isWin ? Color.green.opacity(Design.Opacity.heavy) : Color.red.opacity(Design.Opacity.heavy)
    }
    
    private var borderColor: Color {
        isWin ? Color.green : Color.red
    }
    
    private var amountText: String {
        if amount > 0 {
            return "+$\(amount)"
        } else {
            return "-$\(abs(amount))"
        }
    }
    
    var body: some View {
        VStack(spacing: Design.Spacing.xxSmall) {
            // Title (PP or 21+3)
            Text(title)
                .font(.system(size: titleFontSize, weight: .bold, design: .rounded))
                .foregroundStyle(.white.opacity(Design.Opacity.strong))
            
            // Result
            Text(result)
                .font(.system(size: resultFontSize, weight: .semibold, design: .rounded))
                .foregroundStyle(.white)
                .lineLimit(1)
                .minimumScaleFactor(Design.MinScaleFactor.comfortable)
            
            // Amount
            Text(amountText)
                .font(.system(size: amountFontSize, weight: .black, design: .rounded))
                .foregroundStyle(isWin ? .yellow : .white)
        }
        .padding(.horizontal, Design.Spacing.small)
        .padding(.vertical, Design.Spacing.xSmall)
        .background(
            RoundedRectangle(cornerRadius: Design.CornerRadius.medium)
                .fill(backgroundColor)
                .overlay(
                    RoundedRectangle(cornerRadius: Design.CornerRadius.medium)
                        .strokeBorder(borderColor, lineWidth: Design.LineWidth.medium)
                )
        )
        .shadow(color: borderColor.opacity(Design.Opacity.medium), radius: Design.Shadow.radiusMedium)
        .scaleEffect(isShowing ? Design.Scale.normal : Design.Scale.shrunk)
        .opacity(isShowing ? 1.0 : 0)
        .offset(x: isShowing ? 0 : (showOnLeft ? -Design.Spacing.toastSlide : Design.Spacing.toastSlide))
        .accessibilityElement(children: .combine)
        .accessibilityLabel("\(title): \(result)")
        .accessibilityValue(amountText)
        .onAppear {
            withAnimation(.spring(duration: Design.Animation.springDuration, bounce: Design.AnimationExtra.toastBounce).delay(showOnLeft ? 0 : Design.Animation.staggerDelay1)) {
                isShowing = true
            }
        }
    }
}

// MARK: - Previews

#Preview("Win Toast Left") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        HStack {
            SideBetToastView(
                title: "PP",
                result: "Perfect Pair",
                isWin: true,
                amount: 625,
                showOnLeft: true
            )
            Spacer()
        }
        .padding()
    }
}

#Preview("Lose Toast Right") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        HStack {
            Spacer()
            SideBetToastView(
                title: "21+3",
                result: "No Hand",
                isWin: false,
                amount: -25,
                showOnLeft: false
            )
        }
        .padding()
    }
}

#Preview("Both Toasts") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        HStack {
            SideBetToastView(
                title: "PP",
                result: "Colored Pair",
                isWin: true,
                amount: 300,
                showOnLeft: true
            )
            Spacer()
            SideBetToastView(
                title: "21+3",
                result: "Flush",
                isWin: true,
                amount: 125,
                showOnLeft: false
            )
        }
        .padding()
    }
}

