import SwiftUI
import VNCCore

public struct SettingsView: View {
    @Environment(\.dismiss) private var dismiss

    @AppStorage("clipboardSyncEnabled") private var clipboardSyncDefault = true
    @AppStorage("defaultInputMode") private var defaultInputModeRaw = "touch"
    @AppStorage("autoReconnectEnabled") private var autoReconnect = true
    @AppStorage("reduceMotionInSession") private var reduceMotion = false

    public init() {}

    public var body: some View {
        NavigationStack {
            Form {
                Section {
                    HStack(spacing: 14) {
                        ZStack {
                            RoundedRectangle(cornerRadius: 14, style: .continuous)
                                .fill(LinearGradient(
                                    colors: [.blue, .purple],
                                    startPoint: .topLeading,
                                    endPoint: .bottomTrailing
                                ))
                                .frame(width: 56, height: 56)
                            Image(systemName: "display")
                                .font(.title.weight(.semibold))
                                .foregroundStyle(.white)
                        }
                        VStack(alignment: .leading, spacing: 2) {
                            Text("Screens")
                                .font(.headline)
                            Text("Version \(Self.shortVersion) (\(Self.buildNumber))")
                                .font(.caption)
                                .foregroundStyle(.secondary)
                        }
                        Spacer()
                    }
                    .padding(.vertical, 4)
                }

                Section {
                    Picker("Default input mode", selection: $defaultInputModeRaw) {
                        Text("Touch").tag("touch")
                        Text("Trackpad").tag("trackpad")
                    }
                } header: {
                    Text("Input")
                } footer: {
                    Text("Each saved connection can override this.")
                }

                Section {
                    Toggle("Auto-reconnect on drop", isOn: $autoReconnect)
                } header: {
                    Text("Connection")
                } footer: {
                    Text("Retries with jittered exponential backoff up to ~30 seconds.")
                }

                Section {
                    Toggle("Sync clipboard with remote", isOn: $clipboardSyncDefault)
                } header: {
                    Text("Privacy")
                } footer: {
                    Text("Each connection can override. Passwords are stored in iOS Keychain on this device only — they never sync to iCloud.")
                }

                Section {
                    Toggle("Reduce motion in session", isOn: $reduceMotion)
                } header: {
                    Text("Display")
                }

                Section {
                    Link(destination: URL(string: "https://example.com/privacy")!) {
                        Label("Privacy policy", systemImage: "hand.raised")
                    }
                    Link(destination: URL(string: "https://github.com/royalapplications/royalvnc")!) {
                        Label("RoyalVNCKit on GitHub", systemImage: "link")
                    }
                } header: {
                    Text("About")
                }
            }
            #if os(iOS)
            .scrollContentBackground(.hidden)
            #endif
            .background(formBackground.ignoresSafeArea())
            .navigationTitle("Settings")
            #if os(iOS)
            .navigationBarTitleDisplayMode(.inline)
            #endif
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button("Done") { dismiss() }
                }
            }
        }
    }

    private var formBackground: some View {
        LinearGradient(
            colors: [
                Color(red: 0.04, green: 0.05, blue: 0.10),
                Color(red: 0.02, green: 0.02, blue: 0.05)
            ],
            startPoint: .top,
            endPoint: .bottom
        )
    }

    private static var shortVersion: String {
        Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0"
    }

    private static var buildNumber: String {
        Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "0"
    }
}
