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

import SwiftUI
import Foundation

/// Component for displaying a time segment (hours, minutes, seconds) with fixed-width digits
struct TimeSegment: View {
    let text: String
    @Binding var fontSize: CGFloat
    let opacity: Double
    let digitColor: Color
    let glowIntensity: Double
    let fontFamily: FontFamily
    let fontWeight: Font.Weight
    let fontDesign: Font.Design
    let isDisplayMode: Bool
        
    var body: some View {
        HStack(alignment: .center, spacing: 0) {
            ForEach(Array(text.enumerated()), id: \.offset) { index, character in
                DigitView(
                    digit: String(character),
                    fontName: fontFamily,
                    weight: fontWeight,
                    design: fontDesign,
                    digitColor: digitColor,
                    opacity: clampedOpacity,
                    glowIntensity: glowIntensity,
                    fontSize: $fontSize,
                    isDisplayMode: isDisplayMode
                )
                //.border(.red, width: 1)
            }
        }
        //.border(Color.green, width: 1)
        .frame(maxHeight: .infinity)
    }
    
    // MARK: - Computed Properties
    private var clampedOpacity: Double {
        ColorUtils.clampOpacity(opacity)
    }
}

// MARK: - Preview
#Preview {
    @Previewable @State var fontSize: CGFloat = 100
    TimeSegment(
        text: "12",
        fontSize: $fontSize,
        opacity: 1.0,
        digitColor: .white,
        glowIntensity: 0.2,
        fontFamily: .system,
        fontWeight: .regular,
        fontDesign: .default,
        isDisplayMode: true
    )
    .background(Color.black)
}
