//
//  InsurancePopupView.swift
//  Blackjack
//
//  Modal popup for insurance decision when dealer shows an Ace.
//

import SwiftUI
import CasinoKit

struct InsurancePopupView: View {
    let betAmount: Int
    let balance: Int
    let onTake: () -> Void
    let onDecline: () -> Void
    let onNeverAsk: () -> Void
    
    @State private var showContent = false
    
    var body: some View {
        ZStack {
            // Dimmed background
            Color.black.opacity(Design.Opacity.medium)
                .ignoresSafeArea()
                .onTapGesture { } // Prevent taps passing through
            
            // Popup card
            VStack(spacing: Design.Spacing.large) {
                // Icon
                Image(systemName: "shield.fill")
                    .font(.system(size: Design.IconSize.xLarge))
                    .foregroundStyle(.yellow)
                
                // Title
                Text(String(localized: "INSURANCE?"))
                    .font(.system(size: Design.BaseFontSize.largeTitle, weight: .black, design: .rounded))
                    .foregroundStyle(.white)
                
                // Subtitle
                Text(String(localized: "Dealer showing Ace"))
                    .font(.system(size: Design.BaseFontSize.xxLarge))
                    .foregroundStyle(.white.opacity(Design.Opacity.strong))
                
                // Cost info
                Text(String(localized: "Cost: $\(betAmount) (half your bet)"))
                    .font(.system(size: Design.BaseFontSize.xxLarge))
                    .foregroundStyle(.white.opacity(Design.Opacity.medium))
                    .padding(.bottom, Design.Spacing.small)
                
                // Buttons
                VStack(spacing: Design.Spacing.medium) {
                    HStack(spacing: Design.Spacing.large) {
                        // Decline button
                        Button(action: onDecline) {
                            Text(String(localized: "No Thanks"))
                                .font(.system(size: Design.BaseFontSize.medium, weight: .semibold))
                                .foregroundStyle(.white)
                                .padding(.horizontal, Design.Spacing.xLarge)
                                .padding(.vertical, Design.Spacing.medium)
                                .background(
                                    Capsule()
                                        .fill(Color.red.opacity(Design.Opacity.heavy))
                                )
                        }
                        
                        // Accept button (only if can afford)
                        if balance >= betAmount {
                            Button(action: onTake) {
                                Text(String(localized: "Yes ($\(betAmount))"))
                                    .font(.system(size: Design.BaseFontSize.medium, weight: .bold))
                                    .foregroundStyle(.black)
                                    .padding(.horizontal, Design.Spacing.xLarge)
                                    .padding(.vertical, Design.Spacing.medium)
                                    .background(
                                        Capsule()
                                            .fill(
                                                LinearGradient(
                                                    colors: [Color.CasinoButton.goldLight, Color.CasinoButton.goldDark],
                                                    startPoint: .top,
                                                    endPoint: .bottom
                                                )
                                            )
                                    )
                            }
                        }
                    }
                    
                    // Don't Ask Again button
                    Button(action: onNeverAsk) {
                        Text(String(localized: "Don't Ask"))
                            .font(.system(size: Design.BaseFontSize.small, weight: .medium))
                            .foregroundStyle(.white.opacity(Design.Opacity.medium))
                            .padding(.horizontal, Design.Spacing.large)
                            .padding(.vertical, Design.Spacing.small)
                    }
                }
            }
            .padding(Design.Spacing.xLarge)
            .background(
                RoundedRectangle(cornerRadius: Design.CornerRadius.xLarge)
                    .fill(Color.CasinoModal.backgroundDark)
                    .shadow(color: .black.opacity(Design.Opacity.medium), radius: Design.Shadow.radiusXLarge)
            )
            .overlay(
                RoundedRectangle(cornerRadius: Design.CornerRadius.xLarge)
                    .strokeBorder(Color.yellow.opacity(Design.Opacity.light), lineWidth: Design.LineWidth.thin)
            )
            .scaleEffect(showContent ? Design.Scale.normal : Design.Scale.slightShrink)
            .opacity(showContent ? 1.0 : 0)
        }
        .onAppear {
            withAnimation(.spring(duration: Design.Animation.springDuration, bounce: Design.Animation.springBounce)) {
                showContent = true
            }
        }
        .accessibilityElement(children: .contain)
        .accessibilityAddTraits(.isModal)
    }
}

// MARK: - Previews

#Preview("Can Afford") {
    InsurancePopupView(
        betAmount: 500,
        balance: 4500,
        onTake: {},
        onDecline: {},
        onNeverAsk: {}
    )
}

#Preview("Cannot Afford") {
    InsurancePopupView(
        betAmount: 500,
        balance: 200,
        onTake: {},
        onDecline: {},
        onNeverAsk: {}
    )
}

