// File: Plugins/YourCompany.ProductAttributes/Components/ProductAttributesViewComponent.cs

using FruitBank.Common.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Plugin.Misc.FruitBankPlugin.Models;
using Nop.Plugin.Misc.FruitBankPlugin.Services;
using Nop.Services.Common;
using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Framework.Components;

namespace Nop.Plugin.Misc.FruitBankPlugin.Components
{
    [ViewComponent(Name = "ProductAttributes")]
    public class ProductAttributesViewComponent : NopViewComponent
    {
        private readonly FruitBankAttributeService _fruitBankAttributeService;
        private readonly IWorkContext _workContext;
        private readonly IStoreContext _storeContext;

        public ProductAttributesViewComponent(FruitBankAttributeService fruitBankAttributeService, IWorkContext workContext, IStoreContext storeContext)
        {
            _workContext = workContext;
            _storeContext = storeContext;
            _fruitBankAttributeService = fruitBankAttributeService;
        }

        public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
        {
            if (additionalData is not ProductModel productModel) return Content("");

            var model = new ProductAttributesModel { ProductId = productModel.Id };

            if (model.ProductId > 0)
            {
                //var measuringAttributeValues = await _fruitBankAttributeService.GetMeasuringAttributeValuesAsync<Product>(model.ProductId);
                //if (measuringAttributeValues != null)
                //{
                //    model.IsMeasurable = measuringAttributeValues.IsMeasurable;
                //    model.NetWeight = measuringAttributeValues.NetWeight;
                //}

                model.IsMeasurable = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, bool>(model.ProductId, nameof(IMeasurable.IsMeasurable));
                model.NetWeight = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, double>(model.ProductId, nameof(IMeasuringNetWeight.NetWeight));

                model.Tare = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, double>(model.ProductId, nameof(ITare.Tare));
                model.IncomingQuantity = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, int>(model.ProductId, nameof(IIncomingQuantity.IncomingQuantity));
                model.AverageWeight = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, double>(model.ProductId, nameof(IProductDto.AverageWeight));
                model.AverageWeightTreshold = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, double>(model.ProductId, nameof(IProductDto.AverageWeightTreshold));
            }

            return View("~/Plugins/Misc.FruitBankPlugin/Views/ProductAttributes.cshtml", model);
        }
    }
}
