//============= 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 UnityEngine.UI;
using MyBT;

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

[System.Serializable]
public class NamedParticle : ComponentHandler
{
    public override string TypeLabel()
    {
        return "Particle";
    }

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

    public override void UpdateComponent()
    {
        base.UpdateComponent();
        _particleSys = GetComponent<ParticleSystem>();
    }

    public string objName = "ParticleXY";
    private ParticleSystem _particleSys;
    private List<ParticleSystem.Particle> _enteredParticles = new List<ParticleSystem.Particle>();
    private bool _enteredTrigger = false;

    public override string[][] helpText
    {
        get
        {
            return new string[][] {
                new string[] {"Run", "Return Success on Trigger Event", $"BTC.Run(\"{roomId}\", \"{gameObject.name}\")"},
            };
        }
    }

    public override void Run(NodeState nodeState)
    {
        switch (nodeState)
        {
            case NodeState.FirstRun:
                _enteredTrigger = false;
                break;
            case NodeState.Running:
                if (_enteredTrigger)
                {
                    Task.SetSucceeded();
                }

                break;
            case NodeState.Aborting:
                _enteredTrigger = false;
                break;
        }
    }

    private void OnParticleTrigger()
    {
        // Get particles that entered the trigger
        int numEnter = _particleSys.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, _enteredParticles);

        if (numEnter > 0)
        {
            _enteredTrigger = true;
        }
    }
}