//
//  FooterButtonView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 18/04/2024.
//

import SwiftUI

fileprivate let defaultConfirmationMessage = "Êtes-vous sûr de vouloir faire cela ?"

struct FooterButtonView: View {
    var role: ButtonRole? = nil
    let title: String
    let confirmationMessage: String
    let action: () -> ()
    @State private var askConfirmation: Bool = false

    init(_ title: String, role: ButtonRole? = nil, confirmationMessage: String? = nil, action: @escaping () -> Void) {
        self.title = title
        self.action = action
        self.role = role
        self.confirmationMessage = confirmationMessage ?? defaultConfirmationMessage
    }
    
    var body: some View {
        Button(role: role) {
            if role == .destructive {
                askConfirmation = true
            } else {
                action()
            }
        } label: {
            Text(title)
                .underline()
        }
        .buttonStyle(.borderless)
        .confirmationDialog("Confirmation",
                            isPresented: $askConfirmation,
                            titleVisibility: .visible) {
            Button("OK") {
                action()
            }
            Button("Annuler", role: .cancel) {}
        } message: {
            Text(confirmationMessage)
        }
    }
}
