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

import SwiftUI
import LeStorage

struct GlobalSettingsView: View {
    @EnvironmentObject var dataStore : DataStore
    
    var groupStageMatchFormat: Binding<MatchFormat> {
        Binding {
            dataStore.user.groupStageMatchFormatPreference ?? .nineGames
        } set: { value in
            dataStore.user.groupStageMatchFormatPreference = value
        }
    }
    
    var groupStageMatchFormatPreference: Binding<Bool> {
        Binding {
            dataStore.user.groupStageMatchFormatPreference == nil
        } set: { value in
            if value {
                dataStore.user.groupStageMatchFormatPreference = nil
            } else {
                dataStore.user.groupStageMatchFormatPreference = .nineGames
            }
        }

    }
    
    var bracketMatchFormat: Binding<MatchFormat> {
        Binding {
            dataStore.user.bracketMatchFormatPreference ?? .nineGames
        } set: { value in
            dataStore.user.bracketMatchFormatPreference = value
        }
    }
    
    var bracketMatchFormatPreference: Binding<Bool> {
        Binding {
            dataStore.user.bracketMatchFormatPreference == nil
        } set: { value in
            if value {
                dataStore.user.bracketMatchFormatPreference = nil
            } else {
                dataStore.user.bracketMatchFormatPreference = .nineGames
            }
        }

    }
    
    var loserBracketMatchFormat: Binding<MatchFormat> {
        Binding {
            dataStore.user.loserBracketMatchFormatPreference ?? .nineGames
        } set: { value in
            dataStore.user.loserBracketMatchFormatPreference = value
        }
    }
    
    var loserBracketMatchFormatPreference: Binding<Bool> {
        Binding {
            dataStore.user.loserBracketMatchFormatPreference == nil
        } set: { value in
            if value {
                dataStore.user.loserBracketMatchFormatPreference = nil
            } else {
                dataStore.user.loserBracketMatchFormatPreference = .nineGames
            }
        }

    }
    
    var body: some View {
        @Bindable var user = dataStore.user
        List {
            Section {
                Toggle(isOn: groupStageMatchFormatPreference) {
                    Text("Automatique")
                }
                
                if groupStageMatchFormatPreference.wrappedValue == false {
                    MatchTypeSelectionView(selectedFormat: groupStageMatchFormat)
                }
            } header: {
                Text("Poule")
            } footer: {
                Text("À minima, les règles fédérales seront toujours prises en compte par défaut.")
            }
            
            Section {
                Toggle(isOn: bracketMatchFormatPreference) {
                    Text("Automatique")
                }
                
                if bracketMatchFormatPreference.wrappedValue == false {
                    MatchTypeSelectionView(selectedFormat: bracketMatchFormat)
                }
            } header: {
                Text("Tableau")
            } footer: {
                Text("À minima, les règles fédérales seront toujours prises en compte par défaut.")
            }

            Section {
                Toggle(isOn: loserBracketMatchFormatPreference) {
                    Text("Automatique")
                }
                
                if loserBracketMatchFormatPreference.wrappedValue == false {
                    MatchTypeSelectionView(selectedFormat: loserBracketMatchFormat)
                }
            } header: {
                Text("Match de classement")
            } footer: {
                Text("À minima, les règles fédérales seront toujours prises en compte par défaut.")
            }
        }
        .headerProminence(.increased)
        .onChange(of: [
            user.bracketMatchFormatPreference,
            user.groupStageMatchFormatPreference,
            user.loserBracketMatchFormatPreference
        ]) {
            self.dataStore.saveUser()
        }
        .navigationTitle("Formats par défaut")
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
    }
}
