
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmAddProductType.cs
//文件描述: VmAddProductType.cs
//创 建 者: xls
//创建日期: 2018/9/18 16:59:10
//版 本 号：1.0.0.0
////////////////////////////////////////////////////////////////////////////////

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 FWindSoft.Data;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;

namespace Com.FirmLib.UI.Maintainer
{
    public class VmAddProductType : BaseViewModel
    {
        public VmAddProductType()
        {
            Init();
        }

        private void Init()
        {
            ExistProductTypeIds = new List<string>();
        }
        public void Init(LoadParameter parameter)
        {
            Id = parameter.Parameter as string;
            LoadExistTypes(Id);
            LoadProductTypeItems();
        }
        /// <summary>
        /// 关联厂商Id[可能添加功能需要]
        /// </summary>
        public string Id { get; set; }
        #region 属性相关
        /// <summary>
        /// 存在的产品型号Id集合
        /// </summary>
        public List<string> ExistProductTypeIds { get; set; }

        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));
            }
        }
        /// <summary>
        /// 当前选中设备族
        /// </summary>
        public string CurrentFamily
        {
            get
            {
                if (Root.CurrentItem == null)
                    return string.Empty;
                ShowDataItem item = Root.CurrentItem.RefItem as ShowDataItem;
                if (item != null)
                    return item.Id;
                return string.Empty;
            }
        }
        #endregion

        #region 命令相关

        /// <summary>
        /// 增加维护类型
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void AddProductTypeCommand(object parameter)
        {
            AddTypeList(parameter as Window);
        }
        public bool CanAddProductTypeCommand(object parameter)
        {
            return true;
        }
        #endregion
        #region 私有方法
        /// <summary>
        /// 增加类型信息
        /// </summary>
        /// <param name="window"></param>
        private void AddTypeList(Window window)
        {
            try
            {
                if (Root == null)
                    return;
                #region 获取需要增加类型id
                List<string> productTypeIds = new List<string>();
                var showItems = Root?.GetLastLevelData();
                productTypeIds.AddRange(showItems.Where(s => s.IsChecked == true && !ExistProductTypeIds.Contains(s.RefItem.Id)).Select(s => s.RefItem.Id));
                #endregion

                #region 增加维护类型信息
                if (productTypeIds.Count == 0)
                {
                    MessageTip.Show(this.GetWindow(), "没有需要增加的产品类型");
                    return;
                }
                var flag = BllFactory<ProductDirectoryBll>.Instance.Add(Id, new List<string>(productTypeIds));
                if (flag)
                {
                    ExistProductTypeIds.AddRange(productTypeIds);
                    MessageTip.Show(this.GetWindow(), "添加成功");
                    if (window != null)
                    {
                        //window.DialogResult = true;
                    }

                }
                else
                {
                    MessageTip.Show(this.GetWindow(), BllFactory<ProductDirectoryBll>.Instance.ErrorMessage, "失败");
                }
                #endregion
            }
            catch (Exception ex)
            {

              MessageTipEx.Show(this.GetWindow(), ex);
            }
        }
        #endregion
        #region 加载数据    
        private void LoadProductTypeItems()
        {
            Root = new TreeNavigation<ShowDataItem>();
            Root.CurrentItemChanged = ChangeSelectedItem;
            Root.Items.AddRange(LoadFamilys(null));
            Root.CurrentItem = Root.Items.FirstOrDefault();
        }
        private void LoadExistTypes(string venderId)
        {
            ExistProductTypeIds = new List<string>();
            var types = BllFactory<ProductDirectoryBll>.Instance.GetAllItems(venderId);
            var ids = types.Select(t => t.ProductTypeId).ToList();
            ExistProductTypeIds.AddRange(ids);
        }
        #endregion
       
       
        #region 加载类型相关
        private string GetCurrentFamily()
        {
            string result =  Root.CurrentItem?.RefItem?.Id;
          
            return result??string.Empty;
        }
        private void ChangeSelectedItem(TreeNavigationItem<ShowDataItem> item)
        {
            if (item == null)
                return;
            if (item.Level > 3)
                return;
            item.Nodes.Clear();
            string familyCode = GetCurrentFamily();
            if (item.Level == 1)
            {
                item.Nodes.AddRange(GetBrands(new ProdcutTypeCondition() { FamilyCode = item.RefItem.Id }));
            }
            else if (item.Level == 2)
            {
                item.Nodes.AddRange(GetProducts(new ProdcutTypeCondition() { BrandId = item.RefItem.Id,FamilyCode= familyCode }));
            }
            else if (item.Level == 3)
            {
                item.Nodes.AddRange(GetProductTypes(new ProdcutTypeCondition() { ProductId = item.RefItem.Id, FamilyCode = familyCode }));
                item.UpdateCheckedState();
            }
            item.CurrentItem = item.Nodes.FirstOrDefault();
        }
        /// <summary>
        /// 当前型号数据
        /// </summary>
        private List<ProductTypeDetailItem> DataSource { get; set; }
        private List<TreeNavigationItem<ShowDataItem>> LoadFamilys(ProdcutTypeCondition conditon)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            #region 数据查询
            if (DataSource == null)
            {
                DataSource = ProductTypeManager.ConverToList(BllFactory<GeneralBll>.Instance.GetProductTypeItems());
            }
            #endregion
            var familys = ProductTypeManager.GetFamilys(new ProdcutTypeCondition(DataSource));
            familys.ForEach(b => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = b }));
            return results;
        }
        private List<TreeNavigationItem<ShowDataItem>> GetBrands(ProdcutTypeCondition conditon)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            #region 数据查询
            if (DataSource == null)
            {
                return results;
            }
            #endregion
            var dataSource = DataSource.Where(p => p.FamilyCode == conditon.FamilyCode).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,FamilyCode=conditon.FamilyCode });
            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 转换数据
            List<string> useList = ExistProductTypeIds ?? new List<string>();
            foreach (var productType in productTypes)
            {
                var useData = new TreeNavigationItem<ShowDataItem>()
                {
                    RefItem = productType
                };

                useData.IsChecked = useList.Any(id => id == useData.RefItem?.Id);
                useData.IsEnabled = !(useData.IsChecked == true);
                results.Add(useData);
            }
            #endregion

            return results;
        } 
        #endregion
    }
}
