//
//  AdaptiveLayout.swift
//  Andromida
//
//  Reusable layout modifiers and helpers for responsive iPad/iPhone layouts.
//

import SwiftUI
import Bedrock

// MARK: - Adaptive Content Width Modifier

/// A view modifier that constrains content width on regular-width displays (iPad, large iPhone landscape).
/// On compact-width displays (iPhone portrait), content remains full-width.
struct AdaptiveContentWidth: ViewModifier {
    @Environment(\.horizontalSizeClass) private var horizontalSizeClass
    
    let maxWidth: CGFloat
    let centerContent: Bool
    
    init(maxWidth: CGFloat = Design.Size.maxContentWidthLandscape, centerContent: Bool = true) {
        self.maxWidth = maxWidth
        self.centerContent = centerContent
    }
    
    func body(content: Content) -> some View {
        if horizontalSizeClass == .regular {
            if centerContent {
                content
                    .frame(maxWidth: maxWidth)
                    .frame(maxWidth: .infinity)
            } else {
                content
                    .frame(maxWidth: maxWidth)
            }
        } else {
            content
        }
    }
}

extension View {
    /// Constrains the view's width on regular-width displays (iPad, large iPhone landscape).
    /// - Parameters:
    ///   - maxWidth: The maximum width to constrain to. Defaults to `Design.Size.maxContentWidthLandscape` (800pt).
    ///   - centered: Whether to center the content horizontally. Defaults to `true`.
    /// - Returns: A view with adaptive width constraints applied.
    func adaptiveContentWidth(
        maxWidth: CGFloat = Design.Size.maxContentWidthLandscape,
        centered: Bool = true
    ) -> some View {
        modifier(AdaptiveContentWidth(maxWidth: maxWidth, centerContent: centered))
    }
}

// MARK: - Adaptive Grid Columns

/// Helper for creating responsive grid columns based on horizontal size class.
enum AdaptiveColumns {
    /// Creates grid columns that adapt based on horizontal size class.
    /// - Parameters:
    ///   - compactCount: Number of columns for compact width (iPhone portrait). Defaults to 1.
    ///   - regularCount: Number of columns for regular width (iPad, landscape). Defaults to 2.
    ///   - spacing: Spacing between columns. Defaults to `Design.Spacing.medium`.
    ///   - horizontalSizeClass: The current horizontal size class.
    /// - Returns: An array of `GridItem` configured for the given size class.
    static func columns(
        compactCount: Int = 1,
        regularCount: Int = 2,
        spacing: CGFloat = Design.Spacing.medium,
        horizontalSizeClass: UserInterfaceSizeClass?
    ) -> [GridItem] {
        let count = horizontalSizeClass == .regular ? regularCount : compactCount
        return Array(repeating: GridItem(.flexible(), spacing: spacing), count: count)
    }
}

// MARK: - Use Wide Layout Helper

/// Environment-based helper view that provides layout decisions.
struct AdaptiveLayoutReader<Content: View>: View {
    @Environment(\.horizontalSizeClass) private var horizontalSizeClass
    
    let content: (_ useWideLayout: Bool) -> Content
    
    init(@ViewBuilder content: @escaping (_ useWideLayout: Bool) -> Content) {
        self.content = content
    }
    
    var body: some View {
        content(horizontalSizeClass == .regular)
    }
}
