using ET;
using FairyGUI;
using System.Collections;
using UnityEngine;
namespace GFGGame
{
    /// 
    /// 视图基类,界面不可以直接继承
    /// 
    public class UIView : IUIView
    {
        private string _descFilePath;
        private EnumViewAnimationType _viewAnimationType = EnumViewAnimationType.None;
        /// 
        /// 打开视图传递的参数
        /// 
        public object viewData { get; set; }
        public string viewName { get; set; }
        /// 
        /// 设置视图组件
        /// 
        public virtual GComponent viewCom { get; set; }
        /// 
        /// 设置是否全屏自适应
        /// 
        public bool isfullScreen { get; set; }
        /// 
        /// FairyGUI包名
        /// 
        public string packageName
        {
            get
            {
                return _descFilePath;
            }
            set
            {
                _descFilePath = ResPathUtil.GetUIPackagePath(value);
                GFGUIPackage.AddPackage(_descFilePath);
            }
        }
        /// 
        /// 界面关闭时间
        /// 
        /// 
        public long closeTime { get; set; }
        /// 
        /// 是否显示中
        /// 
        public bool isShowing
        {
            get { return viewCom != null && viewCom.onStage; }
        }
        /// 
        /// 设置视图显示和隐藏的动画类型
        /// 
        protected EnumViewAnimationType viewAnimationType
        {
            set
            {
                _viewAnimationType = value;
            }
        }
        public UIView()
        {
            //
            Init();
            OnInit();
            if (viewCom != null)
            {
                viewCom.onAddedToStage.Add(__addedToStage);
                viewCom.onRemovedFromStage.Add(__removeFromStage);
            }
        }
        public virtual void Dispose()
        {
            if (viewCom != null)
            {
                viewCom.RemoveFromParent();
                viewCom.Dispose();
                viewCom = null;
            }
            if (_descFilePath != null)
            {
                if (packageName != ResPathUtil.GetUIPackagePath("CommonGame") && packageName != ResPathUtil.GetUIPackagePath("Common") && packageName != ResPathUtil.GetUIPackagePath("Main"))
                {
                    GFGUIPackage.RemovePackage(_descFilePath);
                } //这几个包不释放
            }
            _descFilePath = null;
            viewData = null;
            ViewManager.ClearUIView(viewName);
        }
        virtual protected void Init()
        {
        }
        virtual protected void OnInit()
        {
        }
        /// 
        /// 界面打开状态刷新界面
        /// 
        virtual public void Refresh()
        {
            object temp = viewData;
            this.OnHide();
            this.viewData = temp;
            this.OnShown();
        }
        public virtual void Show()
        {
        }
        public virtual void Hide()
        {
            // UIPackageManager.Instance.RemovePackageView(_descFilePath, viewName);
            // UIPackageManager.Instance.AddCloseTime(_descFilePath);
            closeTime = TimeHelper.ClientNowSeconds();
            DoHideAnimation();
        }
        virtual protected void OnShown()
        {
            if (isfullScreen)
            {
                MakeFullScreen(viewCom);
            }
            AddEventListener();
            // if (GuideController.IsGuide())
            // {
            //     Timers.inst.AddUpdate(UpdateToCheckGuide);
            // }
        }
        virtual protected void OnHide()
        {
            viewData = null;
            RemoveEventListener();
            // Timers.inst.Remove(UpdateToCheckGuide);
            TryCompleteGuide();
        }
        virtual protected void RemoveEventListener()
        {
        }
        virtual protected void AddEventListener()
        {
        }
        virtual protected void UpdateToCheckGuide(object param)
        {
        }
        virtual protected void TryCompleteGuide()
        {
        }
        protected virtual void DoShowAnimation()
        {
            OnShown();
            switch (_viewAnimationType)
            {
                case EnumViewAnimationType.ZOOM_CENTER:
                    ViewAnimationFactory.ZoomInCenter(this.viewCom);
                    break;
                default:
                    break;
            }
        }
        protected virtual void DoHideAnimation()
        {
            switch (_viewAnimationType)
            {
                case EnumViewAnimationType.ZOOM_CENTER:
                    ViewAnimationFactory.ZoomOutCenter(this.viewCom, this.OnDoHideAnimationCompleted);
                    break;
                default:
                    this.OnDoHideAnimationCompleted();
                    break;
            }
        }
        protected virtual void OnDoHideAnimationCompleted()
        {
            if (this.viewCom != null)
            {
                this.viewCom.RemoveFromParent();
            }
        }
        private void MakeFullScreen(GObject ui)
        {
            ui.MakeFullScreen();
            ui.AddRelation(GRoot.inst, RelationType.Size);
        }
        void __addedToStage()
        {
            DoShowAnimation();
        }
        void __removeFromStage()
        {
            OnHide();
            if (_descFilePath == ResPathUtil.GetUIPackagePath("Login") || _descFilePath == ResPathUtil.GetUIPackagePath("Loading") || _descFilePath == ResPathUtil.GetUIPackagePath("CreateRole"))//这几个界面关闭后立即释放
            {
                Dispose();
            }
        }
    }
}