//
//  TimeDisplayView.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 9/7/25.
//

import SwiftUI

/// Component for displaying segmented time with customizable formatting
struct TimeDisplayView: View {
    
    // MARK: - Properties
    let date: Date
    let use24Hour: Bool
    let showSeconds: Bool
    let digitColor: Color
    let glowIntensity: Double
    let manualScale: Double
    let stretched: Bool
    let clockOpacity: Double
    let fontFamily: String
    let fontWeight: String
    let fontDesign: String
    
    // MARK: - Formatters
    private static let hour24DF: DateFormatter = {
        let df = DateFormatter()
        df.locale = Locale(identifier: "en_US_POSIX")
        df.dateFormat = "HH"
        return df
    }()
    
    private static let hour12DF: DateFormatter = {
        let df = DateFormatter()
        df.locale = Locale(identifier: "en_US_POSIX")
        df.dateFormat = "h"
        return df
    }()
    
    private static let minuteDF: DateFormatter = {
        let df = DateFormatter()
        df.locale = Locale(identifier: "en_US_POSIX")
        df.dateFormat = "mm"
        return df
    }()
    
    private static let secondDF: DateFormatter = {
        let df = DateFormatter()
        df.locale = Locale(identifier: "en_US_POSIX")
        df.dateFormat = "ss"
        return df
    }()
    
    
    // MARK: - Body
    var body: some View {
        GeometryReader { proxy in
            let containerSize = proxy.size
            let portrait = containerSize.height >= containerSize.width

            // Use optimal font sizing based on actual container size
            let baseFontSize = stretched ? 
            FontUtils.maximumStretchedFontSize(
                containerWidth: containerSize.width,
                containerHeight: containerSize.height,
                isPortrait: portrait,
                showSeconds: showSeconds
            ) :
            FontUtils.optimalFontSize(
                containerWidth: containerSize.width,
                containerHeight: containerSize.height,
                isPortrait: portrait,
                showSeconds: showSeconds
            )
            
            // DEBUG: Print font size and container size
            let _ = print("DEBUG: Container: \(containerSize), Font: \(baseFontSize), Stretched: \(stretched)")
            
            // Time components
            let hour = use24Hour ? Self.hour24DF.string(from: date) : Self.hour12DF.string(from: date)
            let minute = Self.minuteDF.string(from: date)
            let secondsText = Self.secondDF.string(from: date)
            
            
            // Separators - reasonable spacing with extra padding in landscape
            let dotDiameter = baseFontSize * 0.20
            let hSpacing = portrait ? baseFontSize * 0.18 : baseFontSize * 0.25  // More spacing in landscape
            let vSpacing = portrait ? baseFontSize * 0.22 : baseFontSize * 0.30  // More spacing in landscape
            
            // Simple scaling - let the content size naturally and apply manual scale
            let finalScale = stretched ? 1.0 : CGFloat(max(0.1, min(manualScale, 1.0)))
            
            // Time display with consistent centering and stable layout
            Group {
                if portrait {
                    VStack(alignment: .center, spacing: 0) {
                        TimeSegment(text: hour, fontSize: baseFontSize, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontFamily: fontFamily, fontWeight: fontWeight, fontDesign: fontDesign)
                        HorizontalColon(dotDiameter: dotDiameter, spacing: hSpacing, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontWeight: fontWeight)
                        TimeSegment(text: minute, fontSize: baseFontSize, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontFamily: fontFamily, fontWeight: fontWeight, fontDesign: fontDesign)
                        if showSeconds {
                            HorizontalColon(dotDiameter: dotDiameter, spacing: hSpacing, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontWeight: fontWeight)
                            TimeSegment(text: secondsText, fontSize: baseFontSize, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontFamily: fontFamily, fontWeight: fontWeight, fontDesign: fontDesign)
                        }
                    }
                } else {
                    HStack(alignment: .center, spacing: baseFontSize * 0.035) {
                        TimeSegment(text: hour, fontSize: baseFontSize, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontFamily: fontFamily, fontWeight: fontWeight, fontDesign: fontDesign)
                        VerticalColon(dotDiameter: dotDiameter, spacing: vSpacing, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontWeight: fontWeight)
                        TimeSegment(text: minute, fontSize: baseFontSize, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontFamily: fontFamily, fontWeight: fontWeight, fontDesign: fontDesign)
                        if showSeconds {
                            VerticalColon(dotDiameter: dotDiameter, spacing: vSpacing, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontWeight: fontWeight)
                            TimeSegment(text: secondsText, fontSize: baseFontSize, opacity: clockOpacity, digitColor: digitColor, glowIntensity: glowIntensity, fontFamily: fontFamily, fontWeight: fontWeight, fontDesign: fontDesign)
                        }
                    }
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
            .scaleEffect(finalScale, anchor: .center)
            .animation(UIConstants.AnimationCurves.smooth, value: finalScale)
            .animation(UIConstants.AnimationCurves.smooth, value: showSeconds) // Smooth animation for seconds toggle
            .minimumScaleFactor(0.1)
            .clipped() // Prevent overflow beyond bounds
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .onOrientationChange() // Force updates on orientation changes
    }
    
    
}


// MARK: - Preview
#Preview {
    let style = ClockStyle()
    return TimeDisplayView(
        date: Date(),
        use24Hour: style.use24Hour,
        showSeconds: style.showSeconds,
        digitColor: style.digitColor,
        glowIntensity: style.glowIntensity,
        manualScale: style.digitScale,
        stretched: style.stretched,
        clockOpacity: style.clockOpacity,
        fontFamily: style.fontFamily,
        fontWeight: style.fontWeight,
        fontDesign: style.fontDesign
    )    .background(Color.black)
}
