using CommunityToolkit.Mvvm.ComponentModel;
using Prism.Events;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using SlnMesnac.WPF.Attribute;
using SlnMesnac.WPF.Event;
using SlnMesnac.WPF.ViewModel.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SlnMesnac.WPF.ViewModel.ProductDetails
{
    [RegisterAsSingletonAttribute]
    public partial class ProductDetailsViewModel : BaseViewModel
    {
        private readonly Ibase_product_infoServices _product_infoServices;

        private readonly Ibase_product_featureServices _product_featureServices;

        private readonly Ibase_product_describeServices _product_describeServices;

        private readonly Ibase_product_paramServices _product_paramServices;

        private readonly IEventAggregator _eventAggregator;


        [ObservableProperty]
        public ObservableCollection<base_product_feature> productFeature = new ObservableCollection<base_product_feature>();

        [ObservableProperty]
        public ObservableCollection<base_product_describe> productDescribe = new ObservableCollection<base_product_describe>();

        [ObservableProperty]
        public ObservableCollection<base_product_param> productParams = new ObservableCollection<base_product_param>();

        [ObservableProperty]
        public base_product_info productInfo = new base_product_info();

        public ProductDetailsViewModel(Ibase_product_infoServices product_infoServices, Ibase_product_featureServices product_featureServices, Ibase_product_describeServices product_describeServices,
            Ibase_product_paramServices product_paramServices, IEventAggregator eventAggregator)
        {
            _product_featureServices = product_featureServices;
            _product_describeServices = product_describeServices;
            _product_paramServices = product_paramServices;
            _product_infoServices = product_infoServices;

            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<ProductCodeEvent>().Subscribe(OnProductSelected);

        }

        private void OnProductSelected(string productCode)
        {
            Init(productCode);
        }

        private void Init(string productCode)
        {
            ProductFeature.Clear();
            ProductDescribe.Clear();
            ProductParams.Clear();

            ProductInfo = _product_infoServices.Query(x => x.productCode == productCode).First();
            
            var productFeatureInfo = _product_featureServices.Query(x => x.productCode == productCode);

            if (productFeatureInfo.Count == 0)
            {
                for (int i = 0; i < 2; i++)
                {
                    ProductFeature.Add(new base_product_feature()
                    {
                        productFeature = string.Empty
                    });
                }
            }
            else
            {
                ProductFeature = new ObservableCollection<base_product_feature>(productFeatureInfo);
            }

            var productDescribeInfo = _product_describeServices.Query(x => x.productCode == productCode);

            if (productDescribeInfo.Count == 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    ProductDescribe.Add(new base_product_describe()
                    {
                        describeTitle = string.Empty,
                        describeContent = string.Empty
                    });
                }
            }
            else
            {
                ProductDescribe = new ObservableCollection<base_product_describe>(productDescribeInfo);
            }

            var productParamsInfo = _product_paramServices.Query(x => x.productCode == productCode);

            if (productParamsInfo.Count == 0)
            {
                for (int i = 0; i < 19; i++)
                {
                    ProductParams.Add(new base_product_param()
                    {
                        paramContent = string.Empty,
                    });
                }
            }
            else
            {
                ProductParams = new ObservableCollection<base_product_param>(productParamsInfo);
            }
        }
    }
}
