using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
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;
using static MaterialDesignThemes.Wpf.Theme.ToolBar;

namespace SlnMesnac.WPF.ViewModel.ProductDetails
{
    [RegisterAsSingletonAttribute]
    public partial class ProductDetailsEditViewModel : ObservableObject
    {

        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();

        [ObservableProperty]
        public string productImage = string.Empty;

        [ObservableProperty]
        public string featureImage = string.Empty;

        [ObservableProperty]
        public string analyzeImage = string.Empty;

        [ObservableProperty]
        public string sizeLeftImage = string.Empty;

        [ObservableProperty]
        public string sizeRightImage = string.Empty;


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

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

        }

        private void OnProductSelected(base_product_info product)
        {
            ProductInfo = _product_infoServices.Query(x => x.productCode == product.productCode).First(); ;

            Init();
        }

        private void Init()
        {
            ProductFeature.Clear();
            ProductDescribe.Clear();
            ProductParams.Clear();
            var info = ProductInfo;

            if (info != null)
            {
                ProductImage = info.productImage;
                FeatureImage = info.featureImage;
                AnalyzeImage = info.analyzeImage;
                SizeLeftImage = info.sizeLeftImage;
                SizeRightImage = info.sizeRightImage;

                var productFeatureInfo = _product_featureServices.Query(x => x.productCode == info.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 == info.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 == info.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);
                }
            }
        }

        [RelayCommand]
        private void SavePrdouctInfo()
        {
            var info = ProductInfo;

            if(info != null)
            {
                info.productImage = productImage;
                info.featureImage = featureImage;
                info.analyzeImage = analyzeImage;
                info.sizeLeftImage = sizeLeftImage;
                info.sizeRightImage = sizeRightImage;
                _product_infoServices.Update(info);

                var productFeatureInfo = ProductFeature.ToList();
                //productFeatureInfo.ToList().ForEach(x => x.productCode = info.productCode);

                foreach(var item in productFeatureInfo)
                {
                    item.productCode = info.productCode;
                    if (item.objid != 0)
                    {
                        _product_featureServices.Update(item);
                    }
                    else
                    {
                        _product_featureServices.Insert(item);
                    }
                }

                var productDescribeInfo = ProductDescribe.ToList();
                //productDescribeInfo.ToList().ForEach(x => x.productCode = info.productCode);
                foreach (var item in productDescribeInfo)
                {
                    item.productCode = info.productCode;
                    if (item.objid != 0)
                    {
                        _product_describeServices.Update(item);
                    }
                    else
                    {
                        _product_describeServices.Insert(item);
                    }
                }

                var productParamsInfo = ProductParams.ToList();
                //productParamsInfo.ToList().ForEach(x => x.productCode = info.productCode);
                foreach (var item in productParamsInfo)
                {
                    item.productCode = info.productCode;
                    if (item.objid != 0)
                    {
                        _product_paramServices.Update(item);
                    }
                    else
                    {
                        _product_paramServices.Insert(item);
                    }
                }
            }



        }
    }
}
