
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Com.FirmLib.Bll;
using Com.FirmLib.Entity;
using Com.FirmLib.UI.Common;
using Com.FirmLib.UI.Manufacturer.Model;
using FWindSoft.Data;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;

namespace Com.FirmLib.UI.Manufacturer
{
    /// <summary>
    /// 产品信息
    /// </summary>
    public class VmProductList: BaseViewModel
    {
        public VmProductList()
        {
            Init();
        }

        private void Init()
        {
            Root = new TreeNavigation<ShowDataItem>();
        }
        public void Init(LoadParameter parameter)
        {
            RefVenderShowItem showItem = parameter.Parameter as RefVenderShowItem;
            if (showItem == null)
                return;
            RefId = showItem.RefVenderId;
            ManufacturerId = showItem.ManufacturerId;
            ProductTypes = showItem.ProductTypes ?? new List<string>();
            LoadBrandItems(ManufacturerId,ProductTypes);
        }
        /// <summary>
        /// 生产厂商id
        /// </summary>
        public string ManufacturerId { get; set; }
        /// <summary>
        /// 关联厂商Id
        /// </summary>
        public string RefId { get; set; }

        public List<string> ProductTypes { get; set; }
        #region 属性相关

        private TreeNavigation<ShowDataItem> m_Root;
        /// <summary>
        /// 根元素
        /// </summary>
        public TreeNavigation<ShowDataItem> Root
        {
            get { return this.m_Root; }
            set
            {
                this.m_Root = value;
                RaisePropertyChanged(nameof(this.Root));
            }
        }
        #endregion


        #region 加载数据    
        /// <summary>
        /// 加载品牌信息
        /// </summary>
        /// <param name="manufactrerId">生产厂家Id</param>
        /// <param name="types">生产厂家Id</param>
        private void LoadBrandItems(string manufactrerId,List<string> types)
        {
            Root = new TreeNavigation<ShowDataItem>();
            Root.CurrentItemChanged = ChangeSelectedItem;
            Root.Items.AddRange(GetBrands(manufactrerId,types));
            Root.CurrentItem = Root.Items.FirstOrDefault();
        }
        #endregion


        #region 加载类型相关
        private void ChangeSelectedItem(TreeNavigationItem<ShowDataItem> item)
        {
            if (item == null)
                return;
            if (item.Level > 2)
                return;
            item.Nodes.Clear();
            if (item.Level == 1)
            {
                item.Nodes.AddRange(GetProducts(new ProdcutTypeCondition() { BrandId = item.RefItem.Id}));
            }
            else if (item.Level == 2)
            {
                item.Nodes.AddRange(GetProductTypes(new ProdcutTypeCondition() { ProductId = item.RefItem.Id}));
            }
            item.CurrentItem = item.Nodes.FirstOrDefault();    
        }
        /// <summary>
        /// 当前型号数据
        /// </summary>
        private List<ProductTypeDetailItem> DataSource { get; set; }
        private List<TreeNavigationItem<ShowDataItem>> GetBrands(string manufacturerId,List<string> types)
        {

            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            //厂商关联型号信息
            DataSource = LoadProductTypeItems(RefId,types) ?? new List<ProductTypeDetailItem>();
            DataSource =DataSource .Where(p=>p.VenderId==manufacturerId).ToList();
            var brands = ProductTypeManager.GetBrands(new ProdcutTypeCondition(DataSource));
            brands.ForEach(b => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = b }));
            return results;
        }
        private List<TreeNavigationItem<ShowDataItem>> GetProducts(ProdcutTypeCondition conditon)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            var products = ProductTypeManager.GetProducts(new ProdcutTypeCondition(DataSource) { BrandId =conditon.BrandId});
            products.ForEach(b => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = b }));

            return results;
        }
        private List<TreeNavigationItem<ShowDataItem>> GetProductTypes(ProdcutTypeCondition conditon)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            var productTypes = ProductTypeManager.GetProductTypes(new ProdcutTypeCondition(DataSource) { ProductId = conditon.ProductId, FamilyCode = conditon.FamilyCode });
            #region 转换数据
            foreach (var productType in productTypes)
            {
                var useData = new TreeNavigationItem<ShowDataItem>()
                {
                    RefItem = productType
                };
                results.Add(useData);
            }
            #endregion
            return results;
        }
        #endregion

        /// <summary>
        /// 加载产品信息
        /// </summary>
        /// <param name="venderId"></param>
        /// <param name="types"></param>
        private List<ProductTypeDetailItem> LoadProductTypeItems(string venderId,List<string> types)
        {
            List<ProductTypeDetailItem> results = new List<ProductTypeDetailItem>();
            results = BllFactory<ProductDirectoryBll>.Instance.GetAllItems(venderId).Where(c=>types.Contains(c.ProductTypeId)).ToList();
            return results;
        }
    }
}
