using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Com.FirmLib.UI.Insuer;
using Com.FirmLib.UI.Insuer.Model;
using Com.FirmLib.UI.Maintainer;
using Com.FirmLib.UI.Manufacturer;
using Com.FirmLib.UI.Manufacturer.Model;
using Com.FirmLib.UI.Seller;
using FWindSoft.Wpf;
using FWindSoft.Wpf.Controls;

namespace Com.FirmLib.UI
{
    public static class FirmLibPost
    {
        private static readonly Dictionary<string, Type> m_TypeRelations = new Dictionary<string, Type>();

        static FirmLibPost()
        {
            m_TypeRelations.Add(typeof(WinManufactureBaseInfo).Name, typeof(WinManufactureBaseInfo));
            m_TypeRelations.Add(typeof(WinManufactureInfo).Name, typeof(WinManufactureInfo));
            m_TypeRelations.Add(typeof(WinProductType).Name, typeof(WinProductType));
            m_TypeRelations.Add(typeof(WinSellerBaseInfo).Name, typeof(WinSellerBaseInfo));
            m_TypeRelations.Add(typeof(WinSellerInfo).Name, typeof(WinSellerInfo));
            m_TypeRelations.Add(typeof(WinSellerProjectContract).Name, typeof(WinSellerProjectContract));
            m_TypeRelations.Add(typeof(WinMaintainerBaseInfo).Name, typeof(WinMaintainerBaseInfo));
            m_TypeRelations.Add(typeof(WinMaintainerInfo).Name, typeof(WinMaintainerInfo));
            m_TypeRelations.Add(typeof(WinMaintainerProjectAsset).Name, typeof(WinMaintainerProjectAsset));
            m_TypeRelations.Add(typeof(WinInsuerBaseInfo).Name, typeof(WinInsuerBaseInfo));
            m_TypeRelations.Add(typeof(WinInsuerInfo).Name, typeof(WinInsuerInfo));
            m_TypeRelations.Add(typeof(WinInsurancePolicyEditor).Name, typeof(WinInsurancePolicyEditor));
        }

        public static Window StartWindow(string path, Dictionary<string, object> values)
        {
            Window win=null;
            try
            {
                WinFirmMain useWin = null;
                var flags = path.Split('\\').ToList();
                for (int i = 0; i < flags.Count; i++)
                {
                    var child = GetNWindow(flags[i], values);
                    if (child == null)
                        continue;
                    if (useWin == null)
                    {
                        useWin = new WinFirmMain(child);
                    }
                    else
                    {
                        useWin.NavigationBar.Add(child);
                    }
                }
                win = useWin;
            }
            catch (Exception e)
            {

               MessageTipEx.Show( e);
            }
            if (win != null)
            {
                win.Title = "厂商库管理";
            }
            return win;
        }

        /*
        解析模式  base/man/type
       */
        private static NChildWindow GetNWindow(string flag, Dictionary<string, object> values)
        {
            Type useType = null;
            if (!m_TypeRelations.TryGetValue(flag, out useType))
                throw new ArgumentException(nameof(flag) + "标志的窗体未找到");
            var window= Activator.CreateInstance(useType,true) as NChildWindow;
            if (window != null)
            {
                LoadParameter parameter = ParseLoadParameter(window, values);
                LoadParameter.StartParameter(window, parameter);
            }
            return window;
        }

        private static LoadParameter ParseLoadParameter(NChildWindow window,Dictionary<string, object> values)
        {
            LoadParameter result = null;
            do
            {
                #region 生产厂家维护
                if (typeof(WinManufactureBaseInfo) == window.GetType())
                {
                    break;
                }
                if (typeof(WinManufactureInfo) == window.GetType())
                {
                    result = new LoadParameter(values["ManufactureId"]);
                    break;
                }
                if (typeof(WinProductType) == window.GetType())
                {
                    ProductShowItem showItem = new ProductShowItem();
                    showItem.VenderId = values["ManufactureId"].ToString();
                    showItem.BrandId = values["BrandId"].ToString();
                    showItem.ProductId = values["ProductId"].ToString();
                    result = new LoadParameter(showItem);
                    break;
                }
                #endregion

                #region 供应商维护
                if (typeof(WinSellerBaseInfo) == window.GetType())
                {
                    break;
                }
                if (typeof(WinSellerInfo) == window.GetType())
                {
                    result = new LoadParameter(values["SellerId"]);
                    break;
                }
                if (typeof(WinSellerProjectContract) == window.GetType())
                {
                    result = new LoadParameter(values["SellerId"]);
                    break;
                }
                #endregion

                #region 维修商维护
                if (typeof(WinMaintainerBaseInfo) == window.GetType())
                {
                    break;
                }
                if (typeof(WinMaintainerInfo) == window.GetType())
                {
                    result = new LoadParameter(values["MaintainerId"]);
                    break;
                }
                if (typeof(WinMaintainerProjectAsset) == window.GetType())
                {
                    result = new LoadParameter(values["MaintainerId"]);
                    break;
                }
                #endregion

                #region 保险商维护
                if (typeof(WinInsuerBaseInfo) == window.GetType())
                {
                    break;
                }
                if (typeof(WinInsuerInfo) == window.GetType())
                {
                    result = new LoadParameter(values["InsuerId"]);
                    break;
                }
                if (typeof(WinInsurancePolicyEditor) == window.GetType())
                {
                    var f = new InsuerParameter();
                    f.Insuer.Id = values["InsuerId"].ToString();
                    result = new LoadParameter(f);
                    break;
                }
                #endregion
            } while (false);


            return result;
        }
    }
}
