import AppKit
import SwiftUI

struct GeneralSettingsView: View {
    @ObservedObject private var settingsController = AppSettingsController.shared

    private var maxOpenWidth: Double {
        let currentWidth = settingsController.settings.display.openWidth
        let screenWidth = NSScreen.screens.map { $0.frame.width - 40 }.max() ?? 1600
        return max(currentWidth, Double(screenWidth.rounded()))
    }

    private var maxOpenHeight: Double {
        let currentHeight = settingsController.settings.display.openHeight
        let screenHeight = NSScreen.screens.map { $0.frame.height - 20 }.max() ?? 900
        return max(currentHeight, Double(screenHeight.rounded()))
    }

    var body: some View {
        Form {
            Section("Display") {
                Toggle("Show on all displays", isOn: settingsController.binding(\.display.showOnAllDisplays))
                Toggle("Show menu bar icon", isOn: settingsController.binding(\.display.showMenuBarIcon))
                Toggle("Launch at login", isOn: settingsController.binding(\.display.launchAtLogin))
                    .onChange(of: settingsController.settings.display.launchAtLogin) { _, newValue in
                        LaunchAtLoginHelper.setEnabled(newValue)
                    }
            }

            Section("Hover Behavior") {
                Toggle("Open notch on hover", isOn: settingsController.binding(\.behavior.openNotchOnHover))
                if settingsController.settings.behavior.openNotchOnHover {
                    HStack {
                        Text("Hover delay")
                        Slider(value: settingsController.binding(\.behavior.minimumHoverDuration), in: 0.0...2.0, step: 0.05)
                        Text(String(format: "%.2fs", settingsController.settings.behavior.minimumHoverDuration))
                            .monospacedDigit()
                            .frame(width: 50)
                    }
                }
            }

            Section("Gestures") {
                Toggle("Enable gestures", isOn: settingsController.binding(\.behavior.enableGestures))
                if settingsController.settings.behavior.enableGestures {
                    HStack {
                        Text("Sensitivity")
                        Slider(value: settingsController.binding(\.behavior.gestureSensitivity), in: 0.1...1.0, step: 0.05)
                        Text(String(format: "%.2f", settingsController.settings.behavior.gestureSensitivity))
                            .monospacedDigit()
                            .frame(width: 50)
                    }
                }
            }

            Section("Closed Notch Size") {
                Picker("Notch screens", selection: settingsController.binding(\.display.notchHeightMode)) {
                    ForEach(NotchHeightMode.allCases) { mode in
                        Text(mode.label).tag(mode.rawValue)
                    }
                }
                if settingsController.settings.display.notchHeightMode == NotchHeightMode.custom.rawValue {
                    HStack {
                        Text("Custom height")
                        Slider(value: settingsController.binding(\.display.notchHeight), in: 16...64, step: 1)
                        Text("\(Int(settingsController.settings.display.notchHeight))pt")
                            .monospacedDigit()
                            .frame(width: 50)
                    }
                }

                Picker("Non-notch screens", selection: settingsController.binding(\.display.nonNotchHeightMode)) {
                    ForEach(NonNotchHeightMode.allCases) { mode in
                        Text(mode.label).tag(mode.rawValue)
                    }
                }
                if settingsController.settings.display.nonNotchHeightMode == NonNotchHeightMode.custom.rawValue {
                    HStack {
                        Text("Custom height")
                        Slider(value: settingsController.binding(\.display.nonNotchHeight), in: 16...64, step: 1)
                        Text("\(Int(settingsController.settings.display.nonNotchHeight))pt")
                            .monospacedDigit()
                            .frame(width: 50)
                    }
                }
            }

            Section("Open Notch Size") {
                HStack {
                    Text("Width")
                    Slider(value: settingsController.binding(\.display.openWidth), in: 320...maxOpenWidth, step: 10)
                    Text("\(Int(settingsController.settings.display.openWidth))pt")
                        .monospacedDigit()
                        .frame(width: 60)
                }
                HStack {
                    Text("Height")
                    Slider(value: settingsController.binding(\.display.openHeight), in: 140...maxOpenHeight, step: 10)
                    Text("\(Int(settingsController.settings.display.openHeight))pt")
                        .monospacedDigit()
                        .frame(width: 60)
                }
            }
        }
        .formStyle(.grouped)
    }
}
