@using hassio_onedrive_backup.Graph;
@inherits LayoutComponentBase
@inject NavigationManager NavigationManager
@inject IGraphHelper GraphHelper
@inject IJSRuntime JS

<div class="container-fluid p-1 pb-5">
    @if (GraphHelper.IsAuthenticated == false)
    {
        <div class="alert alert-danger" role="alert" style="padding:1px;margin-bottom:1px;">
            <strong>Not logged in!</strong> To sign in to OneDrive, navigate to <a href="@GraphHelper.AuthUrl" class="alert-link" target="_blank">@GraphHelper.AuthUrl</a> and enter the code <i>
                @GraphHelper.AuthCode
            </i>
            <button type="button" class="btn btn-sm m-1 btn-light" @onclick="CopyCodeToClipboard"><span class="oi oi-clipboard" title="Copy code to clipboard"></span></button> to authenticate.
        </div>
    }

    <nav class="navbar navbar-expand-lg">
        <span class="navbar-brand" href="#">OneDrive Backup</span>
        <div class="nav-item">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                Dashboard
            </NavLink>
        </div>
        <div class="nav-item">
            <NavLink class="nav-link" href="about" Match="NavLinkMatch.All">
                About
            </NavLink>
        </div>
    </nav>
</div>

<div class="container" style="padding-bottom: 2rem">
    @Body
</div>

<footer>
    <a href="about"><img class="mx-1" src="images/Bitcoin.svg" alt="Donate Bitcoin" data-bs-toggle="tooltip" data-bs-title="Donate Bitcoin" style="width: 50px; height: auto; transform: scale(0.5);" /></a>
    <a href="https://www.buymeacoffee.com/snirlavis" target="_blank"><img class="mx-1" src="bmc/bmc-button.svg" alt="Buy Me A Coffee" data-bs-toggle="tooltip" data-bs-title="Buy me a coffee" style="height: 1.8rem !important;"></a>
    <p>&copy; Nir Lavi</p>
</footer>

@code{
    protected override void OnInitialized()
    {
        base.OnInitialized();
        GraphHelper.AuthStatusChangedEventHandler += OnStateChanged;
    }

    private void CopyCodeToClipboard()
    {
        JS.InvokeVoidAsync("copyToClipboard", GraphHelper.AuthCode);
    }

    private async void OnStateChanged()
    {
        await InvokeAsync(() =>
        {
            StateHasChanged();
        });
    }
}
