//
//  MatchFormatPickingView.swift
//  PadelClub
//
//  Created by razmig on 26/10/2024.
//

import SwiftUI

struct MatchFormatPickingView: View {
    @Environment(Tournament.self) var tournament
    var title: String? = nil
    @Binding var matchFormat: MatchFormat
    var validateAction: (() async -> ())

    @State private var confirmScheduleUpdate: Bool = false
    @State private var updatingInProgress : Bool = false

    var body: some View {
        Section {
            MatchTypeSelectionView(selectedFormat: $matchFormat, additionalEstimationDuration: tournament.additionalEstimationDuration)
            if confirmScheduleUpdate {
                RowButtonView("Recalculer les horaires") {
                    updatingInProgress = true
                    await validateAction()
                    updatingInProgress = false
                    confirmScheduleUpdate = false
                }
            }
        } header: {
            if let title {
                Text(title)
            }
        } footer: {
            if confirmScheduleUpdate && updatingInProgress == false {
                FooterButtonView("non, ne pas modifier les horaires") {
                    confirmScheduleUpdate = false
                }
            }
        }
        .headerProminence(.increased)
        .onChange(of: matchFormat) {
            confirmScheduleUpdate = true
        }
    }
}

