using FairyGUI;
using UnityEngine;
using System.Collections.Generic;
namespace GFGGame
{
    /// 
    /// FairyGUI中Window的扩展类,功能界面基本都用这个
    /// 
    public class BaseWindow : UIView, IUIView
    {
        private Window _window;
        private GGraph _btnClose;
        private bool _clickBlankToClose;
        private bool _modal;
        public bool modal
        {
            get => _modal;
            set
            {
                _modal = value;
                if (_window != null)
                {
                    _window.modal = modal;
                }
            }
        }
        protected bool bringToFontOnClick
        {
            set
            {
                _window.bringToFontOnClick = value;
            }
        }
        /// 
        /// 点击空白关闭,true关闭,false不关闭,默认值关闭
        /// 
        protected bool clickBlankToClose
        {
            set
            {
                _clickBlankToClose = value;
                if (this.viewCom != null && this.viewCom.root != null)
                {
                    if (_clickBlankToClose)
                    {
                        CheckCreateCloseButton();
                    }
                    else
                    {
                        CheckRemoveCloseButton();
                    }
                }
            }
        }
        public override GComponent viewCom
        {
            get => this._window;
            set
            {
                contentPane = value;
            }
        }
        /// 
        /// 设置视图组件
        /// 
        private GComponent contentPane
        {
            set
            {
                if (_window == null)
                {
                    _window = new Window();
                }
                _window.contentPane = value;
                _window.modal = modal;
            }
        }
        public override void Dispose()
        {
            // if (packageName == ResPathUtil.GetUIPackagePath("CommonGame") || packageName == ResPathUtil.GetUIPackagePath("Common") || packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
            
            if (_window != null)
            {
                _window.RemoveFromParent();
                _window.Dispose();
                _window = null;
            }
            base.Dispose();
        }
        /// 
        /// 打开界面
        /// 
        public override void Show()
        {
            if (_window != null)
            {
                _window.Show();
            }
        }
        /// 
        /// 刷新界面
        /// 
        protected override void BringToFront()
        {
            if (_window != null)
            {
                this._window.BringToFront();
            }
        }
        protected override void OnInit()
        {
            base.OnInit();
            _clickBlankToClose = true;
            if (isfullScreen)
            {
                viewAnimationType = EnumViewAnimationType.None;
            }
        }
        protected override void OnShown()
        {
            base.OnShown();
            CheckCreateCloseButton();
        }
        
        protected override void OnHide()
        {
            base.OnHide();
            CheckRemoveCloseButton();
        }
        protected override void OnDoHideAnimationCompleted()
        {
            if (this._window != null)
            {
                this._window.Hide();
            }
        }
        virtual protected void closeEventHandler(EventContext context)
        {
            Hide();
        }
        private void CheckCreateCloseButton()
        {
            if (_clickBlankToClose)
            {
                _btnClose = CloseButtonPool.GetCloseButton();
                _window.AddChildAt(_btnClose, 0);
                _btnClose.onClick.Add(closeEventHandler);
                _btnClose.x = -(_btnClose.width - _window.width) / 2;
                _btnClose.y = -(_btnClose.height - _window.height) / 2;
            }
        }
        private void CheckRemoveCloseButton()
        {
            if (_btnClose != null)
            {
                _btnClose.onClick.Remove(closeEventHandler);
                _btnClose.RemoveFromParent();
                CloseButtonPool.PutCloseButton(_btnClose);
                _btnClose = null;
            }
        }
        private class CloseButtonPool
        {
            private static List _pool = new List();
            public static GGraph GetCloseButton()
            {
                GGraph btnClose = null;
                if (_pool.Count > 0)
                {
                    int i = _pool.Count - 1;
                    btnClose = _pool[i];
                    _pool.RemoveAt(_pool.Count - 1);
                }
                else
                {
                    btnClose = new GGraph();
                    btnClose.DrawRect(ViewManager.ViewWidth, GRoot.inst.height, 0, Color.red, UIConfig.modalLayerColor);
                    btnClose.AddRelation(GRoot.inst, RelationType.Size);
                    btnClose.alpha = 0;
                }
                return btnClose;
            }
            public static void PutCloseButton(GGraph gGraph)
            {
                if (!_pool.Contains(gGraph))
                {
                    _pool.Add(gGraph);
                }
            }
        }
    }
}