@namespace Oqtane.Modules.Admin.UserProfile
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IUserService UserService
@inject INotificationService NotificationService

@if (PageState.User != null)
{
    <table class="table table-borderless">
        <tr>
            <td>
                <Label For="to" HelpText="Enter the username you wish to send a message to">To: </Label>
            </td>
            <td>
                <input id="to" class="form-control" @bind="@username" />
            </td>
        </tr>
        <tr>
            <td>
                <Label For="subject" HelpText="Enter the subject of the message">Subject: </Label>
            </td>
            <td>
                <input id="subject" class="form-control" @bind="@subject" />
            </td>
        </tr>
        <tr>
            <td>
                <Label For="message" HelpText="Enter the message">Message: </Label>
            </td>
            <td>
                <textarea id="message" class="form-control" @bind="@body" rows="5" />
            </td>
        </tr>
    </table>
    <button type="button" class="btn btn-primary" @onclick="Send">Send</button>
    <NavLink class="btn btn-secondary" href="@NavigateUrl()">Cancel</NavLink>
}

@code {
    private string username = "";
    private string subject = "";
    private string body = "";

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.View;

    public override string Title => "Send Notification";

    private async Task Send()
    {
        var notification = new Notification();
        try
        {
            var user = await UserService.GetUserAsync(username, PageState.Site.SiteId);
            if (user != null)
            {
                notification.SiteId = PageState.Site.SiteId;
                notification.FromUserId = PageState.User.UserId;
                notification.FromDisplayName = PageState.User.DisplayName;
                notification.FromEmail = PageState.User.Email;
                notification.ToUserId = user.UserId;
                notification.ToDisplayName = user.DisplayName;
                notification.ToEmail = user.Email;
                notification.Subject = subject;
                notification.Body = body;
                notification.ParentId = null;
                notification.CreatedOn = DateTime.UtcNow;
                notification.IsDelivered = false;
                notification.DeliveredOn = null;
                notification.SendOn = DateTime.UtcNow;
                notification = await NotificationService.AddNotificationAsync(notification);
                await logger.LogInformation("Notification Created {Notification}", notification);
                NavigationManager.NavigateTo(NavigateUrl());
            }
            else
            {
                AddModuleMessage("User Does Not Exist. Please Verify That The Username Provided Is Correct.", MessageType.Warning);
            }
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Adding Notification {Notification} {Error}", notification, ex.Message);
            AddModuleMessage("Error Adding Notification", MessageType.Error);
        }
    }

}
