//============= Copyright (c) Ludic GmbH, All rights reserved. ==============
//
// Purpose: Part of the My Behaviour Tree Code
//
//=============================================================================

using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;

namespace MyBT {
    [System.Serializable]
    public class ActionNodeRuntimeData : NodeRuntimeData { //, ISerializationCallbackReceiver {
        [SerializeField, XmlIgnore]
        private byte[] serializedData = null;

        // [SerializeField]... its impossible to serialize by unity
        public object userData {
            get {
                return ExtensionMethods.Deserialize(serializedData);
            }
            set {
                serializedData = ExtensionMethods.SerializeToByteArray(value);
                if (node.debugChangesActive) Debug.Log(node.NodeLogger($"ActionNodeRuntimeData", $"serialize {value} {(serializedData!=null?serializedData.Length:0)}"));
            }
        }

        //[SerializeField, XmlIgnore]
        //private NodeResult _taskResult = NodeResult.Continue;
        //public NodeResult taskResult {
        //    get {
        //        return _taskResult;
        //    }
        //    set {
        //        _taskResult = value;
        //        if (node.debugChangesActive)
        //            Debug.Log(node.NodeLogger("taskResultSet", $"set taskResult to '{value}'"));
        //    }
        //}


        public ActionNodeRuntimeData (Node _node, NodeRuntimeData _parentNodeRuntimeData) : base (_node, _parentNodeRuntimeData) {

        }

        ~ActionNodeRuntimeData() {
            Dispose(false);
        }


        public override void Destroy () {
            base.Destroy();
        }

        public override void Clear() {
            base.Clear();
            userData = null;
            serializedData = null;
            // changing taskresult can break execution!
            //taskResult = NodeResult.Undefined;
        }


        // Flag: Has Dispose already been called?
        [System.NonSerialized]
        bool disposed = false;

        // Protected implementation of Dispose pattern.
        protected override void Dispose(bool disposing) {
            if (disposed)
                return;

            if (disposing) {
                // Free any other managed objects here.
                Destroy();
            }

            // Free any unmanaged objects here.
            //

            disposed = true;

            // Call the base class implementation.
            base.Dispose(disposing);
        }
    }
}