//
//  CashierDetailView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 31/03/2024.
//

import SwiftUI

struct CashierDetailView: View {
    var tournaments : [Tournament]

    init(tournaments: [Tournament]) {
        self.tournaments = tournaments
    }
    
    init(tournament: Tournament) {
        self.tournaments = [tournament]
    }
    
    var body: some View {
        List {
            ForEach(tournaments) { tournament in
                Section {
                    LabeledContent {
                        Text(tournament.earnings().formatted(.currency(code: "EUR").precision(.fractionLength(0))))
                    } label: {
                        Text("Encaissement")
                        Text(tournament.paidCompletion().formatted(.percent.precision(.fractionLength(0)))).foregroundStyle(.secondary)
                    }
                    _tournamentCashierDetailView(tournament)
                } header: {
                    if tournaments.count > 1 {
                        Text(tournament.tournamentTitle())
                    }
                }
            }
        }
        .headerProminence(.increased)
    }
    
    private func _tournamentCashierDetailView(_ tournament: Tournament) -> some View {
        DisclosureGroup {
            ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in
                let count = tournament.selectedPlayers().filter({ $0.paymentType == type }).count
                if count > 0 {
                    LabeledContent {
                        if let entryFee = tournament.entryFee {
                            let sum = Double(count) * entryFee
                            Text(sum.formatted(.currency(code: "EUR")))
                        }
                    } label: {
                        Text(type.localizedLabel())
                        Text(count.formatted())
                    }
                }
            }
        } label: {
            Text("Voir le détail")
        }
    
//        
//        Section {
//            ForEach(tournaments) { tournament in
//            }
////                HStack {
////                    Text("Total")
////                    Spacer()
////                    Text(event.earnings.formatted(.currency(code: "EUR").precision(.fractionLength(0))))
////                    Text(event.paidCompletion.formatted(.percent.precision(.fractionLength(0)))).foregroundStyle(.secondary)
////                }
//        } header: {
//            Text("Encaissement")
//        }

    }
}
