//
//  ActionButtonsView.swift
//  Baccarat
//
//  Action buttons for deal, clear, and new round.
//

import SwiftUI
import CasinoKit

/// Action buttons for deal, clear, and new round.
struct ActionButtonsView: View {
    @Bindable var gameState: GameState
    let onDeal: () -> Void
    let onClear: () -> Void
    let onNewRound: () -> Void
    
    // MARK: - Environment
    
    @Environment(\.dynamicTypeSize) private var dynamicTypeSize
    
    /// Whether the current text size is an accessibility size (very large)
    private var isAccessibilitySize: Bool {
        dynamicTypeSize.isAccessibilitySize
    }
    
    // MARK: - Layout Constants
    
    private let buttonFontSize: CGFloat = Design.BaseFontSize.xLarge
    private let iconSize: CGFloat = Design.BaseFontSize.xxLarge + Design.Spacing.xSmall
    private let containerHeight: CGFloat = Design.Size.bettingButtonsContainerHeight
    
    // MARK: - Body
    
    var body: some View {
        ZStack {
            // Fixed height container to prevent layout shifts
            Color.clear
                .frame(height: containerHeight)
            
            // Content changes with animation
            Group {
                if gameState.currentPhase == .betting {
                    bettingButtons
                } else if gameState.currentPhase == .roundComplete && !gameState.showResultBanner {
                    newRoundButton
                }
            }
            .animation(.easeInOut(duration: Design.Animation.quick), value: gameState.currentPhase)
            .animation(.easeInOut(duration: Design.Animation.quick), value: gameState.showResultBanner)
        }
    }
    
    // MARK: - Private Views
    
    private var bettingButtons: some View {
        BettingActionsView(
            hasBet: !gameState.currentBets.isEmpty,
            canDeal: gameState.canDeal,
            amountNeededForMinimum: gameState.isMainBetBelowMinimum ? gameState.amountNeededForMinimum : nil,
            onClear: onClear,
            onDeal: onDeal
        )
    }
    
    @ViewBuilder
    private var newRoundButton: some View {
        let buttonBackground = LinearGradient(
            colors: [Color.CasinoButton.goldLight, Color.CasinoButton.goldDark],
            startPoint: .top,
            endPoint: .bottom
        )
        
        if isAccessibilitySize {
            Button("New Round", systemImage: "arrow.clockwise", action: onNewRound)
                .labelStyle(.iconOnly)
                .font(.system(size: iconSize, weight: .bold))
                .foregroundStyle(.black)
                .padding(Design.Spacing.medium + Design.Spacing.xxSmall)
                .background(
                    Circle()
                        .fill(buttonBackground)
                )
                .shadow(color: .yellow.opacity(Design.Opacity.light), radius: Design.Shadow.radiusMedium)
        } else {
            Button("New Round", systemImage: "arrow.clockwise", action: onNewRound)
                .labelStyle(.titleAndIcon)
                .font(.system(size: buttonFontSize, weight: .bold))
                .foregroundStyle(.black)
                .padding(.horizontal, Design.Spacing.xxLarge)
                .padding(.vertical, Design.Spacing.medium + Design.Spacing.xxSmall)
                .background(
                    Capsule()
                        .fill(buttonBackground)
                )
                .shadow(color: .yellow.opacity(Design.Opacity.light), radius: Design.Shadow.radiusMedium)
        }
    }
}

// MARK: - Previews

#Preview("Betting Phase") {
    ZStack {
        TableBackgroundView()
        VStack {
            Spacer()
            ActionButtonsView(
                gameState: GameState(settings: GameSettings()),
                onDeal: {},
                onClear: {},
                onNewRound: {}
            )
            .padding()
        }
    }
}

