using UnityEngine;
using UnityEngine.UI;

namespace Stary.Evo.Example.Counter
{
    public class CounterViewController : MonoBehaviour, IController
    {
        private Text _countText;
        private ICounterData mCounterData;

        void Start()
        {
            // 获取
            mCounterData = this.GetData<ICounterData>();
            mCounterData.Count.Register(UpdateView);
            _countText = transform.Find("CountText").GetComponent<Text>();
            _countText.text = mCounterData.Count.Value.ToString();
            transform.Find("ButtonAdd").GetComponent<Button>()
                .onClick.AddListener(() =>
                {
                    // 交互逻辑
                    this.SendCommand<CommandAnd>();
                });

            transform.Find("ButtonSub").GetComponent<Button>()
                .onClick.AddListener(() =>
                {
                    // 交互逻辑
                    this.SendCommand<CommandSub>();

                });

        }

        //表现逻辑
        void UpdateView(int countValue)
        {
            _countText.text = countValue.ToString();
        }

        private void OnDestroy()
        {
            mCounterData.Count.Register(UpdateView);
            mCounterData = null;
        }

        IArchitecture IBelongToArchitecture.GetArchitecture()
        {
            return CounterApp.Interface;
        }
    }

}