//
//  HintViews.swift
//  Baccarat
//
//  Baccarat-specific hint views and supplementary components.
//  The main BettingHintView is provided by CasinoKit.
//

import SwiftUI
import CasinoKit

// MARK: - Odds Info View

/// Shows the house edge information for educational purposes.
struct OddsInfoView: View {
    @ScaledMetric(relativeTo: .caption) private var fontSize: CGFloat = Design.BaseFontSize.xSmall
    
    var body: some View {
        HStack(spacing: Design.Spacing.medium) {
            oddsItem(label: "Banker", edge: "1.06%", color: .red)
            oddsItem(label: "Player", edge: "1.24%", color: .blue)
            oddsItem(label: "Tie", edge: "14.4%", color: .green)
        }
        .padding(.horizontal, Design.Spacing.medium)
        .padding(.vertical, Design.Spacing.xSmall)
        .background(
            RoundedRectangle(cornerRadius: Design.CornerRadius.small)
                .fill(Color.black.opacity(Design.Opacity.light))
        )
    }
    
    private func oddsItem(label: String, edge: String, color: Color) -> some View {
        VStack(spacing: Design.Spacing.xxSmall) {
            Text(label)
                .font(.system(size: fontSize, weight: .bold))
                .foregroundStyle(color)
            Text(edge)
                .font(.system(size: fontSize, weight: .medium, design: .monospaced))
                .foregroundStyle(.white.opacity(Design.Opacity.strong))
        }
    }
}

// MARK: - Distribution Bar

/// A visual bar showing the distribution of Player/Banker/Tie results.
struct DistributionBarView: View {
    let player: Int
    let banker: Int
    let tie: Int
    
    @ScaledMetric(relativeTo: .caption) private var fontSize: CGFloat = Design.BaseFontSize.xSmall
    
    private var total: Int { player + banker + tie }
    
    private var playerPct: Double {
        total > 0 ? Double(player) / Double(total) : 0.33
    }
    
    private var bankerPct: Double {
        total > 0 ? Double(banker) / Double(total) : 0.33
    }
    
    private var tiePct: Double {
        total > 0 ? Double(tie) / Double(total) : 0.34
    }
    
    var body: some View {
        VStack(spacing: Design.Spacing.xSmall) {
            // Bar
            GeometryReader { geo in
                HStack(spacing: 0) {
                    Rectangle()
                        .fill(Color.blue)
                        .frame(width: geo.size.width * playerPct)
                    
                    Rectangle()
                        .fill(Color.green)
                        .frame(width: geo.size.width * tiePct)
                    
                    Rectangle()
                        .fill(Color.red)
                        .frame(width: geo.size.width * bankerPct)
                }
                .clipShape(Capsule())
            }
            .frame(height: Design.Spacing.small)
            
            // Labels
            HStack {
                Text("P: \(Int(playerPct * 100))%")
                    .foregroundStyle(.blue)
                
                Spacer()
                
                if tie > 0 {
                    Text("T: \(Int(tiePct * 100))%")
                        .foregroundStyle(.green)
                    
                    Spacer()
                }
                
                Text("B: \(Int(bankerPct * 100))%")
                    .foregroundStyle(.red)
            }
            .font(.system(size: fontSize, weight: .medium, design: .rounded))
        }
        .padding(.horizontal, Design.Spacing.medium)
        .padding(.vertical, Design.Spacing.small)
        .background(
            RoundedRectangle(cornerRadius: Design.CornerRadius.small)
                .fill(Color.black.opacity(Design.Opacity.light))
        )
        .accessibilityElement(children: .ignore)
        .accessibilityLabel(String(localized: "Result distribution"))
        .accessibilityValue("Player \(Int(playerPct * 100))%, Banker \(Int(bankerPct * 100))%, Tie \(Int(tiePct * 100))%")
    }
}

// MARK: - Streak Badge

/// A badge showing the current streak.
struct StreakBadgeView: View {
    let type: GameResult
    let count: Int
    
    @ScaledMetric(relativeTo: .caption) private var fontSize: CGFloat = Design.BaseFontSize.body
    
    private var color: Color {
        switch type {
        case .bankerWins: return .red
        case .playerWins: return .blue
        case .tie: return .green
        }
    }
    
    private var letter: String {
        switch type {
        case .bankerWins: return "B"
        case .playerWins: return "P"
        case .tie: return "T"
        }
    }
    
    var body: some View {
        HStack(spacing: Design.Spacing.xxSmall) {
            Image(systemName: "flame.fill")
                .font(.system(size: fontSize))
                .foregroundStyle(.orange)
            
            Text("\(letter)×\(count)")
                .font(.system(size: fontSize, weight: .bold, design: .rounded))
                .foregroundStyle(color)
        }
        .padding(.horizontal, Design.Spacing.small)
        .padding(.vertical, Design.Spacing.xxSmall)
        .background(
            Capsule()
                .fill(Color.black.opacity(Design.Opacity.heavy))
                .overlay(
                    Capsule()
                        .strokeBorder(color.opacity(Design.Opacity.medium), lineWidth: Design.LineWidth.thin)
                )
        )
        .accessibilityLabel(String(localized: "Streak"))
        .accessibilityValue("\(type == .bankerWins ? "Banker" : "Player") streak of \(count)")
    }
}

// MARK: - Previews

#Preview("Distribution Bar") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        DistributionBarView(player: 12, banker: 15, tie: 3)
            .frame(maxWidth: 300)
    }
}

#Preview("Streak Badge") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        HStack(spacing: Design.Spacing.medium) {
            StreakBadgeView(type: .bankerWins, count: 4)
            StreakBadgeView(type: .playerWins, count: 3)
        }
    }
}

#Preview("Odds Info") {
    ZStack {
        Color.Table.felt.ignoresSafeArea()
        OddsInfoView()
    }
}
