using System.Collections.Generic;
using UnityEngine;
using Vectrosity;

namespace GGJ.LightRenderer
{
    public class Sexy : MonoBehaviour
    {
        private VectorLine spikeLine;
        public GameObject curveMesh;

        public int segments;
        public float radius = 3;
        public Material mat;
        public Color color;
        public string nameOfCurve;

        void Start()
        {
            var c = new List<Vector3>(segments + 1);
            spikeLine = new VectorLine(nameOfCurve, c, 4f, LineType.Continuous);
            spikeLine.SetColor(color);
            spikeLine.material = mat;
            spikeLine.Draw3D();
            curveMesh = GameObject.Find(nameOfCurve);
            curveMesh.transform.position = this.transform.position;
            var r = Random.Range(0, 0.5f);
            LeanTween.rotateZ(curveMesh, 720, 4 + Random.Range(0, 0.4f))
                .setLoopClamp(-1)
                .setLoopType(LeanTweenType.clamp)
                .setDelay(r);
        }

        void Update()
        {
            List<Vector3> spline = new List<Vector3>();
            var n = 60;
            for (int i = 0; i < n; i++)
            {
                float a = i * (360 / n);
                float r = Mathf.Deg2Rad * a;
                var xx = Mathf.Pow(2.7182818285f, -Mathf.Pow(i / 30f, 2f));
                var length = radius + Mathf.Sin(10 * r + Time.time) * 0.2f * xx;
                var x = length * Mathf.Cos(r);
                var y = length * Mathf.Sin(r);
                spline.Add(new Vector3(x, y, 0));
            }
            spikeLine.MakeSpline(spline.ToArray(), segments, true);
            spikeLine.Draw3D();
        }
    }
}