//
//  PlayerHandsContainer.swift
//  Blackjack
//
//  Scrollable container for player hands (supports split hands).
//

import SwiftUI
import CasinoKit

/// Horizontally scrollable container that displays one or more player hands.
/// Handles split hands by showing them side-by-side with auto-scrolling to the active hand.
struct PlayerHandsContainer: View {
    let hands: [BlackjackHand]
    let activeHandIndex: Int
    let isPlayerTurn: Bool
    let showCardCount: Bool
    let showAnimations: Bool
    let dealingSpeed: Double
    let cardWidth: CGFloat
    let cardSpacing: CGFloat
    
    /// Number of visible cards for each hand (completed animations)
    let visibleCardCounts: [Int]
    
    /// Current hint to display (shown on active hand only).
    let currentHint: String?
    
    /// Whether the hint toast should be visible.
    let showHintToast: Bool
    
    /// Tracks the currently scrolled-to hand ID for smooth positioning
    @State private var scrolledHandID: UUID?
    
    /// The active hand's ID, used for scroll position binding
    private var activeHandID: UUID? {
        guard activeHandIndex < hands.count else { return nil }
        return hands[activeHandIndex].id
    }
    
    /// Total card count across all hands - used for animation
    private var totalCardCount: Int {
        hands.reduce(0) { $0 + $1.cards.count }
    }
    
    /// The hands content HStack
    private var handsContent: some View {
        HStack(spacing: Design.Spacing.large) {
            // Display hands in reverse order (right to left play order)
            // Visual order: Hand 3, Hand 2, Hand 1 (left to right)
            // Play order: Hand 1 played first (rightmost), then Hand 2, etc.
            ForEach(Array(hands.enumerated()).reversed(), id: \.element.id) { index, hand in
                let isActiveHand = index == activeHandIndex && isPlayerTurn
                let visibleCount = index < visibleCardCounts.count ? visibleCardCounts[index] : 0
                PlayerHandView(
                    hand: hand,
                    isActive: isActiveHand,
                    showCardCount: showCardCount,
                    showAnimations: showAnimations,
                    dealingSpeed: dealingSpeed,
                    // Hand numbers: rightmost (index 0) is Hand 1, played first
                    handNumber: hands.count > 1 ? index + 1 : nil,
                    cardWidth: cardWidth,
                    cardSpacing: cardSpacing,
                    visibleCardCount: visibleCount,
                    // Only show hint on the active hand
                    currentHint: isActiveHand ? currentHint : nil,
                    showHintToast: isActiveHand && showHintToast
                )
                .id(hand.id)
                .transition(.scale.combined(with: .opacity))
            }
        }
        .animation(.spring(duration: Design.Animation.springDuration), value: hands.count)
        // Animate card count changes so width growth is smooth
        .animation(.spring(duration: Design.Animation.springDuration), value: totalCardCount)
    }
    
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            handsContent
                .padding(.horizontal, Design.Spacing.xxLarge)
                // Only use containerRelativeFrame for single hand (centering)
                // For multiple hands, allow natural scrolling
                .modifier(CenterSingleHandModifier(isSingleHand: hands.count == 1))
                .scrollTargetLayout()
        }
        .scrollClipDisabled()
        .scrollBounceBehavior(.always)
        .scrollPosition(id: $scrolledHandID, anchor: .center)
        .scrollTargetBehavior(.viewAligned)
        .onChange(of: activeHandID) { _, newID in
            // Animate scroll when switching between split hands
            if let newID {
                withAnimation(.easeInOut(duration: Design.Animation.quick)) {
                    scrolledHandID = newID
                }
            }
        }
        .onChange(of: hands.count) { _, _ in
            // Re-center when hands are added (split)
            if let activeID = activeHandID {
                withAnimation(.easeInOut(duration: Design.Animation.quick)) {
                    scrolledHandID = activeID
                }
            }
        }
        .onAppear {
            // Set initial position without animation
            scrolledHandID = activeHandID
        }
        .frame(maxWidth: .infinity)
    }
}

// MARK: - Centering Modifier

/// Conditionally applies containerRelativeFrame for centering single hands.
/// For multiple hands, allows natural content width for scrolling.
private struct CenterSingleHandModifier: ViewModifier {
    let isSingleHand: Bool
    
    func body(content: Content) -> some View {
        if isSingleHand {
            content
                .containerRelativeFrame(.horizontal, alignment: .center)
        } else {
            content
        }
    }
}

// MARK: - Previews

#Preview("Single Hand") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        PlayerHandsContainer(
            hands: [BlackjackHand(cards: [
                Card(suit: .hearts, rank: .ace),
                Card(suit: .spades, rank: .king)
            ], bet: 100)],
            activeHandIndex: 0,
            isPlayerTurn: true,
            showCardCount: false,
            showAnimations: true,
            dealingSpeed: 1.0,
            cardWidth: 60,
            cardSpacing: -20,
            visibleCardCounts: [2],
            currentHint: "Stand",
            showHintToast: true
        )
    }
}

#Preview("Split Hands") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        PlayerHandsContainer(
            hands: [
                BlackjackHand(cards: [
                    Card(suit: .clubs, rank: .eight),
                    Card(suit: .spades, rank: .jack)
                ], bet: 100),
                BlackjackHand(cards: [
                    Card(suit: .hearts, rank: .eight),
                    Card(suit: .diamonds, rank: .five)
                ], bet: 100)
            ],
            activeHandIndex: 1,
            isPlayerTurn: true,
            showCardCount: true,
            showAnimations: true,
            dealingSpeed: 1.0,
            cardWidth: 60,
            cardSpacing: -20,
            visibleCardCounts: [2, 2],
            currentHint: "Hit",
            showHintToast: true
        )
    }
}

