using Assets.Source.Misc.VFX;
using Assets.Source.Patterns.ObjectPool;
using UnityEngine;

namespace Assets.Source.Misc
{
    public class LineRendererEffect : MonoBehaviour, IPoolObject, IVFX
    {
        public bool AutoReleaseAfterDelay = true;
        public float ReleaseDelay = 2f;
        public LineRenderer[] LineRenderers;
        public bool OffsetTexture;
        public Vector2 TextureOffsetSpeed;

        void OnValidate()
        {
            LineRenderers = GetComponentsInChildren<LineRenderer>();
        }

#if UNITY_EDITOR
        public void OnApplyPrefab()
        {
            LineRenderers = GetComponentsInChildren<LineRenderer>();
        }
#endif
        public void OnSpawnFromPool()
        {
            if (!AutoReleaseAfterDelay)
                return;
            //SetPlaying(true);
            this.After(ReleaseDelay, this.Release);
        }

        public void OnReturnToPool()
        {
        }

        void Update()
        {
            if (OffsetTexture)
                foreach (var lineRenderer in LineRenderers)
                    lineRenderer.material.mainTextureOffset += TextureOffsetSpeed * Time.deltaTime;
        }

        void IVFX.Release()
        {
            this.Release();
        }
        public void ReleaseAfterDelay(float? delay = null)
        {
            this.After(delay ?? ReleaseDelay, this.Release);
        }
    }
}
