//
//  TournamentInscriptionView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 19/05/2024.
//

import SwiftUI
import LeStorage

struct TournamentInscriptionView: View {
    @EnvironmentObject var dataStore: DataStore
    var tournament: Tournament

    @ViewBuilder
    var body: some View {
        NavigationLink(value: Screen.inscription) {
            LabeledContent {
                Text(tournament.unsortedTeamsWithoutWO().count.formatted() + "/" + tournament.teamCount.formatted())
            } label: {
                Text("Gestion des inscriptions")
                if let closedRegistrationDate = tournament.closedRegistrationDate {
                    Text("clôturé le " + closedRegistrationDate.formatted(date: .abbreviated, time: .shortened))
                }
            }
        }
        if let endOfInscriptionDate = tournament.mandatoryRegistrationCloseDate(), tournament.inscriptionClosed() == false && tournament.hasStarted() == false {
            LabeledContent {
                Text(endOfInscriptionDate.formatted(date: .abbreviated, time: .shortened))
            } label: {
                Text("Date limite")
            }
        }
    }
    
    private func _save() {
        do {
            try dataStore.tournaments.addOrUpdate(instance: tournament)
        } catch {
            Logger.error(error)
        }
    }

}
