import SwiftUI

struct SettingsView: View {

    let keyManager: KeyManagement
    
    @Binding
    var serverAddress: String
    
    @Binding
    var localAddress: String
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(alignment: .leading, spacing: 8) {
                    VStack(alignment: .leading) {
                        Text("Server address")
                            .bold()
                        TextField("Server address", text: $serverAddress)
                            .foregroundColor(.secondary)
                            .padding(.leading, 8)
                    }.padding(.vertical, 8)
                    VStack(alignment: .leading) {
                        Text("Local address")
                            .bold()
                        TextField("Local address", text: $localAddress)
                            .foregroundColor(.secondary)
                            .padding(.leading, 8)
                    }.padding(.vertical, 8)
                    ForEach(KeyManagement.KeyType.allCases) { keyType in
                        SingleKeyView(
                            keyManager: keyManager,
                            type: keyType)
                    }
                }.padding()
            }.onDisappear {
                if !localAddress.hasSuffix("/") {
                    localAddress += "/"
                }
            }
            .navigationTitle("Settings")
        }
    }
}

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView(
            keyManager: KeyManagement(),
            serverAddress: .constant("https://example.com"),
            localAddress: .constant("192.168.178.42"))
    }
}
