@page "/commessa/{CodJcom}"
@page "/commessa/{CodJcom}/{RagSoc}"
@attribute [Authorize]
@using AutoMapper
@using salesbook.Shared.Components.Layout
@using salesbook.Shared.Components.Layout.Spinner
@using salesbook.Shared.Components.SingleElements
@using salesbook.Shared.Core.Dto
@using salesbook.Shared.Core.Dto.Activity
@using salesbook.Shared.Core.Dto.JobProgress
@using salesbook.Shared.Core.Dto.PageState
@using salesbook.Shared.Core.Entity
@using salesbook.Shared.Core.Interface
@using salesbook.Shared.Core.Interface.IntegryApi
@inject JobSteps JobSteps
@inject IManageDataService ManageData
@inject IIntegryApiService IntegryApiService
@inject IMapper Mapper

<HeaderLayout Title="@CodJcom" ShowProfile="false" Back="true" BackTo="Indietro"/>

@if (IsLoading)
{
    <SpinnerLayout FullScreen="true"/>
}
else
{
    <div class="container content pb-safe-area">
        @if (CommessaModel == null)
        {
            <NoDataAvailable Text="Nessuna commessa trovata"/>
        }
        else
        {
            <input type="radio" class="tab-toggle" name="tab-toggle" id="tab1" checked>
            <input type="radio" class="tab-toggle" name="tab-toggle" id="tab2">
            <input type="radio" class="tab-toggle" name="tab-toggle" id="tab3">

            <div class="box">
                <ul class="tab-list">
                    <li class="tab-item"><label class="tab-trigger" for="tab1">Avanzamento</label></li>
                    <li class="tab-item"><label class="tab-trigger" for="tab2">Attività</label></li>
                    <li class="tab-item"><label class="tab-trigger" for="tab3">Allegati</label></li>
                </ul>
            </div>

            <!-- contenuti -->
            <div class="tab-container">
                <div class="tab-content">
                    @if (Steps != null)
                    {
                        <div class="timeline-container">
                            <div class="timeline">
                                @foreach (var step in Steps)
                                {
                                    <div class="step">
                                        <div class="@(step.Status!.Skip ? "circle skipped" : step.Status!.Progress ? "in-progress" : "circle completed")"></div>
                                        <div class="label">
                                            <div class="titleStep">@step.StepName</div>
                                            @if (step.Date is not null)
                                            {
                                                <div class="subtitleStep">@($"{step.Date.Value:D}") @(step.Status!.Progress ? "(In corso...)" : "")</div>
                                            }
                                        </div>
                                    </div>
                                }
                            </div>
                        </div>
                    }
                </div>
                <div class="tab-content">
                    @if (ActivityIsLoading)
                    {
                        <MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="my-7" />
                    }
                    else
                    {
                        @if (ActivityList is { Count: > 0 })
                        {
                            <div class="contentFlex">
                                <Virtualize Items="ActivityList" Context="activity">
                                    <ActivityCard ShowDate="true" Activity="activity" />
                                </Virtualize>
                            </div>
                        }
                        else
                        {
                            <NoDataAvailable Text="Nessuna attività trovata" />
                        }
                    }
                </div>
                <div class="tab-content">
                    @if (AttachedIsLoading)
                    {
                        <MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="my-7" />
                    }
                    else
                    {
                        @if (ListAttached != null)
                        {
                            <div class="contentFlex">
                                <Virtualize Items="ListAttached" Context="attached">
                                    <AttachCard Attached="attached" />
                                </Virtualize>
                            </div>
                        }
                        else
                        {
                            <NoDataAvailable Text="Nessun allegato presente" />
                        }
                    }
                </div>
            </div>
        }
    </div>
}

@code {
    [Parameter] public string CodJcom { get; set; } = "";
    [Parameter] public string RagSoc { get; set; } = "";

    private List<CRMJobStepDTO>? Steps { get; set; }
    private List<ActivityDTO> ActivityList { get; set; } = [];
    private List<CRMAttachedResponseDTO>? ListAttached { get; set; }
    private JtbComt? CommessaModel { get; set; }

    private bool IsLoading { get; set; } = true;
    private bool ActivityIsLoading { get; set; } = true;
    private bool AttachedIsLoading { get; set; } = true;

    protected override async Task OnInitializedAsync()
    {
        await LoadData();
    }

    private async Task LoadData()
    {
        CommessaModel = (await ManageData.GetTable<JtbComt>(x => x.CodJcom.Equals(CodJcom))).LastOrDefault();
        Steps = JobSteps.Steps;

        _ = LoadActivity();
        _ = LoadAttached();

        IsLoading = false;
    }

    private async Task LoadActivity()
    {
        await Task.Run(async () =>
        {
            var activities = await IntegryApiService.RetrieveActivity(new CRMRetrieveActivityRequestDTO { CodJcom = CodJcom });
            ActivityList = (await ManageData.MapActivity(activities)).OrderByDescending(x =>
                (x.EffectiveTime ?? x.EstimatedTime) ?? x.DataInsAct
            ).ToList();
        });

        ActivityIsLoading = false;
        StateHasChanged();
    }

    private async Task LoadAttached()
    {
        await Task.Run(async () =>
        {
            ListAttached = await IntegryApiService.RetrieveAttached(CodJcom);
        });

        AttachedIsLoading = false;
        StateHasChanged();
    }

}

