//
//  RowButtonView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 03/03/2024.
//

import SwiftUI

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

struct RowButtonView: View {
    var role: ButtonRole? = nil
    let title: String
    var systemImage: String? = nil
    var image: String? = nil
    let confirmationMessage: String
    var action: (() -> ())? = nil
    var asyncAction: (() async -> ())? = nil

    @State private var askConfirmation: Bool = false
    @State private var isLoading = false

    init(_ title: String, role: ButtonRole? = nil, systemImage: String? = nil, image: String? = nil, confirmationMessage: String? = nil, action: @escaping (() -> ())) {
        self.role = role
        self.title = title
        self.systemImage = systemImage
        self.image = image
        self.confirmationMessage = confirmationMessage ?? defaultConfirmationMessage
        self.action = action
    }
    
    init(_ title: String, role: ButtonRole? = nil, systemImage: String? = nil, image: String? = nil, confirmationMessage: String? = nil, asyncAction: @escaping (() async -> ())) {
        self.role = role
        self.title = title
        self.systemImage = systemImage
        self.image = image
        self.confirmationMessage = confirmationMessage ?? defaultConfirmationMessage
        self.asyncAction = asyncAction
    }
    
    var body: some View {
        Button(role: role) {
            if role == .destructive {
                askConfirmation = true
            } else if let action {
                action()
            } else if let asyncAction {
                isLoading = true
                Task {
                    await asyncAction()
                    isLoading = false
                }
            }
        } label: {
            HStack {
                if let systemImage {
                    Image(systemName: systemImage)
                        .resizable()
                        .scaledToFit()
                        .frame(height: 24)
                }
                if let image {
                    Image(image)
                        .resizable()
                        .scaledToFit()
                        .frame(width: 32, height: 32)
                }
                Spacer()
                Text(title)
                    .opacity(isLoading ? 0.0 : 1.0)
                    .foregroundColor(.white)
                    .frame(height: 32)
                Spacer()
            }
            .font(.headline)
        }
        .overlay {
            if isLoading {
                ZStack {
                    Color.master
                    ProgressView()
                        .tint(.white)
                }
            }
        }
        .disabled(isLoading)
        .frame(maxWidth: .infinity)
        .buttonStyle(.borderedProminent)
        .tint(role == .destructive ? Color.red : Color.master)
        .listRowBackground(Color.clear)
        .listRowInsets(EdgeInsets(.zero))
        .confirmationDialog("Confirmation",
                            isPresented: $askConfirmation,
                            titleVisibility: .visible) {
            Button("OK") {
                if let action {
                    action()
                } else if let asyncAction {
                    isLoading = true
                    Task {
                        await asyncAction()
                        isLoading = false
                    }
                }
            }
            Button("Annuler", role: .cancel) {}
        } message: {
            Text(confirmationMessage)
        }

    }
}
