//
//  DigitView.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 9/9/25.
//

import SwiftUI

/// Component for displaying a single digit with fixed width and glow effects
struct DigitView: View {
    let digit: String
    let fontSize: CGFloat
    let opacity: Double
    let digitColor: Color
    let glowIntensity: Double
    let fontFamily: String
    let fontWeight: String
    let fontDesign: String
    let digitWidth: CGFloat
    let digitHeight: CGFloat
    
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                glowText
                    .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
                mainText
                    .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
    
    // MARK: - Computed Properties
    private var customFont: Font {
        FontUtils.customFont(
            size: fontSize,
            family: fontFamily,
            weight: fontWeight,
            design: fontDesign
        )
    }
    
    private var glowRadius: CGFloat {
        ColorUtils.glowRadius(intensity: glowIntensity)
    }
    
    private var glowOpacity: Double {
        ColorUtils.glowOpacity(intensity: glowIntensity) * opacity
    }
    
    private var glowText: some View {
        Text(digit)
            .font(customFont)
            .foregroundColor(digitColor)
            .blur(radius: glowRadius)
            .opacity(glowOpacity)
    }
    
    private var mainText: some View {
        Text(digit)
            .font(customFont)
            .foregroundColor(digitColor)
            .opacity(opacity)
    }
}

// MARK: - Preview
#Preview {
    let digitView = DigitView(
        digit: "8",
        fontSize: 80,
        opacity: 1.0,
        digitColor: .white,
        glowIntensity: 0.2,
        fontFamily: "System",
        fontWeight: "Regular",
        fontDesign: "Default",
        digitWidth: 60,
        digitHeight: 100
    )
    
    return digitView
        .background(Color.black)
        .frame(width: 100, height: 120)
}
