//
//  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
    var systemImage: String? = nil
    let title: String
    let confirmationMessage: String
    let action: () -> ()
    @State private var askConfirmation: Bool = false

    init(_ title: String, role: ButtonRole? = nil, systemImage: String? = nil, confirmationMessage: String? = nil, action: @escaping () -> Void) {
        self.title = title
        self.systemImage = systemImage
        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: {
            if let systemImage {
                HStack {
                    Text(title)
                        .underline()
                    Image(systemName: systemImage).font(.caption)
                }
            } else {
                Text(title)
                    .underline()
            }
        }
        .buttonStyle(.borderless)
        .confirmationDialog("Confirmation",
                            isPresented: $askConfirmation,
                            titleVisibility: .visible) {
            Button("OK") {
                action()
            }
            Button("Annuler", role: .cancel) {}
        } message: {
            Text(confirmationMessage)
        }
    }
}
