
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmSingleProductTypeSelecter.cs
//文件描述: VmSingleProductTypeSelecter.cs
//创 建 者: xls
//创建日期: 2018/9/12 22:34:31
//版 本 号：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;
using FWindSoft.Data;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;
using FWindSoft.Wpf.Common;

namespace Com.FirmLib.UI.Seller
{
    public class VmSingleProductTypeSelector : BaseViewModel
    {
        public VmSingleProductTypeSelector()
        {
            Init();
        }

        private void Init()
        {

        }
        public void Init(LoadParameter parameter)
        {
            OriginProductType = parameter.Parameter as ProductTypeShowItem;
            //todo 加载所有的
            Root = new TreeNavigation<ShowDataItem>();
            Root.CurrentItemChanged = ChangeSelectedItem;
            Root.Items.AddRange(GetBrands());
            Root.CurrentItem = Root.Items.FirstOrDefault(c=>FilterNullCondition(c.RefItem.Id,OriginProductType?.BrandId))?? Root.Items.FirstOrDefault();
          
        }
        private bool FilterNullCondition(string input, string baseMatch)
        {
            if (baseMatch == null)
                return true;
            return input == baseMatch;
        }
        private ProductTypeShowItem OriginProductType { 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));
            }
        }

        public ShowDataItem BrandItem
        {
            get { return Root?.CurrentItem?.RefItem; }
        }
        public ShowDataItem ProductItem
        {
            get { return Root?.CurrentItem?.CurrentItem?.RefItem; }
        }
        public ShowDataItem ProductTypeItem
        {
            get { return Root?.CurrentItem?.CurrentItem?.CurrentItem?.RefItem; }
        }
        #endregion

        #region 命令相关

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void OkCommand(object parameter)
        {
            try
            {
                ProductTypeShowItem showItem = new ProductTypeShowItem();
                showItem.BrandName = BrandItem.Name;
                showItem.BrandId = BrandItem.Id;

                showItem.ProductName = ProductItem.Name;
                showItem.ProductId = ProductItem.Id;

                showItem.ProductTypeName = ProductTypeItem.Name;
                showItem.ProductTypeId = ProductTypeItem.Id;
                showItem.FamilyCode = ProductTypeItem.FamilyCode;
                Window window = parameter as Window;
                if (window != null)
                {
                    window.SetRealResult<ProductTypeShowItem>(showItem);
                    window.DialogResult = true;
                }
                    
            }
            catch (Exception ex)
            {

               MessageTipEx.Show(ex);
            }
        }
        public bool CanOkCommand(object parameter)
        {
            return BrandItem!=null&&ProductItem!=null&&ProductTypeItem!=null;
        }

        #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(item.RefItem.Id));
                item.CurrentItem = item.Nodes.FirstOrDefault(c => FilterNullCondition(c.RefItem.Id, OriginProductType?.ProductId)) ?? item.Nodes.FirstOrDefault();
            }
            else if (item.Level == 2)
            {
                item.Nodes.AddRange(GetProductTypes(item.RefItem.Id));
                item.CurrentItem = item.Nodes.FirstOrDefault(c => FilterNullCondition(c.RefItem.Id, OriginProductType?.ProductTypeId)) ?? item.Nodes.FirstOrDefault();
            }
        }
        /// <summary>
        /// 当前型号数据
        /// </summary>
        private List<ProductTypeDetailItem> DataSource { get; set; }
        /// <summary>
        /// 获取品牌信息
        /// </summary>
        /// <returns></returns>
        private List<TreeNavigationItem<ShowDataItem>> GetBrands()
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            #region 数据查询
            if (DataSource == null)
            {
                DataSource = ProductTypeManager.ConverToList(BllFactory<GeneralBll>.Instance.GetProductTypeItems());
            }
            #endregion
            var brands = ProductTypeManager.GetBrands(new ProdcutTypeCondition(DataSource));
            brands.ForEach(b => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = b }));
            return results;
        }
        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        private List<TreeNavigationItem<ShowDataItem>> GetProducts(string brandId)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            var brands = ProductTypeManager.GetProducts(new ProdcutTypeCondition(DataSource) { BrandId = brandId });
            brands.ForEach(b => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = b }));

            return results;
        }
        /// <summary>
        /// 获取类型信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        private List<TreeNavigationItem<ShowDataItem>> GetProductTypes(string productId)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            var productTypes = ProductTypeManager.GetProductTypes(new ProdcutTypeCondition(DataSource) { ProductId = productId });
            productTypes.ForEach(pt=> results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = pt }));
            return results;
        }
        #endregion
    }
}
