//
//  MatchListView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 03/04/2024.
//

import SwiftUI

struct MatchListView: View {
    @EnvironmentObject var dataStore: DataStore
    let section: String
    let matches: [Match]?
    var matchViewStyle: MatchViewStyle = .standardStyle
    var hideWhenEmpty: Bool = false
    
    @State var isExpanded: Bool = true
    
    private func _shouldHide() -> Bool {
        if matches != nil && matches!.isEmpty && hideWhenEmpty == true {
            return true
        } else {
            return false
        }
    }
        
    @ViewBuilder
    var body: some View {
        if _shouldHide() == false {
            Section {
                DisclosureGroup(isExpanded: $isExpanded) {
                    if let matches {
                        ForEach(matches) { match in
                            MatchRowView(match: match, matchViewStyle: matchViewStyle)
                                .listRowInsets(EdgeInsets(top: 0, leading: -2, bottom: 0, trailing: 8))
                        }
                    }
                } label: {
                    LabeledContent {
                        if matches == nil {
                            ProgressView()
                        } else {
                            Text(matches!.count.formatted() + " match" + matches!.count.pluralSuffix)
                        }
                    } label: {
                        Text(section.firstCapitalized)
                    }
                }
            }
        }
    }
}
