//
//  EventView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 17/05/2024.
//

import SwiftUI
import LeStorage

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

    case links
    case tournaments(Event)
    case cashier
    
    var id: String {
        return String(describing: self)
    }
    
    func selectionLabel() -> String {
        switch self {
        case .links:
            return "Liens"
        case .tournaments:
            return "Épreuves"
        case .cashier:
            return "Finance"
        }
    }
    
    func badgeValue() -> Int? {
        switch self {
        case .links:
            return nil
        case .tournaments(let event):
            return event.tournaments.count
        case .cashier:
            return nil
        }
    }
    
    func badgeValueColor() -> Color? {
        return .logoBackground
    }
    
    func badgeImage() -> Badge? {
        return nil
    }

}

struct EventView: View {
    let event: Event
    @State private var selectedDestination: EventDestination?
    
    func allDestinations() -> [EventDestination] {
        [.links, .tournaments(event), .cashier]
    }
    
    var body: some View {
        VStack(spacing: 0) {
            GenericDestinationPickerView(selectedDestination: $selectedDestination, destinations: allDestinations(), nilDestinationIsValid: true)
            switch selectedDestination {
            case .none:
                EventSettingsView(event: event)
            case .some(let selectedEventDestination):
                switch selectedEventDestination {
                case .links:
                    EventLinksView(event: event)
                case .tournaments(let event):
                    EventTournamentsView(event: event)
                case .cashier:
                    CashierDetailView(tournaments: event.tournaments)
                }
            }
        }
        .headerProminence(.increased)
        .toolbarBackground(.visible, for: .navigationBar)
        .navigationBarTitleDisplayMode(.inline)
        .navigationTitle("Gestion de l'événement")
    }
}
