//
//  SettingsView.swift
//  Cable
//
//  Created by Stefan Lange-Hegermann on 09.10.25.
//


import SwiftUI

struct SettingsView: View {
    @EnvironmentObject var unitSettings: UnitSystemSettings
    @Environment(\.dismiss) private var dismiss
    @Environment(\.openURL) private var openURL

    var body: some View {
        NavigationStack {
            Form {
                Section("Units") {
                    Picker("Unit System", selection: $unitSettings.unitSystem) {
                        ForEach(UnitSystem.allCases, id: \.self) { system in
                            Text(system.displayName).tag(system)
                        }
                    }
                    .pickerStyle(.segmented)
                }
                Section {
                    VStack(alignment: .leading, spacing: 12) {
                        HStack(spacing: 8) {
                            Image(systemName: "exclamationmark.triangle.fill")
                                .foregroundColor(.orange)
                                .font(.system(size: 18))
                            Text("Safety Disclaimer")
                                .font(.headline)
                                .fontWeight(.semibold)
                        }

                        VStack(alignment: .leading, spacing: 8) {
                            Text("This application provides electrical calculations for educational and estimation purposes only.")
                                .font(.body)

                            Text("Important:")
                                .font(.subheadline)
                                .fontWeight(.semibold)

                            VStack(alignment: .leading, spacing: 4) {
                                Text("• Always consult qualified electricians for actual installations")
                                Text("• Follow all local electrical codes and regulations")
                                Text("• Electrical work should only be performed by licensed professionals")
                                Text("• These calculations may not account for all environmental factors")
                                Text("• The app developers assume no liability for electrical installations")
                            }
                            .font(.caption)
                            .foregroundColor(.secondary)
                        }
                    }
                    .padding(.vertical, 8)
                }
            }
            .navigationTitle("Settings")
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("Close") {
                        dismiss()
                    }
                }
            }
        }
    }
}

#Preview("Settings (Default)") {
    let settings = UnitSystemSettings()
    return SettingsView()
        .environmentObject(settings)
}
