//
//  SettingsView.swift
//  App
//
//  Created by James Magahern on 3/3/21.
//

import SwiftUI

struct SettingsCategoryCell: View {
    @State var title: String = ""
    
    var body: some View {
        HStack {
            Text(title)
                .bold()
                .frame(height: 34.0)
        }
    }
}

struct AmberSettingsView: View {
    @Environment(\.presentationMode)
        @Binding private var presentationMode
    
    @State private var defaultSearchEngineName = Settings.shared.defaultSearchEngineName {
        didSet { Settings.shared.defaultSearchEngineName = defaultSearchEngineName }
    }
    
    var body: some View {
        NavigationView {
            List {
                Section(header: Text("Redirect Rules"), content: {
                    Text("To Do")
                })
                
                Section(header: Text("Search Provider"), content: {
                    ForEach(Array(Settings.shared.searchEngines.keys).sorted(), id: \.self, content: { name in
                        Button(action: { defaultSearchEngineName = name }, label: {
                            HStack {
                                Text(name)
                                Spacer()
                                if defaultSearchEngineName == name {
                                    Image(systemName: "checkmark")
                                }
                            }
                        }).buttonStyle(PlainButtonStyle())
                    })
                })
            }
            .listStyle(InsetGroupedListStyle())
            .navigationBarTitle("Settings", displayMode: .inline)
            .toolbar(content: {
                #if !targetEnvironment(macCatalyst)
                Button("Done", action: { presentationMode.dismiss() })
                #endif
            })
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        AmberSettingsView()
    }
}
