@using MudBlazor
@implements IDisposable

<MudDialog Class="image-preview-dlg" DefaultFocus="DefaultFocus.None">
    <DialogContent>
        @if (ViewModel != null)
        {
            <div class="imgpreview-label">@ViewModel.ImageName</div>
            <div class="imgpreview-toolbar">
                <div class="imgpreview-toolbar-left">
                    @if (ViewModel.IsPreviousImageEnabled)
                    {
                        <MudButton Variant="Variant.Outlined" Color="Color.Primary" Class="imgpreview-btn"
                                   OnClick="OnPrevious">Previous image</MudButton>
                    }
                    <MudButton Variant="Variant.Outlined" Color="Color.Primary" Class="imgpreview-btn"
                               OnClick="OnSwitchView">@(ViewModel.OriginalView ? "Show Analysis" : "Show Original")</MudButton>
                </div>
                <div class="imgpreview-toolbar-right">
                    @if (ViewModel.LearnButtonVisible)
                    {
                        <MudButton Variant="Variant.Outlined" Color="Color.Primary" Class="imgpreview-btn"
                                   OnClick="OnLearn">Learn this</MudButton>
                    }
                    @if (ViewModel.IsNextImageEnabled)
                    {
                        <MudButton Variant="Variant.Outlined" Color="Color.Primary" Class="imgpreview-btn"
                                   OnClick="OnNext">Next image</MudButton>
                    }
                    <MudButton Variant="Variant.Filled" Color="Color.Primary" Class="imgpreview-btn"
                               OnClick="Close">Close</MudButton>
                </div>
            </div>
            <div class="imgpreview-image-area">
                @{
                    var imageUri = ViewModel.ImageAnalysis?.ToBase64DataUri();
                }
                @if (!string.IsNullOrEmpty(imageUri))
                {
                    <img src="@imageUri" alt="@ViewModel.ImageName" class="imgpreview-image" />
                }
            </div>
        }
    </DialogContent>
</MudDialog>

@code {
    [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = null!;
    [Parameter] public ErrorPreviewVM? ViewModel { get; set; }

    private ErrorPreviewVM? _subscribedVm;
    private bool _disposed;

    protected override void OnParametersSet()
    {
        if (_subscribedVm != ViewModel)
        {
            if (_subscribedVm != null)
                _subscribedVm.PropertyChanged -= OnPropertyChanged;
            _subscribedVm = ViewModel;
            if (ViewModel != null)
                ViewModel.PropertyChanged += OnPropertyChanged;
        }
    }

    private async void OnPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (_disposed) return;
        await InvokeAsync(StateHasChanged);
    }

    private void OnPrevious() => ViewModel?.PreviousImageCommand.Execute(null);
    private void OnNext() => ViewModel?.NextImageCommand.Execute(null);
    private void OnSwitchView() => ViewModel?.SwitchViewCommand.Execute(null);
    private async Task OnLearn()
    {
        if (ViewModel != null)
            await ViewModel.Learn();
    }

    private void Close() => MudDialog.Close();

    public void Dispose()
    {
        _disposed = true;
        if (_subscribedVm != null)
            _subscribedVm.PropertyChanged -= OnPropertyChanged;
    }
}
