//
//  DateUpdateManagerView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 17/04/2024.
//

import SwiftUI

struct DatePickingView: View {
    let title: String
    @Binding var startDate: Date
    @Binding var currentDate: Date?
    var duration: Int?
    var validateAction: (() async -> ())
        
    @State private var confirmFollowingScheduleUpdate: Bool = false
    @State private var updatingInProgress: Bool = false

    var body: some View {
        Section {
            DatePicker(selection: $startDate) {
                Text(startDate.formatted(.dateTime.weekday(.wide))).font(.headline)
            }
            if confirmFollowingScheduleUpdate {
                RowButtonView("Modifier la suite du programme") {
                    updatingInProgress = true
                    await validateAction()
                    updatingInProgress = false
                    confirmFollowingScheduleUpdate = false
                }
            }
        } header: {
            Text(title)
        } footer: {
            if confirmFollowingScheduleUpdate && updatingInProgress == false {
                FooterButtonView("non, ne pas modifier la suite") {
                    currentDate = startDate
                    confirmFollowingScheduleUpdate = 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)
            }
        }
        .onChange(of: startDate) {
            confirmFollowingScheduleUpdate = true
        }
        .headerProminence(.increased)
    }
}

struct MatchFormatPickingView: View {
    @Binding var matchFormat: MatchFormat
    var validateAction: (() async -> ())

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

    var body: some View {
        Section {
            MatchFormatPickerView(headerLabel: "Format", matchFormat: $matchFormat)
            if confirmScheduleUpdate {
                RowButtonView("Recalculer les horaires") {
                    updatingInProgress = true
                    await validateAction()
                    updatingInProgress = false
                    confirmScheduleUpdate = false
                }
            }
        } footer: {
            if confirmScheduleUpdate && updatingInProgress == false {
                FooterButtonView("non, ne pas modifier les horaires") {
                    confirmScheduleUpdate = false
                }
            }
        }
        .headerProminence(.increased)
        .onChange(of: matchFormat) {
            confirmScheduleUpdate = true
        }
    }
}
