@using Java.Sql
@using salesbook.Shared.Components.Layout.Spinner
@using salesbook.Shared.Core.Dto.Activity
@using salesbook.Shared.Core.Entity
@using salesbook.Shared.Core.Interface
@inject IManageDataService ManageDataService
@inject ISnackbar Snackbar
@inject IDialogService Dialog

<div class="row" id="@Notification.Id" @onclick="OpenActivity">
    <div class="behind-left">
        <button class="read-btn">
            <MudIcon Icon="@Icons.Material.Rounded.Check" />
        </button>
    </div>
    <div class="behind-right">
        <button class="trash-btn">
            <MudIcon Icon="@Icons.Material.Rounded.Delete" />
        </button>
    </div>
    <div class="notification-card">
        @if (Notification.NotificationData is { Type: not null })
        {
            @switch (Notification.NotificationData.Type)
            {
                case "memo":
                    <div class="avatar"><MudIcon Icon="@Icons.Material.Rounded.ContentPaste" /></div>
                    break;
                case "newPlanned":
                    <div class="avatar"><MudIcon Icon="@Icons.Material.Rounded.CalendarMonth" /></div>
                    break;
            }
        }
        <div class="notification-body">
            <div class="title-row">
                <div class="title">@Notification.Title</div>

                <div class="section-time">
                    <div class="notification-time">@GetTimeAgo(Notification.StartDate)</div>
                    @if (Unread)
                    {
                        <span class="unread-dot"></span>
                    }
                </div>
            </div>

            @if (
                Notification.StartDate < DateTime.Today && Notification.Body != null && Notification.Body.Contains("Oggi")
            )
            {
                <div class="subtitle">@Notification.Body.Replace("Oggi", $"{Notification.StartDate:d}")</div>
            }
            else
            {
                <div class="subtitle">@Notification.Body</div>
            }
        </div>
    </div>
</div>

<OverlayLayout Visible="VisibleOverlay" />

@code {
    [Parameter] public bool Unread { get; set; }
    [Parameter] public WtbNotification Notification { get; set; } = new();

    private bool VisibleOverlay { get; set; }

    private async Task OpenActivity()
    {
        if(Notification.NotificationData?.ActivityId == null) return;
        var activityId = Notification.NotificationData.ActivityId;

        Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
        Snackbar.Clear();

        VisibleOverlay = true;
        StateHasChanged();

        var activity = (await ManageDataService.GetActivityTryLocalDb(new WhereCondActivity { ActivityId = activityId })).LastOrDefault();

        VisibleOverlay = false;
        StateHasChanged();

        if (activity == null) Snackbar.Add("Impossibile aprire l'attivitą", Severity.Error);

        _ = ModalHelpers.OpenActivityForm(Dialog, activity, null);
    }

    private static string GetTimeAgo(DateTime? timestamp)
    {
        if (timestamp is null) return "";

        var difference = DateTime.Now - timestamp.Value;

        if (DateTime.Now.Day != timestamp.Value.Day)
            return timestamp.Value.ToString("dd/MM/yyyy");

        switch (difference.TotalMinutes)
        {
            case < 1:
                return "Adesso";
            case < 60:
                return $"{(int)difference.TotalMinutes} minuti fa";
            default:
            {
                switch (difference.TotalHours)
                {
                    case < 2:
                        return $"{(int)difference.TotalHours} ora fa";
                    case < 24:
                        return $"{timestamp.Value:t}";
                    default:
                    {
                        return timestamp.Value.ToString("dd/MM/yyyy");
                    }
                }
            }
        }
    }
}