@using salesbook.Shared.Core.Dto
@using salesbook.Shared.Components.Layout
@using salesbook.Shared.Core.Interface
@using salesbook.Shared.Components.Layout.Overlay
@inject IAttachedService AttachedService

<MudDialog Class="customDialog-form disable-safe-area">
    <DialogContent>
        <HeaderLayout ShowProfile="false" SmallHeader="true" Cancel="true" OnCancel="() => MudDialog.Cancel()" Title="@TitleModal"/>

        @if (RequireNewName)
        {
            <MudTextField @bind-Value="NewName" Class="px-3" Variant="Variant.Outlined"/>            
        }
        else
        {
            @if (!SelectTypePicture)
            {
                <div style="margin-bottom: 1rem;" class="content attached">
                    <MudFab Size="Size.Small" Color="Color.Primary"
                            StartIcon="@Icons.Material.Rounded.Image"
                            Label="Immagini" OnClick="OnImage"/>

                    <MudFab Size="Size.Small" Color="Color.Primary"
                            StartIcon="@Icons.Material.Rounded.InsertDriveFile"
                            Label="File" OnClick="OnFile"/>

                    @if (CanAddPosition)
                    {
                        <MudFab Size="Size.Small" Color="Color.Primary"
                                StartIcon="@Icons.Material.Rounded.AddLocationAlt"
                                Label="Posizione" OnClick="OnPosition"/>
                    }
                </div>
            }
            else
            {
                <div style="margin-bottom: 1rem;" class="content attached">
                    <MudFab Size="Size.Small" Color="Color.Primary"
                            StartIcon="@Icons.Material.Rounded.CameraAlt"
                            Label="Camera" OnClick="OnCamera"/>

                    <MudFab Size="Size.Small" Color="Color.Primary"
                            StartIcon="@Icons.Material.Rounded.Image"
                            Label="Galleria" OnClick="OnGallery"/>
                </div>
            }
        }
    </DialogContent>
    <DialogActions>
        @if (RequireNewName)
        {
            <MudButton Disabled="NewName.IsNullOrEmpty()" Class="my-3" Size="Size.Small" Variant="Variant.Filled" Color="Color.Primary"
                       StartIcon="@Icons.Material.Rounded.Check" OnClick="OnNewName">
                Salva
            </MudButton>
        }
    </DialogActions>
</MudDialog>

<SaveOverlay VisibleOverlay="VisibleOverlay" SuccessAnimation="SuccessAnimation"/>

@code {
    [CascadingParameter] private IMudDialogInstance MudDialog { get; set; }

    [Parameter] public bool CanAddPosition { get; set; }

    //Overlay for save
    private bool VisibleOverlay { get; set; }
    private bool SuccessAnimation { get; set; }

    private AttachedDTO? Attached { get; set; }

    private bool _requireNewName;
    private bool RequireNewName
    {
        get => _requireNewName;
        set
        {
            _requireNewName = value;
            TitleModal = _requireNewName ? "Nome allegato" : "Aggiungi allegati";
            StateHasChanged();
        }
    }
    private bool SelectTypePicture { get; set; }

    private string TitleModal { get; set; } = "Aggiungi allegati";

    private string? _newName;
    private string? NewName
    {
        get => _newName;
        set
        {
            _newName = value;
            StateHasChanged();
        }
    }

    protected override async Task OnInitializedAsync()
    {
        SelectTypePicture = false;
        RequireNewName = false;
        Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
    }

    private async Task OnImage()
    {
        SelectTypePicture = true;
        StateHasChanged();
    }

    private async Task OnCamera()
    {
        Attached = await AttachedService.SelectImageFromCamera();

        if (Attached != null)
        {
            RequireNewName = true;
            StateHasChanged();
        }
    }

    private async Task OnGallery()
    {
        Attached = await AttachedService.SelectImageFromGallery();

        if (Attached != null)
        {
            RequireNewName = true;
            StateHasChanged();
        }
    }

    private async Task OnFile()
    {
        Attached = await AttachedService.SelectFile();
        MudDialog.Close(Attached);
    }

    private async Task OnPosition()
    {
        Attached = await AttachedService.SelectPosition();

        if (Attached != null)
        {
            RequireNewName = true;
            StateHasChanged();
        }
    }

    private void OnNewName()
    {
        if (Attached != null)
        {
            switch (Attached.Type)
            {
                case AttachedDTO.TypeAttached.Position:
                {
                    CanAddPosition = false;

                    Attached.Description = NewName!;
                    Attached.Name = NewName!;

                    break;
                }
                case AttachedDTO.TypeAttached.Image:
                {
                    var extension = Path.GetExtension(Attached.Name);
                    Attached.Name = NewName! + extension;

                    break;
                }
                case AttachedDTO.TypeAttached.Document:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        MudDialog.Close(Attached);
    }

}