//
//  GlobalSettingsView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 16/10/2023.
//

import SwiftUI
import LeStorage

struct GlobalSettingsView: View {
    @EnvironmentObject var dataStore : DataStore
    
    var body: some View {
        @Bindable var user = dataStore.user
        List {
            Section {
                Picker(selection: $user.groupStageMatchFormatPreference) {
                    Text("Automatique").tag(nil as MatchFormat?)
                    ForEach(MatchFormat.allCases, id: \.self) { format in
                        Text(format.format).tag(format as MatchFormat?)
                    }
                } label: {
                    HStack {
                        Text("Poule")
                        Spacer()
                    }
                }
                Picker(selection: $user.bracketMatchFormatPreference) {
                    Text("Automatique").tag(nil as MatchFormat?)
                    ForEach(MatchFormat.allCases, id: \.self) { format in
                        Text(format.format).tag(format as MatchFormat?)
                    }
                } label: {
                    HStack {
                        Text("Tableau")
                        Spacer()
                    }
                }
                Picker(selection: $user.loserBracketMatchFormatPreference) {
                    Text("Automatique").tag(nil as MatchFormat?)
                    ForEach(MatchFormat.allCases, id: \.self) { format in
                        Text(format.format).tag(format as MatchFormat?)
                    }
                } label: {
                    HStack {
                        Text("Match de classement")
                        Spacer()
                    }
                }
            } header: {
                Text("Vos formats préférés")
            } footer: {
                Text("À minima, les règles fédérales seront toujours prises en compte par défaut.")
            }
        }
        .onChange(of: [
            user.bracketMatchFormatPreference,
            user.groupStageMatchFormatPreference,
            user.loserBracketMatchFormatPreference
        ]) {
            self.dataStore.saveUser()
        }
        .navigationTitle("Formats par défaut")
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
    }
}
