using System;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Dialog Example
/// Multiple event calls to modify the contents of a dialog box are not recommended,
/// Usually, I will choose to conduct actual dialogue development in combination with some kind of configuration table system,
/// But doing so here can make users learn BehaviorTreeSystem more simply and clearly
/// 多次事件调用来修改对话框的内容并不是一个值得推荐的做法,
/// 通常情况下,我会选择结合配置表系统进行实际对话开发,
/// 但是这里如此做能让用户学习起来更简单和清晰 
/// 
/// The behavior tree can quickly build a dialogue system and speed up the development of the plot system
/// 行为树能快速的构建出一个对话系统,加快剧情系统的开发
/// </summary>
public class Example2 : MonoBehaviour
{
    [SerializeField]
    Transform m_grid;
    [SerializeField]
    BehaviorTreeSlayer.BehaviorTree behaviorTree;
    [SerializeField]
    Texture2D[] icons;
    [SerializeField]
    Text label;
    [SerializeField]
    RawImage portrait;
    private void Start()
    {
        behaviorTree.Regist("SetIcon", SetIcon);
        behaviorTree.Regist("SetText", SetText);
        behaviorTree.Regist("SetBtns", SetBtns);
    }

    private void SetBtns(object obj)
    {
        int count = (int)obj;
        for (int i = 0; i < count; i++)
        {
            m_grid.GetChild(i).gameObject.SetActive(true);
        }
        for (int i = count; i < m_grid.childCount; i++)
        {
            m_grid.GetChild(i).gameObject.SetActive(false);
        }

    }
    private void OnDestroy()
    {
        behaviorTree.UnRegist("SetIcon");
        behaviorTree.UnRegist("SetText");
        behaviorTree.UnRegist("SetBtns");
    }
    private void SetText(object obj)
    {
        //Please notice that sometimes, \n maybe auto translate to \\n
        label.text = obj.ToString().Replace("\\n", "\n");
    }

    private void SetIcon(object obj)
    {
        int idx = (int)obj;
        portrait.texture = icons[idx % icons.Length];
    }

    public void OnClick(int x)
    {
        behaviorTree["index"] = x;
        behaviorTree.Dispatch("EvtIndex", behaviorTree);
    }

}