using FNZCM.BlazorWasm.Helpers;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace FNZCM.BlazorWasm.UI.Components
{
    public class FnzComponentBase : ComponentBase, IAsyncDisposable
    {
        private readonly Lazy<DotNetObjectReference<FnzComponentBase>> lazy;
        public FnzComponentBase() => lazy = new Lazy<DotNetObjectReference<FnzComponentBase>>(() => DotNetObjectReference.Create(this));
        protected DotNetObjectReference<FnzComponentBase> DotNetRef => lazy.Value;

        [Inject] protected IJSRuntime JSRuntime { get; set; }
        [Inject] protected ApiClient ApiClient { get; set; }
        [Inject] protected FnzDataSetSingleto DataSet { get; set; }
        [Inject] protected LocalStorageHelper LocalStorage { get; set; }
        [Inject] protected PlaylistHelper PlaylistHelper { get; set; }
        [Inject] protected NavigationManager NavigationManager { get; set; }

        [Parameter(CaptureUnmatchedValues = true)]
        public Dictionary<string, object> InputAttributes { get; set; }

        public ElementReference Element { get; protected set; }

        protected virtual string Prefix => "fnz";

        public Guid InstanceId { get; } = Guid.NewGuid();
        public string ElementId => Prefix.Replace(".", "-") + "-" + InstanceId.ToString("n");

        public virtual async ValueTask DisposeAsync()
        {
            if (lazy.IsValueCreated) DotNetRef.Dispose();
        }
    }
}
