using System.Collections;
using System.Collections.Generic;
using GGJ.LightRenderer;
using GGJ.Models;
using UnityEngine;

namespace GGJ
{
    public class LightSegment
    {
        public Color color;
        public Vector3 start;
        public Vector3 end;
        public float startsWith;
        public float endsWidth;
        public LightMoveType moveType;
        public float dist;
        public bool completionSegment;
    }

    public interface ILightRenderer
    {
        void Draw(LightSegment segment);
        void Clear();
        void SetDistanceTravelled(float dist);
    }

    public class LightManager : MonoBehaviour
    {
        public List<GGJ.Models.Light> lights;

        public ILightRenderer lightRenderer;
        public Line line;
        public float spreadFactor = 0.1f;
        public float lightStartWidth = 0.1f;
        public VectorLight vlight;
        public float speed = 10f;
        private float travelled = 0f;
        private bool isStartedToTravel = false;

        void Awake()
        {
            lightRenderer = vlight;
        }

        public void Clear()
        {
            lightRenderer.Clear();
            lights.Clear();
        }

        public void StartTravel()
        {
            travelled = 0f;
            isStartedToTravel = true;
        }

        // Update is called once per frame
        void Update()
        {
            if (isStartedToTravel)
            {
                travelled += speed * Time.deltaTime;
                lightRenderer.SetDistanceTravelled(travelled);
            }
            lightRenderer.Clear();
            var cw = lightStartWidth;
            var dist = 0f;
            int trackId;
            if (lights.Count > 0)
            {
                trackId = lights[0].trackId;
            }
            else
            {
                return;
            }
            for (int i = 0; i < lights.Count; i++)
            {
                var lig = lights[i];
                var ls = new LightSegment();
                if (lig.trackId != trackId)
                {
                    dist = 0f;
                    cw = lightStartWidth;
                    trackId = lig.trackId;
                }
                ls.start = lig.from;
                ls.color = lig.colorType == LightColorType.All ? Color.white : lig.color;

                if (i + 1 < lights.Count)
                {
                    var nligt = lights[i + 1];
                    if (nligt.trackId == lig.trackId)
                    {
                        ls.end = nligt.from;
                    }
                    else
                    {
                        ls.end = lig.from + lig.direction.normalized * 10;
                    }
                }
                else
                {
                    ls.end = lig.from + lig.direction.normalized * 10;
                }
                ls.moveType = lig.moveType;
                ls.startsWith = cw;
                ls.endsWidth = cw;
                ls.dist = dist;
                ls.completionSegment = lig.isComplation;
                dist += (ls.end - ls.start).magnitude;
                lightRenderer.Draw(ls);
            }
        }
    }
}