namespace BehaviorTreeSlayer
{
    public class BTreeManager
    {
        static BTreeManager ins;
        public static BTreeManager Ins
        {
            get
            {
                if (ins == null)
                {
                    ins = new BTreeManager();
                }
                return ins;
            }
        }
        private BTreeManager() { }
        Entry entry;
        string config;
        bool allowRun;
        public void Init()
        {
            entry = null;
            config = "";
        }
        public void Init(string xml)
        {
            if (config == null || entry == null || config.Length != xml.Length)
            {
                entry = XmlUtils.DeSerialize<Entry>(xml);
                config = xml;
            }
        }
        public void Init(Entry e)
        {
            entry = e;
        }
        public Entry Entry
        {
            get
            {
                if (entry == null)
                {
                    if (string.IsNullOrEmpty(config))
                    {
                        entry = new Entry();
                    }
                    //else
                    //{
                    //    entry = XmlUtils.DeSerialize<Entry>(config);
                    //}
                }
                return entry;
            }
        }
        public void SetEntry(string xml)
        {
            entry = XmlUtils.DeSerialize<Entry>(xml);
        }
        public void Start(object obj)
        {
            allowRun = true;
            Entry.Enter(obj);
        }
        public void Update(double deltaTime, object obj)
        {
            if (allowRun)
            {
                entry?.Tick(deltaTime, obj);
            }
        }
    }

}