@page "/"
@attribute [Authorize]
@using salesbook.Shared.Core.Interface
@using salesbook.Shared.Components.Layout.Spinner
@inject IFormFactor FormFactor
@inject INetworkService NetworkService
@inject IFirebaseNotificationService FirebaseNotificationService

<SpinnerLayout FullScreen="true" />

@code
{
    protected override async Task OnInitializedAsync()
    {
        try
        {
            await FirebaseNotificationService.InitFirebase();
        }
        catch (Exception e)
        {
            Console.WriteLine($"Firebase init: {e.Message}");
        }

        var lastSyncDate = LocalStorage.Get<DateTime>("last-sync");

        if (!FormFactor.IsWeb() && NetworkService.IsNetworkAvailable() && lastSyncDate.Equals(DateTime.MinValue))
        {
            NavigationManager.NavigateTo("/sync");
            return;
        }

        NavigationManager.NavigateTo("/Calendar");
    }
}
