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

public class Bezier : MonoSingleton<Bezier>
{
  //  public Transform[] controlPoints;
    public LineRenderer lineRenderer;

    private int layerOrder = 0;
    private int _segmentNum = 50;

    private float rr = 0.2f;

    public Transform point;


    private Vector2 Interval = new Vector2(0.05f, 0);

    private Material material;

    private Vector2 offset;
    // 0.3 弧度半径
    void Start()
    {

        material = lineRenderer.materials[0];
        if (!lineRenderer)
        {
            lineRenderer = GetComponent<LineRenderer>();
        }
        // lineRenderer.sortingLayerID = layerOrder;

        // DrawCurve();

    
        StartCoroutine(SetOffset());        
    }

    public void SetRoute(List<Vector3> listPos)
    {
        int index = 0;
       
        RouteClear();
        lineRenderer.positionCount = (listPos.Count - 2) * _segmentNum + listPos.Count;
        Debug.Log(lineRenderer.positionCount);
        lineRenderer.SetPosition(index++, listPos[0]);
        for (int i = 1; i < listPos.Count - 1; i++)
        {

            point.position = listPos[i];
            point.LookAt(listPos[i - 1]);
            point.Translate(0, 0, rr, Space.Self);
            Vector3 pos1 = point.position;
            lineRenderer.SetPosition(index++, pos1);

            //GameObject obj = new GameObject(i.ToString());
            //obj.transform.position = point.position;

            //GameObject obj2 = new GameObject(i.ToString());
            //obj2.transform.position = controlPoints[i].position;

            point.position = listPos[i];
            point.LookAt(listPos[i + 1]);
            point.Translate(0, 0, rr + 0.05f, Space.Self);
            Vector3 pos2 = point.position;

            Debug.Log(pos1 + "  " + point.position + "  " + pos2);

            List<Vector3> listLinePos = DrawCurve(pos1, listPos[i], pos2);
            for (int j = 0; j < listLinePos.Count; j++)
            {
                lineRenderer.SetPosition(index++, listLinePos[j]);
            }

        }

        lineRenderer.SetPosition(index++, listPos[listPos.Count - 1]);

        float dis = 0;
        for (int i = 0; i < lineRenderer.positionCount-1; i++)
        {
            dis += Vector3.Distance(lineRenderer.GetPosition(i), lineRenderer.GetPosition(i + 1));
        }

        material.SetTextureScale("_MainTex", new Vector2(dis * 8, 1f));

    }

    public void RouteClear()
    {
        lineRenderer.positionCount = 0;
    }

    private IEnumerator SetOffset()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.04f);
            offset -= Interval;
            material.SetTextureOffset("_MainTex", offset);
           
        }
    }

    void Update()
    {

       // DrawCurve();

    }

    void DrawCurve()
    {
        //for (int i = 1; i <= _segmentNum; i++)
        //{
        //    float t = i / (float)_segmentNum;
        //    int nodeIndex = 0;
        //    Vector3 pixel = CalculateCubicBezierPoint(t, controlPoints[nodeIndex].position,
        //        controlPoints[nodeIndex + 1].position, controlPoints[nodeIndex + 2].position);
        //    lineRenderer.positionCount = i;
        //    lineRenderer.SetPosition(i - 1, pixel);
        //}

    }

    List<Vector3>  DrawCurve( Vector3 pos1,Vector3 pos2 ,Vector3 pos3)
    {
        List<Vector3> listPos = new List<Vector3>();
        for (int i = 1; i <= _segmentNum; i++)
        {
            float t = i / (float)_segmentNum;
            int nodeIndex = 0;
            Vector3 pixel = CalculateCubicBezierPoint(t, pos1,
                pos2, pos3);

            listPos.Add(pixel);
            
        }
        return listPos;
    }

    Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
    {
        float u = 1 - t;
        float tt = t * t;
        float uu = u * u;

        Vector3 p = uu * p0;
        p += 2 * u * t * p1;
        p += tt * p2;

        return p;
    }

}