//
//  CompactHandView.swift
//  Baccarat
//
//  A compact view showing cards in a horizontal row with overlap.
//

import SwiftUI
import CasinoKit

/// A compact hand view showing cards in a row with overlap.
struct CompactHandView: View {
    let cards: [Card]
    let cardsFaceUp: [Bool]
    let isWinner: Bool
    /// Container width passed from parent for sizing
    let containerWidth: CGFloat
    
    // MARK: - Environment
    
    @Environment(\.horizontalSizeClass) private var horizontalSizeClass
    
    // MARK: - Constants
    
    /// Overlap ratio relative to card width (negative = overlap)
    private let overlapRatio: CGFloat = -0.45
    
    /// Maximum number of cards in baccarat hand
    private let maxCards: Int = 3
    
    /// Placeholder spacing when no cards
    private let placeholderSpacing: CGFloat = Design.Spacing.small
    
    // MARK: - Computed Properties
    
    /// Whether we're on a large screen (iPad)
    private var isLargeScreen: Bool {
        horizontalSizeClass == .regular
    }
    
    /// WIN badge font size - larger on iPad
    private var winBadgeFontSize: CGFloat {
        isLargeScreen ? 14 : 10
    }
    
    /// Card width calculated from container width
    /// Formula: containerWidth = cardWidth + (cardWidth + overlap) * 2
    /// Where overlap = cardWidth * overlapRatio
    private var cardWidth: CGFloat {
        let divisor = 1 + CGFloat(maxCards - 1) * (1 + overlapRatio)
        return containerWidth / divisor
    }
    
    /// Card overlap based on card width
    private var cardOverlap: CGFloat {
        cardWidth * overlapRatio
    }
    
    /// Card height based on aspect ratio
    private var cardHeight: CGFloat {
        cardWidth * CasinoDesign.Size.cardAspectRatio
    }
    
    // MARK: - Body
    
    var body: some View {
        cardsContent
            .frame(width: containerWidth, height: cardHeight)
            .background(winnerBorder)
            .overlay(alignment: .bottom) {
                winBadge
            }
    }
    
    // MARK: - Private Views
    
    private var cardsContent: some View {
        HStack(spacing: cards.isEmpty ? placeholderSpacing : cardOverlap) {
            if cards.isEmpty {
                // Placeholders - no overlap, just side by side
                ForEach(0..<2, id: \.self) { _ in
                    CardPlaceholderView(width: cardWidth)
                }
            } else {
                ForEach(cards.indices, id: \.self) { index in
                    let isFaceUp = index < cardsFaceUp.count ? cardsFaceUp[index] : false
                    CardView(
                        card: cards[index],
                        isFaceUp: isFaceUp,
                        cardWidth: cardWidth
                    )
                    .zIndex(Double(index))
                }
            }
        }
        .animation(nil, value: cards.count) // Prevent size animation during dealing
    }
    
    private var winnerBorder: some View {
        RoundedRectangle(cornerRadius: Design.CornerRadius.small)
            .strokeBorder(
                isWinner ? Color.yellow : Color.clear,
                lineWidth: Design.LineWidth.standard
            )
    }
    
    @ViewBuilder
    private var winBadge: some View {
        if isWinner {
            Text("WIN")
                .font(.system(size: winBadgeFontSize, weight: .black))
                .foregroundStyle(.black)
                .padding(.horizontal, Design.Spacing.small)
                .padding(.vertical, Design.Spacing.xxSmall)
                .background(
                    Capsule()
                        .fill(Color.yellow)
                )
                .offset(y: Design.Spacing.small)
        }
    }
}

// MARK: - Previews

#Preview("Empty Hand") {
    ZStack {
        TableBackgroundView()
        CompactHandView(
            cards: [],
            cardsFaceUp: [],
            isWinner: false,
            containerWidth: 160
        )
    }
}

#Preview("Two Cards") {
    ZStack {
        TableBackgroundView()
        CompactHandView(
            cards: [
                Card(suit: .spades, rank: .king),
                Card(suit: .hearts, rank: .eight)
            ],
            cardsFaceUp: [true, true],
            isWinner: false,
            containerWidth: 160
        )
    }
}

#Preview("Three Cards - Winner") {
    ZStack {
        TableBackgroundView()
        CompactHandView(
            cards: [
                Card(suit: .spades, rank: .four),
                Card(suit: .hearts, rank: .three),
                Card(suit: .clubs, rank: .two)
            ],
            cardsFaceUp: [true, true, true],
            isWinner: true,
            containerWidth: 160
        )
    }
}

#Preview("Cards Face Down") {
    ZStack {
        TableBackgroundView()
        CompactHandView(
            cards: [
                Card(suit: .diamonds, rank: .ace),
                Card(suit: .spades, rank: .seven)
            ],
            cardsFaceUp: [false, false],
            isWinner: false,
            containerWidth: 160
        )
    }
}

