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

import SwiftUI
import Bedrock

/// Settings interface for clock customization
struct ClockSettingsView: View {
    
    // MARK: - Properties
    @State private var style: ClockStyle
    let onCommit: (ClockStyle) -> Void
    var onResetOnboarding: (() -> Void)?
    
    @State private var digitColor: Color = .white
    @State private var backgroundColor: Color = .black
    
    // MARK: - Init
    init(
        style: ClockStyle,
        onCommit: @escaping (ClockStyle) -> Void,
        onResetOnboarding: (() -> Void)? = nil
    ) {
        self._style = State(initialValue: style)
        self.onCommit = onCommit
        self.onResetOnboarding = onResetOnboarding
    }
    
    // MARK: - Body
    var body: some View {
        let isPad = UIDevice.current.userInterfaceIdiom == .pad
        ScrollView {
            VStack(spacing: Design.Spacing.xxLarge) {
                BasicAppearanceSection(
                    style: $style,
                    digitColor: $digitColor,
                    backgroundColor: $backgroundColor
                )

                FontSection(style: $style)

                AdvancedAppearanceSection(style: $style)

                BasicDisplaySection(style: $style)

                AdvancedDisplaySection(style: $style)

                NightModeSection(style: $style)

                OverlaySection(style: $style)

                #if DEBUG
                SettingsSectionHeader(
                    title: "Debug",
                    systemImage: "ant.fill",
                    accentColor: AppStatus.error
                )

                SettingsCard(backgroundColor: AppSurface.card, borderColor: AppBorder.subtle) {
                    SettingsNavigationRow(
                        title: "Icon Generator",
                        subtitle: "Generate and save app icon",
                        backgroundColor: AppSurface.primary
                    ) {
                        IconGeneratorView(config: .noiseClock, appName: "TheNoiseClock")
                    }

                    SettingsNavigationRow(
                        title: "Branding Preview",
                        subtitle: "Preview icon and launch screen",
                        backgroundColor: AppSurface.primary
                    ) {
                        BrandingPreviewView(
                            iconConfig: .noiseClock,
                            launchConfig: .noiseClock,
                            appName: "TheNoiseClock"
                        )
                    }
                    
                    if let onResetOnboarding {
                        Divider()
                            .background(AppBorder.subtle)
                        
                        Button {
                            onResetOnboarding()
                        } label: {
                            HStack {
                                VStack(alignment: .leading, spacing: Design.Spacing.xxSmall) {
                                    Text("Reset Onboarding")
                                        .typography(.body)
                                        .foregroundStyle(AppTextColors.primary)
                                    Text("Show onboarding screens again on next launch")
                                        .typography(.caption)
                                        .foregroundStyle(AppTextColors.secondary)
                                }
                                Spacer()
                                Image(systemName: "arrow.counterclockwise")
                                    .foregroundStyle(AppAccent.primary)
                            }
                            .padding(Design.Spacing.medium)
                            .background(AppSurface.primary)
                        }
                        .buttonStyle(.plain)
                    }
                }
                #endif
            }
            .frame(maxWidth: Design.Size.maxContentWidthPortrait)
            .frame(maxWidth: .infinity, alignment: .center)
            .padding(.horizontal, Design.Spacing.large)
            .padding(.top, Design.Spacing.large)
            .padding(.bottom, Design.Spacing.xxxLarge)
        }
        .background(AppSurface.primary)
        .navigationTitle(isPad ? "" : "Settings")
        .navigationBarTitleDisplayMode(.inline)
        .onAppear {
            digitColor = Color(hex: style.digitColorHex) ?? .white
            backgroundColor = Color(hex: style.backgroundHex) ?? .black
        }
        .onDisappear {
            onCommit(style)
        }
    }
}

// MARK: - Preview
#Preview {
    ClockSettingsView(
        style: ClockStyle(),
        onCommit: { _ in }
    )
}
