//
//  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
    
    @State private var digitColor: Color = .white
    @State private var backgroundColor: Color = .black
    @State private var showAdvancedSettings = false
    
    // MARK: - Init
    init(style: ClockStyle, onCommit: @escaping (ClockStyle) -> Void) {
        self._style = State(initialValue: style)
        self.onCommit = onCommit
    }
    
    // MARK: - Body
    var body: some View {
        ScrollView {
            VStack(spacing: Design.Spacing.xxLarge) {
                BasicAppearanceSection(
                    style: $style,
                    digitColor: $digitColor,
                    backgroundColor: $backgroundColor
                )

                BasicDisplaySection(style: $style)

                if showAdvancedSettings {
                    AdvancedAppearanceSection(style: $style)

                    FontSection(style: $style)

                    NightModeSection(style: $style)

                    OverlaySection(style: $style)

                    AdvancedDisplaySection(style: $style)
                }

                SettingsSectionHeader(
                    title: "Advanced",
                    systemImage: "gearshape",
                    accentColor: AppAccent.primary
                )

                SettingsCard(backgroundColor: AppSurface.card, borderColor: AppBorder.subtle) {
                    SettingsToggle(
                        title: "Show Advanced Settings",
                        subtitle: "Reveal additional customization options",
                        isOn: $showAdvancedSettings,
                        accentColor: AppAccent.primary
                    )
                }

                #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"
                        )
                    }
                }
                #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("Clock 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 }
    )
}
