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

public class MovingPlatform : MonoBehaviour
{
    public GameObject m_PointsContainer;
    public float m_TimeToPointS;
    public bool m_AlwaysShowGizmos = false;

    private int m_TargetPoint;
    private List<Vector2> m_AllTargetPoints = new List<Vector2>();
    private float m_CurrentTime;

    void Awake()
    {
        m_AllTargetPoints.Add(transform.position);

        if (m_PointsContainer != null)
        {
            int allPoints = m_PointsContainer.transform.childCount;
            for (int i = 0; i < allPoints; i++)
            {
                m_AllTargetPoints.Add(m_PointsContainer.transform.GetChild(i).position);
            }
        }

        if (m_AllTargetPoints.Count > 1)
        {
            m_TargetPoint = 0;
        }
        else
        {
            m_TargetPoint = 0;
        }

        m_CurrentTime = m_TimeToPointS;
    }

    public void Update()
    {
        PerformUpdate();
    }

    protected virtual void PerformUpdate()
    {
        if(m_TimeToPointS == 0)
        {
            return;
        }

        m_CurrentTime -= Time.deltaTime;
        if (m_CurrentTime <= 0)
        {
            m_TargetPoint = (m_TargetPoint + 1) % m_AllTargetPoints.Count;
            m_CurrentTime = m_TimeToPointS;
        }

        Vector2 currentPoint = m_AllTargetPoints[m_TargetPoint];
        Vector2 targetPoint = m_AllTargetPoints[(m_TargetPoint + 1) % m_AllTargetPoints.Count];

        Vector3 nextPos = Vector2.Lerp(currentPoint, targetPoint, (m_TimeToPointS - m_CurrentTime) / m_TimeToPointS);
        nextPos.z = transform.position.z;
        this.transform.position = nextPos;
    }

    void OnDrawGizmos()
    {
        if (!m_AlwaysShowGizmos)
            return;

        DrawHelperLines();
    }

    void OnDrawGizmosSelected()
    {
        DrawHelperLines();
    }

    private void DrawHelperLines()
    {
        Gizmos.color = Color.green;

        if (m_AllTargetPoints.Count <= 0)
        {
            int allPoints = m_PointsContainer.transform.childCount;
            Gizmos.DrawLine(transform.position, m_PointsContainer.transform.GetChild(0).position);

            for (int i = 0; i < allPoints - 1; i++)
            {
                Gizmos.DrawLine(m_PointsContainer.transform.GetChild(i).position, m_PointsContainer.transform.GetChild((i + 1) % allPoints).position);
                Gizmos.DrawSphere(m_PointsContainer.transform.GetChild(i).position, 0.1f);
            }

            Gizmos.DrawSphere(m_PointsContainer.transform.GetChild(allPoints - 1).position, 0.1f);
            Gizmos.DrawLine(transform.position, m_PointsContainer.transform.GetChild(allPoints - 1).position);
        }
        else
        {
            for (int i = 0; i < m_AllTargetPoints.Count; i++)
            {
                Gizmos.DrawLine(m_AllTargetPoints[i], m_AllTargetPoints[(i + 1) % m_AllTargetPoints.Count]);
                Gizmos.DrawSphere(m_AllTargetPoints[i], 0.1f);
            }
        }
    }


}