//
//  TableBackgroundView.swift
//  CasinoKit
//
//  A reusable casino table background with felt pattern.
//

import SwiftUI

/// A casino table felt background with radial gradient.
/// This creates a consistent look across all casino games.
public struct TableBackgroundView: View {
    /// Whether to show the decorative felt pattern.
    public let showPattern: Bool
    
    /// Creates a table background with the standard casino felt colors.
    /// - Parameter showPattern: Whether to show the decorative pattern (default: true).
    public init(showPattern: Bool = true) {
        self.showPattern = showPattern
    }
    
    /// Legacy initializer for backwards compatibility (colors are ignored).
    @available(*, deprecated, message: "Use init(showPattern:) - colors are now standardized")
    public init(
        feltColor: Color,
        edgeColor: Color,
        showPattern: Bool = true
    ) {
        self.showPattern = showPattern
    }
    
    public var body: some View {
        ZStack {
            // Base dark layer (deepest background)
            Color.CasinoTable.baseDark
                .ignoresSafeArea()
            
            // Radial gradient for depth (lighter center, darker edges)
            RadialGradient(
                colors: [
                    Color.CasinoTable.backgroundLight,
                    Color.CasinoTable.backgroundDark
                ],
                center: .center,
                startRadius: 50,
                endRadius: 600
            )
            .ignoresSafeArea()
            
            // Optional subtle felt texture pattern
            if showPattern {
                FeltPatternView()
                    .opacity(CasinoDesign.Opacity.verySubtle)
                    .ignoresSafeArea()
            }
        }
        .accessibilityHidden(true)
    }
}

/// A subtle decorative pattern for the felt (random dot texture).
public struct FeltPatternView: View {
    public init() {}
    
    public var body: some View {
        Canvas { context, size in
            // Use deterministic pseudo-random for consistent appearance
            var rng = SeededRandomNumberGenerator(seed: 12345)
            let dotCount = 2000
            
            for _ in 0..<dotCount {
                let x = Double.random(in: 0...size.width, using: &rng)
                let y = Double.random(in: 0...size.height, using: &rng)
                let dotSize = Double.random(in: 1...2, using: &rng)
                
                let rect = CGRect(x: x, y: y, width: dotSize, height: dotSize)
                context.fill(Path(ellipseIn: rect), with: .color(.white))
            }
        }
    }
}

/// Simple seeded random number generator for consistent felt pattern.
private struct SeededRandomNumberGenerator: RandomNumberGenerator {
    private var state: UInt64
    
    init(seed: UInt64) {
        state = seed
    }
    
    mutating func next() -> UInt64 {
        state = state &* 6364136223846793005 &+ 1442695040888963407
        return state
    }
}

#Preview {
    TableBackgroundView()
}

