//
//  MatchFormatPickerView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 22/03/2024.
//

import SwiftUI

struct MatchFormatPickerView: View {
    @Environment(Tournament.self) var tournament: Tournament
    let headerLabel: String
    @Binding var matchFormat: MatchFormat
    @State private var isExpanded: Bool = false

    var body: some View {
        DisclosureGroup(isExpanded: $isExpanded) {
            Picker(selection: $matchFormat) {
                ForEach(MatchFormat.allCases, id: \.rawValue) { format in
                    Text(format.computedShortLabel).tag(format)
                }
            } label: {
            }
            .pickerStyle(.inline)
            .onChange(of: matchFormat) {
                isExpanded = false
            }
        } label: {
            descriptionView
        }
    }
    
    var descriptionView: some View {
        VStack(alignment: .leading) {
            HStack {
                Text(headerLabel).font(.footnote)
                Spacer()
                Text("Durée").font(.footnote)
            }
            HStack {
                Text(matchFormat.format).font(.title).fontWeight(.semibold)
                Spacer()
                VStack(alignment: .trailing) {
                    Text("~" + matchFormat.formattedEstimatedDuration(tournament.additionalEstimationDuration))
                    Text(matchFormat.formattedEstimatedBreakDuration() + " de pause").foregroundStyle(.secondary).font(.subheadline)
                }
            }
        }
    }
}


//#Preview {
//    List {
//        MatchFormatPickerView(headerLabel: "Test", matchFormat: .constant(MatchFormat.superTie))
//    }
//}
