using FairyGUI;
using UnityEngine;
using XGame.Framework.UI;

namespace XGame.Framework.FGUI
{
    /// <summary>
    /// FGUI相关的操作都在IUIViewModel的实现类里
    /// </summary>
    public abstract class FguiViewModel : IUIViewModel, IUIVMInitable
    {
        protected GComponent Panel { get; private set; }
        public Transform Tr => Panel.displayObject.gameObject.transform;

        void IUIVMInitable.Init(IUIPanel uiPanel, IUIViewAdapter adapter)
        {
            Init((uiPanel as FguiPanel).Panel, adapter);
        }
        void IUIVMInitable.Init(IUINested nested, IUIViewAdapter adapter)
        {
            Init((nested as FguiNested).Component, adapter);
        }
        private void Init(GComponent panel, IUIViewAdapter adapter)
        {
            Panel = panel;
            BindComponents(panel, adapter);
            CustomBind(panel, adapter);
        }
        /// <summary>
        /// 结束ui的所有tween
        /// </summary>
        public void KillTweens(bool complete = false)
        {
            GTween.Kill(Panel, complete);
        }
        protected abstract void BindComponents(GComponent panel, IUIViewAdapter adapter);
        /// <summary>
        /// 自定义绑定，有需要的重载
        /// </summary>
        /// <param name="panel"></param>
        protected virtual void CustomBind(GComponent panel, IUIViewAdapter adapter)
        {
        }
    }
}