//
//  BettingZone.swift
//  CasinoKit
//
//  A reusable betting zone for casino table layouts.
//

import SwiftUI

/// A tappable betting zone with label and chip display.
public struct BettingZone: View {
    /// The zone label (e.g., "PLAYER", "TIE", "INSURANCE").
    public let label: String
    
    /// Optional payout info (e.g., "1:1", "8:1").
    public let payoutInfo: String?
    
    /// Current bet amount (0 if no bet).
    public let betAmount: Int
    
    /// Whether the zone is enabled for betting.
    public let isEnabled: Bool
    
    /// Action when the zone is tapped.
    public let onTap: () -> Void
    
    /// Background color for the zone.
    public let backgroundColor: Color
    
    /// Text color.
    public let textColor: Color
    
    // Layout
    @ScaledMetric(relativeTo: .headline) private var labelFontSize: CGFloat = 16
    @ScaledMetric(relativeTo: .caption) private var payoutFontSize: CGFloat = 12
    
    /// Creates a betting zone.
    public init(
        label: String,
        payoutInfo: String? = nil,
        betAmount: Int = 0,
        isEnabled: Bool = true,
        backgroundColor: Color = .blue.opacity(0.2),
        textColor: Color = .white,
        onTap: @escaping () -> Void
    ) {
        self.label = label
        self.payoutInfo = payoutInfo
        self.betAmount = betAmount
        self.isEnabled = isEnabled
        self.backgroundColor = backgroundColor
        self.textColor = textColor
        self.onTap = onTap
    }
    
    public var body: some View {
        Button(action: onTap) {
            ZStack {
                // Background
                RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.medium)
                    .fill(backgroundColor)
                    .overlay(
                        RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.medium)
                            .strokeBorder(
                                textColor.opacity(CasinoDesign.Opacity.light),
                                lineWidth: CasinoDesign.LineWidth.thin
                            )
                    )
                
                // Content
                VStack(spacing: CasinoDesign.Spacing.xxSmall) {
                    Text(label)
                        .font(.system(size: labelFontSize, weight: .bold))
                        .foregroundStyle(textColor)
                    
                    if let payout = payoutInfo {
                        Text(payout)
                            .font(.system(size: payoutFontSize, weight: .medium))
                            .foregroundStyle(textColor.opacity(CasinoDesign.Opacity.medium))
                    }
                }
                
                // Chip badge for bet amount
                if betAmount > 0 {
                    ChipBadge(amount: betAmount)
                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing)
                        .padding(CasinoDesign.Spacing.xSmall)
                }
            }
        }
        .buttonStyle(.plain)
        .disabled(!isEnabled)
        .accessibilityLabel(label)
        .accessibilityValue(betAmount > 0 ? "$\(betAmount) bet" : "No bet")
        .accessibilityHint(isEnabled ? "Double tap to place bet" : "Betting disabled")
    }
}

/// A small chip badge showing bet amount.
public struct ChipBadge: View {
    public let amount: Int
    
    private let badgeSize: CGFloat = 28
    private let fontSize: CGFloat = 10
    
    public init(amount: Int) {
        self.amount = amount
    }
    
    public var body: some View {
        ZStack {
            Circle()
                .fill(Color.yellow)
                .frame(width: badgeSize, height: badgeSize)
            
            Circle()
                .strokeBorder(Color.orange, lineWidth: 2)
                .frame(width: badgeSize - 4, height: badgeSize - 4)
            
            Text(formattedAmount)
                .font(.system(size: fontSize, weight: .bold))
                .foregroundStyle(.black)
                .minimumScaleFactor(0.5)
        }
    }
    
    private var formattedAmount: String {
        if amount >= 1_000_000 {
            return "\(amount / 1_000_000)M"
        } else if amount >= 1_000 {
            return "\(amount / 1_000)K"
        }
        return "\(amount)"
    }
}

#Preview {
    ZStack {
        Color.CasinoTable.felt.ignoresSafeArea()
        
        HStack(spacing: 20) {
            BettingZone(
                label: "PLAYER",
                payoutInfo: "1:1",
                betAmount: 0,
                backgroundColor: .blue.opacity(0.2)
            ) { }
            .frame(width: 120, height: 80)
            
            BettingZone(
                label: "TIE",
                payoutInfo: "8:1",
                betAmount: 500,
                backgroundColor: .green.opacity(0.2)
            ) { }
            .frame(width: 80, height: 80)
            
            BettingZone(
                label: "BANKER",
                payoutInfo: "0.95:1",
                betAmount: 2500,
                backgroundColor: .red.opacity(0.2)
            ) { }
            .frame(width: 120, height: 80)
        }
    }
}

