using UnityEngine;
using System.Collections.Generic;
using System;
using FairyGUI;
namespace GFGGame
{
    /// 
    /// ��ͼ������
    /// ������ͼ����ʾ������
    /// 
    public class ViewManager
    {
        private static Dictionary _viewDic;
        private static GComponent _bottomLayer;
        private static GComponent _topLayer;
        private static Dictionary _goBackDatas = new Dictionary();
        public static void Init()
        {
            GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("Common"));
            UIConfig.buttonSound = (NAudioClip)UIPackage.GetItemAsset("Common", "click");
            Font afont = GFGAsset.Load(ResPathUtil.GetFontPath("STZHONGS"));
            FontManager.RegisterFont(new DynamicFont("STZhongsong", afont));
            UIConfig.defaultFont = "STZhongsong";
            Font afont1 = GFGAsset.Load(ResPathUtil.GetFontPath("ZIHUN125"));
            FontManager.RegisterFont(new DynamicFont("ZIHUN125", afont1));
            Font afont2 = GFGAsset.Load(ResPathUtil.GetFontPath("SIMKAI"));
            FontManager.RegisterFont(new DynamicFont("SIMKAI", afont2));
            _viewDic = new Dictionary();
            //��ʼ����ͼ������
            _bottomLayer = CreateLayer("BottomLayer");
            _topLayer = CreateLayer("TopLayer");
            _topLayer.sortingOrder = ConstSortingOrder.TOP;
        }
        public static void AddChildToBottomLayer(GObject gObject)
        {
            _bottomLayer.AddChild(gObject);
        }
        public static void AddChildToTopLayer(GObject gObject)
        {
            _topLayer.AddChild(gObject);
        }
        /// 
        /// ��ʾһ����ͼ
        /// 
        /// Ҫ��ʾ����ͼ����
        /// Ҫ���ݸ���ͼ�IJ���
        /// �Ӹ���ͼ���ص���ͼ��Ϣ
        /// �Ƿ�ر�������ͼ
        public static void Show(string viewName, object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false)
        {
            string name = GetName(viewName);
            if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
            {
                return;
            }
            if (hideOthers)
            {
                HideAllView(name);
            }
            UIView obj = null;
            if (_viewDic.ContainsKey(name))
            {
                obj = _viewDic[name];
            }
            else
            {
                obj = CreateViewInstance(viewName) as UIView;
                _viewDic.Add(name, obj);
            }
            if (obj != null)
            {
                IUIView view = (IUIView)obj;
                view.viewData = viewData;
                if (!view.isShowing)
                {
                    view.Show();
                    Debug.Log("��ǰ��" + name);
                }
                else
                {
                    view.Refresh();
                }
                if (resetGobackParams)
                {
                    if (_goBackDatas.ContainsKey(name) == true)
                    {
                        _goBackDatas.Remove(name);
                    }
                }
                if (goBackParams != null)
                {
                    _goBackDatas[name] = goBackParams;
                }
                // else 
            }
        }
        public static bool isViewOpen(string viewName)
        {
            string name = GetName(viewName);
            UIView obj = null;
            if (_viewDic.ContainsKey(name))
            {
                obj = _viewDic[name];
                if (obj != null)
                {
                    IUIView view = (IUIView)obj;
                    if (view.isShowing) return true;
                }
            }
            return false;
        }
        public static bool Show(object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false) where T : class, new()
        {
            // string[] names = typeof(T).FullName.Split('.');
            // string viewName = names[names.Length - 1];
            string name = GetName(typeof(T).FullName);
            if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
            {
                return false;
            }
            if (hideOthers)
            {
                HideAllView(name);
            }
            UIView obj = null;
            if (_viewDic.ContainsKey(name))
            {
                obj = _viewDic[name];
            }
            else
            {
                obj = new T() as UIView;
                _viewDic.Add(name, obj);
            }
            if (obj != null)
            {
                IUIView view = (IUIView)obj;
                view.viewData = viewData;
                if (!view.isShowing)
                {
                    view.Show();
                    Debug.Log("当前打开:" + name);
                }
                else
                {
                    view.Refresh();
                }
                if (goBackParams != null)
                {
                    _goBackDatas[name] = goBackParams;
                }
                else if (resetGobackParams)
                {
                    if (_goBackDatas.ContainsKey(name) == true)
                    {
                        _goBackDatas.Remove(name);
                    }
                }
            }
            return true;
        }
        public static void Hide(string viewName)
        {
            string name = GetName(viewName);
            if (!_viewDic.ContainsKey(name))
            {
                return;
            }
            UIView obj = _viewDic[name];
            if (obj != null && obj.isShowing)
            {
                IUIView view = (IUIView)obj;
                view.Hide();
            }
        }
        public static void Hide()
        {
            string name = GetName(typeof(T).FullName);
            if (!_viewDic.ContainsKey(name))
            {
                return;
            }
            object obj = _viewDic[name];
            if (obj != null)
            {
                IUIView view = (IUIView)obj;
                view.Hide();
            }
        }
        public static void GoBackFrom(string viewName, bool clearGoBackDatas = false)
        {
            string name = GetName(viewName);
            ViewManager.Hide(name);
            if (_goBackDatas.ContainsKey(name))
            {
                object[] gobackItems = _goBackDatas[name];
                string tViewName = gobackItems[0] as string;
                object tViewData = null;
                if (gobackItems.Length > 1)
                {
                    tViewData = gobackItems[1];
                }
                ViewManager.Show(tViewName, tViewData);
            }
            else
            {
                ViewManager.Show(ViewName.MAINUI_VIEW, null, null, true);
            }
        }
        public static object[] GetGoBackDatas(string viewName)
        {
            string name = GetName(viewName);
            object[] value;
            _goBackDatas.TryGetValue(name, out value);
            return value;
        }
        public static UIView GetUIView(string viewName)
        {
            UIView obj = _viewDic[viewName];
            if (obj != null && obj.isShowing)
            {
                return obj;
            }
            return null;
        }
        public static void HideAllView(string excludeViewName = null)
        {
            foreach (string viewName in _viewDic.Keys)
            {
                if (viewName != excludeViewName)
                {
                    Hide(viewName);
                }
            }
        }
        private static object CreateViewInstance(string name)
        {
            // Debug.LogFormat("CreateViewInstance {0}", name);
            Type type = Type.GetType(name);
            if (type != null)
            {
                return Activator.CreateInstance(type);
            }
            return null;
        }
        private static GComponent CreateLayer(string name)
        {
            GComponent layer = new GComponent();
            layer.name = name;
            GRoot.inst.AddChild(layer);
            layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
            layer.AddRelation(GRoot.inst, RelationType.Size);
            return layer;
        }
        public static bool CheckIsTopView(GComponent viewCom)
        {
            if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
            if (viewCom.parent != null)
            {
                int index = viewCom.parent.GetChildIndex(viewCom);
                if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
                {
                    return true;
                }
            }
            if (GRoot.inst.GetTopWindow() == viewCom)
            {
                return true;
            }
            return false;
        }
        public static string GetName(string fullName)
        {
            string[] names = fullName.Split('.');
            string name = names[names.Length - 1];
            return name;
        }
        public static void SetMaskAlpha(float alpha)
        {
            GRoot.inst.modalLayer.alpha = alpha;
        }
    }
}