import SwiftUI

// MARK: - Form Styling Extensions

extension View {
    /// Applies standard form styling with list configuration and backgrounds
    /// Use this on all Form views for consistency
    func standardFormStyle() -> some View {
        self
            .listStyle(.plain)
            .scrollContentBackground(.hidden)
            .background(Color.clear)
    }

    /// Applies standard section background styling
    /// Use this instead of manually adding `.listRowBackground(Color.appBackgroundSecondary)`
    func sectionBackground() -> some View {
        self.listRowBackground(Color.appBackgroundSecondary)
    }

    /// Applies clear background for header sections
    func headerSectionBackground() -> some View {
        self.listRowBackground(Color.clear)
    }
}

// MARK: - Loading State Extensions

extension View {
    /// Shows a loading overlay with optional message
    func loadingOverlay(isLoading: Bool, message: String = L10n.Tasks.loading) -> some View {
        ZStack {
            self
                .disabled(isLoading)
                .blur(radius: isLoading ? 3 : 0)

            if isLoading {
                StandardLoadingView(message: message)
            }
        }
    }
}

// MARK: - Conditional Modifier Extensions

extension View {
    /// Applies a modifier conditionally
    @ViewBuilder
    func `if`<Transform: View>(_ condition: Bool, transform: (Self) -> Transform) -> some View {
        if condition {
            transform(self)
        } else {
            self
        }
    }

    /// Applies one of two modifiers based on a condition
    @ViewBuilder
    func `if`<TrueContent: View, FalseContent: View>(
        _ condition: Bool,
        if ifTransform: (Self) -> TrueContent,
        else elseTransform: (Self) -> FalseContent
    ) -> some View {
        if condition {
            ifTransform(self)
        } else {
            elseTransform(self)
        }
    }
}

// MARK: - Safe Area Extensions

extension View {
    /// Ignores safe area for all edges
    func ignoresSafeAreaAll() -> some View {
        self.ignoresSafeArea(.all, edges: .all)
    }
}

// MARK: - Tap Gesture Extensions

extension View {
    /// Dismisses keyboard when tapped
    func dismissKeyboardOnTap() -> some View {
        self.onTapGesture {
            UIApplication.shared.sendAction(
                #selector(UIResponder.resignFirstResponder),
                to: nil,
                from: nil,
                for: nil
            )
        }
    }
}

// MARK: - Standard Loading View

struct StandardLoadingView: View {
    let message: String

    init(message: String = L10n.Tasks.loading) {
        self.message = message
    }

    var body: some View {
        VStack(spacing: OrganicSpacing.comfortable) {
            ZStack {
                Circle()
                    .fill(Color.appPrimary.opacity(0.1))
                    .frame(width: 64, height: 64)
                ProgressView()
                    .scaleEffect(1.2)
                    .tint(Color.appPrimary)
            }
            Text(message)
                .font(.system(size: 15, weight: .medium, design: .rounded))
                .foregroundColor(Color.appTextSecondary)
        }
        .padding(OrganicSpacing.spacious)
        .background(
            RoundedRectangle(cornerRadius: 24, style: .continuous)
                .fill(Color.appBackgroundSecondary)
                .overlay(
                    GrainTexture(opacity: 0.015)
                        .clipShape(RoundedRectangle(cornerRadius: 24, style: .continuous))
                )
        )
        .naturalShadow(.medium)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.appBackgroundPrimary.opacity(0.9))
    }
}
