//
//  TopBarView.swift
//  CasinoKit
//
//  A reusable top bar for casino games showing balance and toolbar buttons.
//

import SwiftUI

/// A button configuration for the top bar toolbar.
public struct TopBarButton: Identifiable {
    public let id = UUID()
    
    /// The SF Symbol icon name.
    public let icon: String
    
    /// The accessibility label for VoiceOver.
    public let accessibilityLabel: String
    
    /// The action to perform when tapped.
    public let action: () -> Void
    
    /// Creates a toolbar button configuration.
    /// - Parameters:
    ///   - icon: SF Symbol name for the button icon.
    ///   - accessibilityLabel: VoiceOver label for the button.
    ///   - action: Closure to execute when tapped.
    public init(icon: String, accessibilityLabel: String, action: @escaping () -> Void) {
        self.icon = icon
        self.accessibilityLabel = accessibilityLabel
        self.action = action
    }
}

/// A top bar showing balance and customizable toolbar buttons.
public struct TopBarView: View {
    /// The current balance to display.
    public let balance: Int
    
    /// Optional secondary info (e.g., cards remaining).
    public let secondaryInfo: String?
    
    /// Icon for secondary info.
    public let secondaryIcon: String?
    
    /// Custom buttons to display at the front of the toolbar (before stats/help/settings).
    public let leadingButtons: [TopBarButton]
    
    /// Action when settings is tapped.
    public let onSettings: (() -> Void)?
    
    /// Action when help/rules is tapped.
    public let onHelp: (() -> Void)?
    
    /// Action when stats is tapped.
    public let onStats: (() -> Void)?
    
    // MARK: - Font Sizes (fixed for top bar constraints)
    
    private let balanceFontSize: CGFloat = 24
    private let dollarFontSize: CGFloat = 14
    private let secondaryFontSize: CGFloat = 14
    private let iconSize: CGFloat = 20
    
    /// Creates a top bar.
    /// - Parameters:
    ///   - balance: The current balance.
    ///   - secondaryInfo: Optional secondary info text.
    ///   - secondaryIcon: Optional SF Symbol for secondary info.
    ///   - leadingButtons: Custom buttons to add at the front of the toolbar.
    ///   - onSettings: Settings button action.
    ///   - onHelp: Help button action.
    ///   - onStats: Stats button action.
    public init(
        balance: Int,
        secondaryInfo: String? = nil,
        secondaryIcon: String? = nil,
        leadingButtons: [TopBarButton] = [],
        onSettings: (() -> Void)? = nil,
        onHelp: (() -> Void)? = nil,
        onStats: (() -> Void)? = nil
    ) {
        self.balance = balance
        self.secondaryInfo = secondaryInfo
        self.secondaryIcon = secondaryIcon
        self.leadingButtons = leadingButtons
        self.onSettings = onSettings
        self.onHelp = onHelp
        self.onStats = onStats
    }
    
    public var body: some View {
        HStack {
            // Balance display
            HStack(spacing: CasinoDesign.Spacing.xxSmall) {
                Text("$")
                    .font(.system(size: dollarFontSize, weight: .bold))
                    .foregroundStyle(Color.CasinoTopBar.balanceText)
                
                Text(balance.formatted())
                    .font(.system(size: balanceFontSize, weight: .bold, design: .rounded))
                    .foregroundStyle(Color.CasinoTopBar.balanceText)
                    .contentTransition(.numericText())
            }
            .accessibilityElement(children: .ignore)
            .accessibilityLabel(String(localized: "Balance", bundle: .module))
            .accessibilityValue("$\(balance.formatted())")
            
            Spacer()
            
            // Secondary info (centered)
            if let info = secondaryInfo {
                HStack(spacing: CasinoDesign.Spacing.xSmall) {
                    if let icon = secondaryIcon {
                        Image(systemName: icon)
                    }
                    Text(info)
                }
                .font(.system(size: secondaryFontSize))
                .foregroundStyle(Color.CasinoTopBar.secondaryText)
            }
            
            Spacer()
            
            // Toolbar buttons
            HStack(spacing: CasinoDesign.Spacing.medium) {
                // Custom leading buttons (game-specific)
                ForEach(leadingButtons) { button in
                    ToolbarButton(icon: button.icon, action: button.action)
                        .accessibilityLabel(button.accessibilityLabel)
                }
                
                if let onStats = onStats {
                    ToolbarButton(icon: "chart.bar.fill", action: onStats)
                        .accessibilityLabel(String(localized: "Statistics", bundle: .module))
                }
                
                if let onHelp = onHelp {
                    ToolbarButton(icon: "info.circle", action: onHelp)
                        .accessibilityLabel(String(localized: "Rules", bundle: .module))
                }
                
                if let onSettings = onSettings {
                    ToolbarButton(icon: "gearshape.fill", action: onSettings)
                        .accessibilityLabel(String(localized: "Settings", bundle: .module))
                }
            }
        }
        .padding(.horizontal, CasinoDesign.Spacing.large)
        .padding(.vertical, CasinoDesign.Spacing.small)
    }
}

/// A single toolbar button.
private struct ToolbarButton: View {
    let icon: String
    let action: () -> Void
    
    private let iconSize: CGFloat = 20
    
    var body: some View {
        Button(action: action) {
            Image(systemName: icon)
                .font(.system(size: iconSize))
                .foregroundStyle(Color.CasinoTopBar.iconButton)
        }
    }
}

#Preview {
    ZStack {
        Color.CasinoTable.felt.ignoresSafeArea()
        
        VStack {
            TopBarView(
                balance: 10_500,
                secondaryInfo: "411",
                secondaryIcon: "rectangle.portrait.on.rectangle.portrait.fill",
                leadingButtons: [
                    TopBarButton(icon: "arrow.counterclockwise", accessibilityLabel: "Reset") {}
                ],
                onSettings: {},
                onHelp: {},
                onStats: {}
            )
            
            Spacer()
        }
    }
}

