//
//  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 club(Event)
    case links
    case tournaments(Event)
    case cashier
    
    var id: String {
        return String(describing: self)
    }
    
    func selectionLabel(index: Int) -> String {
        switch self {
        case .club:
            return "Club"
        case .links:
            return "Liens"
        case .tournaments:
            return "Tournois"
        case .cashier:
            return "Finance"
        }
    }
    
    func badgeValue() -> Int? {
        switch self {
        case .links, .club:
            return nil
        case .tournaments(let event):
            return event.tournaments.count
        case .cashier:
            return nil
        }
    }
    
    func badgeValueColor() -> Color? {
        return .logoBackground
    }
    
    func badgeImage() -> Badge? {

        switch self {
        case .club(let event):
            if event.club != nil {
                return .checkmark
            } else {
                return .xmark
            }
        default:
            return nil
        }
    }

}

struct EventView: View {
    let event: Event
    @State private var selectedDestination: EventDestination?
    
    init(event: Event) {
        self.event = event
        _selectedDestination = State(wrappedValue: event.club == nil ? .club(event) : nil)
    }
    
    func allDestinations() -> [EventDestination] {
        [.club(event), .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 .club(let event):
                    EventClubSettingsView(event: event)
                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")
    }
}
