@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@inject NavigationManager NavigationManager

@if (!string.IsNullOrEmpty(Message))
{
    <div class="@_classname alert-dismissible fade show mb-3" role="alert">
        @((MarkupString)Message)
        @if (PageState != null)
        {
            @if (Type == MessageType.Error && UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
            {
                <NavLink class="ms-2" href="@NavigateUrl("admin/log")">View Details</NavLink>
            }
            <form method="post" @onsubmit="DismissModal" @formname="@_formname" data-enhance>
                <input type="hidden" name="@Constants.RequestVerificationToken" value="@SiteState.AntiForgeryToken" />
                <button type="submit" class="btn-close" aria-label="Close"></button>
            </form>
        }
    </div>
}

@code {
    private string _classname = string.Empty;
    private string _formname = "ModuleMessageForm";

    [Parameter]
    public string Message { get; set; }

    [Parameter]
    public MessageType Type { get; set; }

    public void RefreshMessage(string message, MessageType type)
    {
        Message = message;
        Type = type;

        UpdateClassName();

        StateHasChanged();
    }

    protected override void OnInitialized()
    {
        if (ModuleState != null)
        {
            _formname += ModuleState.PageModuleId.ToString();
        }
    }

    protected override void OnParametersSet()
    {
        UpdateClassName();
    }

    private void UpdateClassName()
    {
        if (!string.IsNullOrEmpty(Message))
        {
            _classname = GetMessageType(Type);
        }
    }

    private string GetMessageType(MessageType type)
    {
        string classname = string.Empty;
        switch (type)
        {
            case MessageType.Success:
                classname = "alert alert-success";
                break;
            case MessageType.Info:
                classname = "alert alert-info";
                break;
            case MessageType.Warning:
                classname = "alert alert-warning";
                break;
            case MessageType.Error:
                classname = "alert alert-danger";
                break;
        }

        return classname;
    }

    private void DismissModal()
    {
        Message = "";
    }
}
