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

import SwiftUI

struct DatePickingViewWithFormat: View {
    @Environment(Tournament.self) var tournament
    @Binding var matchFormat: MatchFormat
    let title: String
    @Binding var startDate: Date
    @Binding var currentDate: Date?
    var duration: Int?
    var validateAction: ((Bool) async -> ())
    
    @State private var confirmScheduleUpdate: Bool = false
    @State private var updatingInProgress : Bool = false

    var body: some View {
        Section {
            MatchTypeSelectionView(selectedFormat: $matchFormat, additionalEstimationDuration: tournament.additionalEstimationDuration)
            DatePicker(selection: $startDate) {
                Text(startDate.formatted(.dateTime.weekday(.wide))).font(.headline)
            }
            if confirmScheduleUpdate {
                RowButtonView("Sauver et modifier la suite") {
                    updatingInProgress = true
                    await validateAction(true)
                    updatingInProgress = false
                    confirmScheduleUpdate = false
                }
            }
        } header: {
            Text(title)
        } footer: {
            if confirmScheduleUpdate && updatingInProgress == false {
                HStack {
                    FooterButtonView("sauver sans modifier la suite") {
                        Task {
                            await validateAction(false)
                            confirmScheduleUpdate = false
                        }
                    }
                    Text("ou")
                    FooterButtonView("annuler") {
                        confirmScheduleUpdate = false
                    }
                }
            } else {
                HStack {
                    Menu {
                        Button("de 30 minutes") {
                            startDate = startDate.addingTimeInterval(1800)
                        }
                        
                        Button("d'une heure") {
                            startDate = startDate.addingTimeInterval(3600)
                        }
                        
                        Button("à 9h") {
                            startDate = startDate.atNine()
                        }
                        
                        Button("à demain 9h") {
                            startDate = startDate.tomorrowAtNine
                        }
                        
                        if let duration {
                            Button("à la prochaine rotation") {
                                startDate = startDate.addingTimeInterval(Double(duration) * 60)
                            }
                            Button("à la précédente rotation") {
                                startDate = startDate.addingTimeInterval(Double(duration) * -60)
                            }
                        }
                    } label: {
                        Text("décaler")
                            .underline()
                    }
                    .buttonStyle(.borderless)
                    Spacer()
                    
                    if currentDate != nil {
                        FooterButtonView("retirer l'horaire bloqué") {
                            currentDate = nil
                        }
                    } else {
                        FooterButtonView("bloquer l'horaire") {
                            currentDate = startDate
                        }
                    }
                }
                .buttonStyle(.borderless)
            }

        }
        .headerProminence(.increased)
        .onChange(of: matchFormat) {
            confirmScheduleUpdate = true
        }
        .onChange(of: startDate) {
            confirmScheduleUpdate = true
        }
    }
}
