using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SFramework
{
    public class SUIObjectBase : MonoBehaviour
    {
        public GameObject _rootPanel;
        public delegate void ClickDelegate(GameObject buttonObject);

        void Awake()
        {
            UI_Awake();
        }

        void Start()
        {
            UI_Start();

            UI_RegisterEvent();
            UI_RefreshInfo();
            UI_BindTouchEvent();
        }

        void Update()
        {
            UI_Update();
        }

        void OnDestroy()
        {
            UI_UnregisterEvent();

            UI_OnDestroy();
        }

        public GameObject GetSubUI(string subUIName)
        {
            Debug.Assert(_rootPanel != null);

            Transform subUITransform = _rootPanel.transform.Find(subUIName);
            Debug.Assert(subUITransform != null, string.Format("GetSubUI Failed For Name : {0}", subUIName));
            return subUITransform.gameObject;
        }

        public void BindButtonClick(string gameObjectPathName, ClickDelegate clickFunc)
        {
            Debug.Assert(_rootPanel != null);

            GameObject buttonObject = _rootPanel.transform.Find(gameObjectPathName).gameObject as GameObject;
            UnityEngine.UI.Button buttonControl = buttonObject.GetComponent<UnityEngine.UI.Button>();
            buttonControl.onClick.AddListener(delegate ()
            {
                clickFunc(buttonObject);
            });
        }

        public void BindButtonClick(GameObject buttonObject, ClickDelegate clickFunc)
        {
            Debug.Assert(_rootPanel != null);

            UnityEngine.UI.Button buttonControl = buttonObject.GetComponent<UnityEngine.UI.Button>();
            buttonControl.onClick.AddListener(delegate ()
            {
                clickFunc(buttonObject);
            });
        }

        public virtual void UI_Awake() { }
        public virtual void UI_Start() { }
        public virtual void UI_Update() { }
        public virtual void UI_OnDestroy() { }
        public virtual void UI_RegisterEvent() { }
        public virtual void UI_UnregisterEvent() { }
        public virtual void UI_RefreshInfo() { }
        public virtual void UI_BindTouchEvent() { }
    }
}
