@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 (Type == MessageType.Error && PageState != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
        {
            <NavLink class="ms-2" href="@NavigateUrl("admin/log")">View Details</NavLink>
        }
        @if (ModuleState != null)
        {
            @if (ModuleState.RenderMode == RenderModes.Static)
            {
                <a href="@NavigationManager.Uri" class="btn-close" data-dismiss="alert" aria-label="close"></a>
            }
            else
            {
                <button type="button" class="btn-close" data-dismiss="alert" aria-label="close" @onclick="CloseMessage"></button>
            }
        }
    </div>
}

@code {
    private string _message = string.Empty;
    private string _classname = string.Empty;

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

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

    [Parameter]
    public RenderModeBoundary Parent { get; set; }

    protected override void OnParametersSet()
    {
        _message = Message;
        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 CloseMessage(MouseEventArgs e)
    {
        if(Parent != null)
        {
            Parent.DismissMessage();
        }
        else
        {
            NavigationManager.NavigateTo(NavigationManager.Uri);
        }
    }
}
