//
//  HandLabelView.swift
//  Blackjack
//
//  Shared label component for hand displays (dealer and player).
//  Shows title text with an optional value badge.
//

import SwiftUI
import CasinoKit

/// Displays a hand label with title and value badge.
/// Used by both DealerHandView and PlayerHandView for consistent appearance.
struct HandLabelView: View {
    let title: String
    let value: Int?
    let valueText: String?
    let badgeColor: Color
    
    @ScaledMetric(relativeTo: .headline) private var labelFontSize: CGFloat = Design.Size.handLabelFontSize
    @ScaledMetric(relativeTo: .headline) private var badgeHeight: CGFloat = CasinoDesign.Size.valueBadge
    
    /// Creates a hand label with a numeric value badge.
    init(title: String, value: Int?, badgeColor: Color) {
        self.title = title
        self.value = value
        self.valueText = nil
        self.badgeColor = badgeColor
    }
    
    /// Creates a hand label with a text value badge (for soft hands like "8/18").
    init(title: String, valueText: String?, badgeColor: Color) {
        self.title = title
        self.value = nil
        self.valueText = valueText
        self.badgeColor = badgeColor
    }
    
    var body: some View {
        HStack(spacing: Design.Spacing.small) {
            Text(title)
                .font(.system(size: labelFontSize, weight: .bold, design: .rounded))
                .foregroundStyle(.white)
            
            if let value = value {
                ValueBadge(value: value, color: badgeColor)
            } else if let text = valueText, !text.isEmpty {
                TextValueBadge(text: text, color: badgeColor)
            }
        }
        .fixedSize() // Prevent the label from being constrained/truncated
        .frame(minHeight: badgeHeight)
        .animation(nil, value: value)
        .animation(nil, value: valueText)
    }
}

/// A badge that displays text (for soft hand values like "8/18").
struct TextValueBadge: View {
    let text: String
    let color: Color
    
    // Match ValueBadge styling from CasinoKit
    @ScaledMetric(relativeTo: .headline) private var fontSize: CGFloat = 15
    @ScaledMetric(relativeTo: .headline) private var badgeHeight: CGFloat = CasinoDesign.Size.valueBadge
    @ScaledMetric(relativeTo: .headline) private var badgePadding: CGFloat = 8
    
    var body: some View {
        Text(text)
            .font(.system(size: fontSize, weight: .bold, design: .rounded))
            .foregroundStyle(.white)
            .lineLimit(1)
            .fixedSize(horizontal: true, vertical: false) // Prevent truncation
            .padding(.horizontal, badgePadding)
            .frame(minHeight: badgeHeight)
            .background(
                Capsule()
                    .fill(color)
            )
    }
}

// MARK: - Previews

#Preview("Dealer Label") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        VStack(spacing: Design.Spacing.large) {
            HandLabelView(title: "DEALER", value: 21, badgeColor: Color.Hand.dealer)
            HandLabelView(title: "DEALER", value: 17, badgeColor: Color.Hand.dealer)
            HandLabelView(title: "DEALER", value: nil, badgeColor: Color.Hand.dealer)
        }
    }
}

#Preview("Player Label") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        VStack(spacing: Design.Spacing.large) {
            HandLabelView(title: "PLAYER", value: 21, badgeColor: Color.Hand.player)
            HandLabelView(title: "PLAYER", valueText: "8/18", badgeColor: Color.Hand.player)
            HandLabelView(title: "Hand 1", value: 17, badgeColor: Color.Hand.player)
            HandLabelView(title: "Hand 2", valueText: "5/15", badgeColor: Color.Hand.active)
        }
    }
}

