//
//  HiLoCountBadge.swift
//  Blackjack
//
//  Badge showing the Hi-Lo counting value of a card.
//

import SwiftUI
import CasinoKit

/// A small badge showing the Hi-Lo counting value of a card.
struct HiLoCountBadge: View {
    let card: Card
    
    @ScaledMetric(relativeTo: .caption2) private var fontSize: CGFloat = Design.Size.countBadgeFontSize
    @ScaledMetric(relativeTo: .caption2) private var paddingH: CGFloat = Design.Size.countBadgePaddingH
    @ScaledMetric(relativeTo: .caption2) private var paddingV: CGFloat = Design.Size.countBadgePaddingV
    @ScaledMetric(relativeTo: .caption2) private var offsetAmount: CGFloat = Design.Size.countBadgeOffset
    
    var body: some View {
        Text(card.hiLoDisplayText)
            .font(.system(size: fontSize, weight: .bold, design: .rounded))
            .foregroundStyle(badgeTextColor)
            .padding(.horizontal, paddingH)
            .padding(.vertical, paddingV)
            .background(
                Capsule()
                    .fill(badgeBackgroundColor)
            )
            .offset(x: -offsetAmount, y: offsetAmount)
    }
    
    private var badgeBackgroundColor: Color {
        switch card.hiLoValue {
        case 1: return .green    // Low cards = positive for player
        case -1: return .red     // High cards = negative for player
        default: return .gray    // Neutral
        }
    }
    
    private var badgeTextColor: Color {
        .white
    }
}

// MARK: - Card Accessibility Extension

extension Card {
    /// Accessibility description for VoiceOver.
    var accessibilityDescription: String {
        "\(rank.accessibilityName) of \(suit.accessibilityName)"
    }
}

// MARK: - Previews

#Preview("Low Card (+1)") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        CardView(card: Card(suit: .hearts, rank: .five), isFaceUp: true, cardWidth: 70)
            .overlay(alignment: .bottomLeading) {
                HiLoCountBadge(card: Card(suit: .hearts, rank: .five))
            }
    }
}

#Preview("High Card (-1)") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        CardView(card: Card(suit: .spades, rank: .king), isFaceUp: true, cardWidth: 70)
            .overlay(alignment: .bottomLeading) {
                HiLoCountBadge(card: Card(suit: .spades, rank: .king))
            }
    }
}

#Preview("Neutral Card (0)") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        CardView(card: Card(suit: .diamonds, rank: .seven), isFaceUp: true, cardWidth: 70)
            .overlay(alignment: .bottomLeading) {
                HiLoCountBadge(card: Card(suit: .diamonds, rank: .seven))
            }
    }
}

