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

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

#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(NamedGameObject))]
public class NamedGameObjectInspector : ComponentHandlerInspector {
}
#endif

public class NamedGameObject : ComponentHandler {
    public override string TypeLabel () {
        return "GO";
    }

    public override string ContentLabel() {
        UpdateComponent();
        return objName;
    }

    public override void UpdateComponent() {
        base.UpdateComponent();
        go = gameObject;
    }

    public override void Show(MyBT.NodeState nodeState) {
        if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running))  {
            gameObject.SetActive(true);
            Task.SetSucceeded();
        }
    }

    public override void Hide(MyBT.NodeState nodeState) {
        if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running))  {
            gameObject.SetActive(false);
            Task.SetSucceeded();
        }
    }

    public override string[][] helpText {
        get {
            return new string[][] {
                new string[] {"Show", $"BTC.Show(\"{gameObject.name}\")"},
                new string[] {"Hide", $"BTC.Hide(\"{gameObject.name}\")"},
                new string[] {"Set", $"BTC.Set(\"{gameObject.name}\", \"parent\", \"TARGETOBJECT\")"}
            };
        }
    }

    public override void Set(MyBT.NodeState nodeState, string key, string value) {
        if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running))  {
            if (key == "parent") {
                Transform newParent = null;
                List<ComponentHandler> componentHandlers = BTC.Instance.GetHandlers(value);
                if (componentHandlers.Count > 0) {
                    newParent = componentHandlers[0].gameObject.transform;
                }
                gameObject.transform.SetParent(newParent);

                gameObject.transform.localPosition = Vector3.zero;
                gameObject.transform.localRotation = Quaternion.identity;
                gameObject.transform.localScale = Vector3.one;
                
                Task.SetSucceeded();
                return;
            }

            gameObject.transform.localPosition = Vector3.zero;
            gameObject.transform.localRotation = Quaternion.identity;
            gameObject.transform.localScale = Vector3.one;
            
            Task.SetFailed();
        }
    }

    public override void SetPosition(NodeState nodeState, float newX, float newY, float newZ)
    {
        if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running))
        {
            Vector3 newPos = new Vector3(newX, newY, newZ);
            gameObject.transform.localPosition = newPos;
            Task.SetSucceeded();
        }
    }

    public GameObject go;
    public string objName = "GoXY";
}