using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Configuration;
using MudBlazor;

namespace BackOffice.Common.BaseComponents
{
    
    public partial class Image
    {
        private string src = string.Empty;
        [Parameter] public EventCallback OnClick { get; set; }
        [Inject] private IConfiguration Configuration { get; set; } = default!;
        
        private string FileBaseUrl => Configuration["GwUrl"]?.TrimEnd('/') ?? "";
        
        protected override async Task OnParametersSetAsync()
        {
            await base.OnParametersSetAsync();

            if (!string.IsNullOrWhiteSpace(Src))
            {
                if (Src.Contains("data:") || Src.StartsWith("http"))
                    src = Src;
                else
                    src = FileBaseUrl + "/uploads/" + Src.TrimStart('/');
            }
            else
            {
                src = string.Empty;
            }

        }
        //protected override async Task OnAfterRenderAsync(bool firstRender)
        //{
        //    await base.OnAfterRenderAsync(firstRender);
        //    if (firstRender)
        //    {
        //        if (!string.IsNullOrWhiteSpace(Src))
        //        {
        //            src = await FileManagement.GetFileBase64(Src);
        //            StateHasChanged();
        //        }
        //    }
        //}
    }
}