using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody2D))]

public class EnvironmentMovement : MonoBehaviour
{
    public string name = "";
    WaveGenerator IntervalScript;

    public int[] timings;
    public string[] actions;
    public int intervalCounter;
    public int timingCounter;

    public bool move;
    public bool rotate;
    public bool laser;

    string[] actionDirections;
    public float moveSpeed;
    Vector3 moveOrigin;
    public float rotateSpeed;
    GameObject Beam;
    Animator objectAnimator;
    public float laserStartTime;
    float laserTime;

    // Use this for initialization
    void Start ()
    {
        IntervalScript = GameObject.Find("Wave Generator").GetComponent<WaveGenerator>();

        //intervalCounter = -1;
        intervalCounter = intervalCounter - 1;
        timingCounter = 0;

        actionDirections = new string[3];
        objectAnimator = GetComponent<Animator>();
        if (transform.Find("Beam"))
        {
            Beam = transform.Find("Beam").gameObject;
            Beam.SetActive(false);
        }
        laserTime = laserStartTime;

        ActionCheck();
    }

    // Update is called once per frame
    void FixedUpdate ()
    {
        //Whenever an Interval occurs.
        if (IntervalScript.hitInterval == true)
        {
            ActionCheck();
        }

        //Actions

        //Move
        if (move == true)
        {
            switch (actionDirections[1])
            {
                case "Up":
                    transform.position = Vector3.MoveTowards(transform.position, moveOrigin + new Vector3(0, int.Parse(actionDirections[2]), 0), (moveSpeed * Time.deltaTime));
                    if (transform.position == moveOrigin + new Vector3(0, int.Parse(actionDirections[2]), 0))
                    {
                        move = false;
                    }
                    break;
                case "Down":
                    transform.position = Vector3.MoveTowards(transform.position, moveOrigin + new Vector3(0, -int.Parse(actionDirections[2]), 0), (moveSpeed * Time.deltaTime));
                    if (transform.position == moveOrigin + new Vector3(0, -int.Parse(actionDirections[2]), 0))
                    {
                        move = false;
                    }
                    break;
                case "Left":
                    transform.position = Vector3.MoveTowards(transform.position, moveOrigin + new Vector3(-int.Parse(actionDirections[2]), 0, 0), (moveSpeed * Time.deltaTime));
                    if (transform.position == moveOrigin + new Vector3(-int.Parse(actionDirections[2]), 0, 0))
                    {
                        move = false;
                    }
                    break;
                case "Right":
                    transform.position = Vector3.MoveTowards(transform.position, moveOrigin + new Vector3(int.Parse(actionDirections[2]), 0, 0), (moveSpeed * Time.deltaTime));
                    if (transform.position == moveOrigin + new Vector3(int.Parse(actionDirections[2]), 0, 0))
                    {
                        move = false;
                    }
                    break;
            }
        }

        //Rotate
        if (rotate == true)
        {
            switch (actionDirections[1])
            {
                case "Left":
                    transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, int.Parse(actionDirections[2])), Time.fixedDeltaTime * rotateSpeed);
                    if (transform.rotation == Quaternion.Euler(0, 0, int.Parse(actionDirections[2])))
                    {
                        rotate = false;
                    }
                    break;
                case "Right":
                    transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, -int.Parse(actionDirections[2])), Time.fixedDeltaTime * rotateSpeed);
                    if (transform.rotation == Quaternion.Euler(0, 0, -int.Parse(actionDirections[2])))
                    {
                        rotate = false;
                    }
                    break;
            }
        }

        //Laser
        if (laser == true)
        {
            laserTime -= Time.deltaTime;
            if (laserTime <= 0)
            {
                laserTime = laserStartTime;
                Beam.SetActive(false);
                laser = false;
            }
        }
    }

    void ActionCheck()
    {
        intervalCounter += 1;
        if (intervalCounter >= timings[timingCounter])
        {
            actionDirections = actions[timingCounter].Split();
            switch (actionDirections[0])
            {
                case "Move":
                    moveOrigin = transform.position;
                    move = true;
                    break;
                case "Rotate":
                    rotate = true;
                    break;
                case "Cue":
                    objectAnimator.SetTrigger(actionDirections[1]);
                    break;
                case "Laser":
                    Beam.SetActive(true);
                    laser = true;
                    break;
                case "":
                    //Skip Beat.
                    break;
            }
            timingCounter += 1;
            if (timingCounter == timings.Length)
            {
                timingCounter = 0;
            }
            intervalCounter = 0;
        }
    }
}
