//
//  TournamentRunningView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 23/03/2024.
//

import SwiftUI

struct TournamentRunningView: View {
    var tournament: Tournament
    var allMatches: [Match]
    
    init(tournament: Tournament) {
        self.tournament = tournament
        self.allMatches = tournament.allMatches()
    }
    
    @ViewBuilder
    var body: some View {
        
        let runningMatches = Tournament.runningMatches(allMatches)
        let matchesLeft = Tournament.matchesLeft(allMatches)
        let readyMatches = Tournament.readyMatches(allMatches)
        let availableToStart = Tournament.availableToStart(allMatches, in: runningMatches, checkCanPlay: true)

        Section {
            MatchListView(section: "prêt à démarrer", matches: availableToStart, hideWhenEmpty: tournament.hasEnded(), isExpanded: true)
        }

        Section {
            MatchListView(section: "à lancer", matches: readyMatches, hideWhenEmpty: tournament.hasEnded(), isExpanded: availableToStart.isEmpty)
        }

        Section {
            MatchListView(section: "à venir", matches: matchesLeft, hideWhenEmpty: true, isExpanded: readyMatches.isEmpty)
        }
        
        Section {
            MatchListView(section: "en cours", matches: runningMatches, hideWhenEmpty: tournament.hasEnded(), isExpanded: matchesLeft.isEmpty)
        }
        
        Section {
            let finishedMatches = Tournament.finishedMatches(allMatches, limit: tournament.courtCount)
            MatchListView(section: "Dernier\(finishedMatches.count.pluralSuffix) match\(finishedMatches.count.pluralSuffix) terminé\(finishedMatches.count.pluralSuffix)", matches: finishedMatches, isExpanded: tournament.hasEnded())
        }
    }
}

//#Preview {
//    TournamentRunningView(tournament: Tournament.mock())
//}
