//
//  TournamentSettingsView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 22/03/2024.
//

import SwiftUI

enum TournamentSettings: Identifiable, Selectable, Equatable {
    static func == (lhs: TournamentSettings, rhs: TournamentSettings) -> Bool {
        return lhs.id == rhs.id
    }

    case status
    case general
    case club(Tournament)
    case matchFormats

    var id: String { String(describing: self) }

    func selectionLabel() -> String {
        switch self {
        case .status:
            return "Statut"
        case .matchFormats:
            return "Formats"
        case .general:
            return "Général"
        case .club:
            return "Club"
        }
    }
    
    func badgeValue() -> Int? {
        nil
    }
    
    func badgeValueColor() -> Color? {
        return nil
    }
    
    func badgeImage() -> Badge? {
        switch self {
        case .club(let tournament):
            if tournament.club() != nil {
                return .checkmark
            } else {
                return .xmark
            }
        default:
            return nil
        }
    }
}

struct TournamentSettingsView: View {
    @State private var selectedDestination: TournamentSettings? = .general
    @Environment(Tournament.self) var tournament: Tournament
    
    private func destinations() -> [TournamentSettings] {
        [.status, .general, .club(tournament), .matchFormats]
    }
    
    var body: some View {
        VStack(spacing: 0) {
            GenericDestinationPickerView(selectedDestination: $selectedDestination, destinations: destinations(), nilDestinationIsValid: false)
            switch selectedDestination! {
            case .status:
                TournamentStatusView()
            case .matchFormats:
                TournamentMatchFormatsSettingsView()
            case .general:
                TournamentGeneralSettingsView(tournament: tournament)
            case .club:
                TournamentClubSettingsView()
            }
        }
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
        .navigationTitle("Réglages")
    }

}

//#Preview {
//    TournamentSettingsView()
//}
