//
//  View+Extensions.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 9/7/25.
//

import SwiftUI
import Bedrock

extension View {
    
    /// Apply standard card styling
    /// - Returns: View with card styling applied
    func cardStyle() -> some View {
        self
            .background(AppSurface.overlay, in: RoundedRectangle(cornerRadius: Design.CornerRadius.appLarge))
            .overlay(
                RoundedRectangle(cornerRadius: Design.CornerRadius.appLarge)
                    .stroke(AppBorder.subtle, lineWidth: Design.LineWidth.thin)
            )
    }
    
    /// Apply standard button styling
    /// - Parameters:
    ///   - isEnabled: Whether the button is enabled
    ///   - color: Button color
    /// - Returns: View with button styling applied
    func buttonStyle(isEnabled: Bool = true, color: Color = AppAccent.primary) -> some View {
        self
            .padding(Design.Spacing.medium)
            .background(isEnabled ? color : color.opacity(Design.Opacity.light))
            .foregroundColor(.white)
            .cornerRadius(Design.CornerRadius.small)
            .disabled(!isEnabled)
    }
    
    
    /// Apply responsive font sizing that updates on orientation and layout changes
    /// - Parameters:
    ///   - baseSize: Base font size
    ///   - isPortrait: Whether in portrait orientation
    ///   - showSeconds: Whether seconds are displayed (for time components)
    ///   - showAmPm: Whether AM/PM is displayed
    /// - Returns: View with responsive font sizing
    func responsiveFontSize(
        baseSize: CGFloat,
        isPortrait: Bool,
        showSeconds: Bool = false,
        showAmPm: Bool = false
    ) -> some View {
        self.modifier(ResponsiveFontModifier(
            baseSize: baseSize,
            isPortrait: isPortrait,
            showSeconds: showSeconds,
            showAmPm: showAmPm
        ))
    }
    
    /// Force view to update on orientation changes
    /// - Returns: View that updates on orientation changes
    func onOrientationChange() -> some View {
        self.modifier(OrientationChangeModifier())
    }
    
    /// Apply standard section title styling
    /// - Returns: View with section title styling applied
    func sectionTitleStyle() -> some View {
        self
            .font(.title2.weight(.bold))
            .foregroundColor(AppTextColors.primary)
    }
    
    /// Center content horizontally with spacers
    /// - Returns: View wrapped in centered HStack
    func centered() -> some View {
        HStack {
            Spacer()
            self
            Spacer()
        }
    }
    
    /// Apply standard section header styling with title
    /// - Parameter title: The title text
    /// - Returns: View with section header styling
    func sectionHeader(title: String) -> some View {
        VStack(alignment: .leading, spacing: Design.Spacing.medium) {
            Text(title)
                .sectionTitleStyle()
            self
        }
    }
    
    /// Apply standard content padding
    /// - Parameters:
    ///   - horizontal: Horizontal padding amount
    ///   - vertical: Vertical padding amount
    /// - Returns: View with standard padding applied
    func contentPadding(horizontal: CGFloat = Design.Spacing.large, vertical: CGFloat? = nil) -> some View {
        self
            .padding(.horizontal, horizontal)
            .padding(.vertical, vertical ?? horizontal)
    }
}

// MARK: - View Modifiers

/// Modifier for responsive font sizing that updates on layout changes
struct ResponsiveFontModifier: ViewModifier {
    let baseSize: CGFloat
    let isPortrait: Bool
    let showSeconds: Bool
    let showAmPm: Bool
    
    func body(content: Content) -> some View {
        content
            .font(.system(size: baseSize, weight: .bold, design: .rounded))
            .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
                // Force view update on orientation change
            }
    }
}

/// Modifier that forces view updates on orientation changes
struct OrientationChangeModifier: ViewModifier {
    @State private var orientation = UIDevice.current.orientation
    
    func body(content: Content) -> some View {
        content
            .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
                let newOrientation = UIDevice.current.orientation
                // Don't update on face down orientation to prevent redraw when putting phone down
                if newOrientation != .faceUp && newOrientation != .faceDown {
                    orientation = newOrientation
                }
            }
            .id(orientation.rawValue) // Force view recreation on orientation change
    }
}
