//
//  GameOverView.swift
//  Baccarat
//
//  Game over screen shown when player runs out of money.
//

import SwiftUI
import CasinoKit

/// Game over screen shown when player runs out of money.
struct GameOverView: View {
    let roundsPlayed: Int
    let onPlayAgain: () -> Void
    
    @State private var showContent = false
    @Environment(\.horizontalSizeClass) private var horizontalSizeClass
    
    /// Maximum width for the modal card on iPad
    private var maxModalWidth: CGFloat {
        horizontalSizeClass == .regular ? Design.Size.maxModalWidth : .infinity
    }
    
    // MARK: - Scaled Font Sizes (Dynamic Type)
    
    @ScaledMetric(relativeTo: .largeTitle) private var iconSize: CGFloat = Design.BaseFontSize.display
    @ScaledMetric(relativeTo: .largeTitle) private var titleFontSize: CGFloat = Design.BaseFontSize.largeTitle
    @ScaledMetric(relativeTo: .body) private var messageFontSize: CGFloat = Design.BaseFontSize.xLarge
    @ScaledMetric(relativeTo: .body) private var statsFontSize: CGFloat = 17
    @ScaledMetric(relativeTo: .headline) private var buttonFontSize: CGFloat = Design.BaseFontSize.xLarge
    
    // MARK: - Layout Constants
    
    private let modalCornerRadius = Design.CornerRadius.xxxLarge
    private let statsCornerRadius = Design.CornerRadius.large
    private let cardPadding = Design.Spacing.xxxLarge
    private let contentSpacing: CGFloat = 28
    private let buttonHorizontalPadding: CGFloat = 48
    private let buttonVerticalPadding: CGFloat = 18
    
    // MARK: - Body
    
    var body: some View {
        ZStack {
            // Solid dark backdrop - fully opaque
            Color.black
                .ignoresSafeArea()
            
            // Modal card
            modalContent
        }
        .onAppear {
            withAnimation(.spring(duration: Design.Animation.springDuration, bounce: Design.Animation.springBounce)) {
                showContent = true
            }
        }
        .accessibilityElement(children: .contain)
        .accessibilityLabel(String(localized: "Game Over"))
        .accessibilityAddTraits(.isModal)
    }
    
    // MARK: - Private Views
    
    private var modalContent: some View {
        VStack(spacing: contentSpacing) {
            // Broke icon
            Image(systemName: "creditcard.trianglebadge.exclamationmark")
                .font(.system(size: iconSize))
                .foregroundStyle(.red)
                .symbolEffect(.pulse, options: .repeating)
            
            // Title
            Text("GAME OVER")
                .font(.system(size: titleFontSize, weight: .black, design: .rounded))
                .foregroundStyle(.white)
            
            // Message
            Text("You've run out of chips!")
                .font(.system(size: messageFontSize, weight: .medium))
                .foregroundStyle(.white.opacity(Design.Opacity.strong))
            
            // Stats card
            statsCard
            
            // Play Again button
            playAgainButton
        }
        .padding(cardPadding)
        .background(modalBackground)
        .shadow(color: .red.opacity(Design.Opacity.hint), radius: Design.Shadow.radiusXXLarge)
        .frame(maxWidth: maxModalWidth)
        .padding(.horizontal, Design.Spacing.xxLarge)
        .scaleEffect(showContent ? Design.Scale.normal : Design.Scale.slightShrink)
        .opacity(showContent ? 1.0 : 0)
    }
    
    private var statsCard: some View {
        VStack(spacing: Design.Spacing.medium) {
            HStack {
                Text("Rounds Played")
                    .foregroundStyle(.white.opacity(Design.Opacity.medium))
                Spacer()
                Text("\(roundsPlayed)")
                    .bold()
                    .foregroundStyle(.white)
            }
        }
        .font(.system(size: statsFontSize))
        .padding()
        .background(
            RoundedRectangle(cornerRadius: statsCornerRadius)
                .fill(Color.white.opacity(Design.Opacity.subtle))
                .overlay(
                    RoundedRectangle(cornerRadius: statsCornerRadius)
                        .strokeBorder(Color.white.opacity(Design.Opacity.subtle), lineWidth: Design.LineWidth.thin)
                )
        )
        .padding(.horizontal, Design.Spacing.xLarge)
    }
    
    private var playAgainButton: some View {
        Button {
            onPlayAgain()
        } label: {
            HStack(spacing: Design.Spacing.small) {
                Image(systemName: "arrow.counterclockwise")
                Text("Play Again")
            }
            .font(.system(size: buttonFontSize, weight: .bold))
            .foregroundStyle(.black)
            .padding(.horizontal, buttonHorizontalPadding)
            .padding(.vertical, buttonVerticalPadding)
            .background(
                Capsule()
                    .fill(
                        LinearGradient(
                            colors: [Color.Button.goldLight, Color.Button.goldDark],
                            startPoint: .top,
                            endPoint: .bottom
                        )
                    )
            )
            .shadow(color: .yellow.opacity(Design.Opacity.light), radius: Design.Shadow.radiusXLarge)
        }
        .padding(.top, Design.Spacing.medium)
    }
    
    private var modalBackground: some View {
        RoundedRectangle(cornerRadius: modalCornerRadius)
            .fill(
                LinearGradient(
                    colors: [Color.Modal.backgroundLight, Color.Modal.backgroundDark],
                    startPoint: .top,
                    endPoint: .bottom
                )
            )
            .overlay(
                RoundedRectangle(cornerRadius: modalCornerRadius)
                    .strokeBorder(
                        LinearGradient(
                            colors: [
                                Color.red.opacity(Design.Opacity.medium),
                                Color.red.opacity(Design.Opacity.hint)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        lineWidth: Design.LineWidth.medium
                    )
            )
    }
}

// MARK: - Previews

#Preview("Game Over") {
    GameOverView(
        roundsPlayed: 42,
        onPlayAgain: {}
    )
}

#Preview("Few Rounds") {
    GameOverView(
        roundsPlayed: 3,
        onPlayAgain: {}
    )
}

