using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using OneOf;
namespace AntDesign
{
///
/// show a simple Confirm dialog like MessageBox of Windows, it's different from ModalService. ModalService can only create OK-Cancel Confirm dialog and return ConfirmRef, but ConfirmService return ConfirmResult
///
public class ConfirmService
{
internal event Func OnOpenEvent;
///
/// show a confirm dialog like MessageBox of Windows
///
/// the content of dialog
/// the title of dialog
/// the buttons of dialog
/// the icon of dialog
/// the configuration options for dialog
///
public async Task Show(
OneOf content,
OneOf title,
ConfirmButtons confirmButtons,
ConfirmIcon confirmIcon,
ConfirmButtonOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
ConfirmOptions confirmOptions = new ConfirmOptions()
{
Content = content,
ConfirmButtons = confirmButtons,
ConfirmIcon = confirmIcon,
};
if (title.IsT0)
{
confirmOptions.Title = title.AsT0;
}
else
{
confirmOptions.TitleTemplate = title.AsT1;
}
#region config button default properties
if (options.Button1Props != null)
{
confirmOptions.Button1Props = options.Button1Props;
}
if (options.Button2Props != null)
{
confirmOptions.Button2Props = options.Button2Props;
}
if (options.Button3Props != null)
{
confirmOptions.Button3Props = options.Button3Props;
}
#endregion
var confirmRef = new ConfirmRef(confirmOptions)
{
TaskCompletionSource = new TaskCompletionSource()
};
if (OnOpenEvent != null)
{
await OnOpenEvent.Invoke(confirmRef);
}
return await confirmRef.TaskCompletionSource.Task;
}
///
/// show a confirm dialog like MessageBox of Windows
///
/// the content of dialog
/// the title of dialog
/// the buttons of dialog
/// the icon of dialog
///
public Task Show
(OneOf content,
OneOf title,
ConfirmButtons confirmButtons = ConfirmButtons.OKCancel,
ConfirmIcon confirmIcon = ConfirmIcon.Info)
{
return Show(content, title, confirmButtons, confirmIcon, new ConfirmButtonOptions());
}
}
}