@using salesbook.Shared.Core.Dto
@using salesbook.Shared.Core.Entity
@using salesbook.Shared.Core.Helpers.Enum
@inject IDialogService Dialog

<div class="activity-card @Activity.Category.ConvertToHumanReadable()" @onclick="OpenActivity">
    <div class="activity-left-section">
        <div class="activity-body-section">
            <div class="title-section">
                <MudText Class="activity-title" Typo="Typo.body1" HtmlTag="h3">
                    @switch (Activity.Category)
                    {
                        case ActivityCategoryEnum.Commessa:
                            @Activity.Commessa
                            break;
                        case ActivityCategoryEnum.Interna:
                            @Activity.Cliente
                            break;
                        case ActivityCategoryEnum.Memo:
                            @Activity.ActivityDescription
                            break;
                        default:
                            @("")
                            break;
                    }
                </MudText>
                <div class="activity-hours-section">
                    <span class="activity-hours">
                        @if (Activity.EffectiveTime is null)
                        {
                            @($"{Activity.EstimatedTime:t}")
                        }
                        else
                        {
                            @($"{Activity.EffectiveTime:t}")
                        }
                    </span>
                </div>
            </div>
            @if (Activity.Category != ActivityCategoryEnum.Memo)
            {
                <MudText Class="activity-subtitle" Typo="Typo.body1" HtmlTag="p">@Activity.ActivityDescription</MudText>
            }
        </div>
    </div>

    <div class="activity-info-section">
        @if (Durata != null && (Durata.Value.TotalHours > 0 || Durata.Value.Minutes > 0))
        {
            var ore = (int)Durata.Value.TotalHours;
            var minuti = Durata.Value.Minutes;

            <MudChip T="string" Icon="@IconConstants.Chip.Time" Color="Color.Dark" Size="Size.Small">
                @(ore > 0 ? $"{ore}h{(minuti > 0 ? $" {minuti}m" : "")}" : $"{minuti}m")
            </MudChip>
        }
        @if (Activity.ActivityResultId != null)
        {
            <MudChip T="string" Icon="@IconConstants.Chip.Stato" Size="Size.Small" Color="Color.Success">@Activity.ActivityResultId</MudChip>
        }
        <MudChip T="string" Icon="@IconConstants.Chip.User" Size="Size.Small">@Activity.UserName</MudChip>
    </div>
</div>

@code {
    [Parameter] public ActivityDTO Activity { get; set; } = new();
    [Parameter] public EventCallback<string> ActivityChanged { get; set; }
    [Parameter] public EventCallback<ActivityDTO> ActivityDeleted { get; set; }

    private TimeSpan? Durata { get; set; }

    protected override void OnParametersSet()
    {
        Durata = Activity switch
        {
            { EffectiveTime: not null, EffectiveEndtime: not null } => Activity.EffectiveEndtime.Value - Activity.EffectiveTime.Value,
            { EstimatedTime: not null, EstimatedEndtime: not null } => Activity.EstimatedEndtime.Value - Activity.EstimatedTime.Value,
            _ => null
        };
    }

    private async Task OpenActivity()
    {
        var result = await ModalHelpers.OpenActivityForm(Dialog, null, Activity.ActivityId);

        switch (result)
        {
            case { Canceled: false, Data: not null } when result.Data.GetType() == typeof(StbActivity):
                await ActivityChanged.InvokeAsync(((StbActivity)result.Data).ActivityId);
                break;
            case { Canceled: false, Data: not null } when result.Data.GetType() == typeof(ActivityDTO):
                await ActivityDeleted.InvokeAsync((ActivityDTO)result.Data);
                break;
        }
    }
}