using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using OneOf;
namespace AntBlazor
{
///
/// Message Service
///
public class MessageService
{
internal event Action OnConfig;
internal event Func OnOpening;
internal event Action OnDestroy;
#region API
public Task Open([NotNull]MessageConfig config)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
var task = OnOpening?.Invoke(config);
return task;
}
public Task Success(OneOf content, double? duration = null, Action onClose = null)
{
return PreOpen(MessageType.Success, content, duration, onClose);
}
public Task Error(OneOf content, double? duration = null, Action onClose = null)
{
return PreOpen(MessageType.Error, content, duration, onClose);
}
public Task Info(OneOf content, double? duration = null, Action onClose = null)
{
var task = PreOpen(MessageType.Info, content, duration, onClose);
return task;
}
public Task Warning(OneOf content, double? duration = null, Action onClose = null)
{
return PreOpen(MessageType.Warning, content, duration, onClose); ;
}
public Task Warn(OneOf content, double? duration = null, Action onClose = null)
{
return Warning(content, duration, onClose);
}
public Task Loading(OneOf content, double? duration = null, Action onClose = null)
{
return PreOpen(MessageType.Loading, content, duration, onClose);
}
private Task PreOpen(MessageType type, OneOf content, double? duration = null, Action onClose = null)
{
MessageConfig config;
if (content.IsT2)
{
config = content.AsT2;
}
else
{
config = new MessageConfig() { };
OneOf configContent;
if (content.IsT1)
{
configContent = content.AsT1;
}
else
{
configContent = content.AsT0;
}
config.Content = configContent;
config.Duration = duration;
if (onClose != null)
{
config.OnClose += onClose;
}
}
config.Type = type;
return Open(config);
}
#endregion
public void Config(MessageGlobalConfig config)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
OnConfig?.Invoke(config);
}
public void Destroy()
{
OnDestroy?.Invoke();
}
}
}