﻿using UnityEngine;
using System.Collections;
using TMPro;

public class movePod : MonoBehaviour {

	public float rotationSpeed = 20.0f;
	public float translationSpeed = 10.0f;
	public Transform target;
    public Transform Head;
    public enum MotionType { SIMPLE, AUTO_DIRECTION, AUTO_CENTER }
    public MotionType motionType = MotionType.SIMPLE;
    public float smoothTime = 0.3f;
    public TextMeshPro text;


    float yVelocity = 0.0f;
    float textDuration = 3.0f; //seconds
    float textToGo = 0.0f;

    const string AxeHorizontal = "L_XAxis_1";
    const string AxeVertical = "L_YAxis_1";

	// Use this for initialization
	void Start () {
		if (target == null) { target = transform; }
	}

    void Update()
    {
        //if (Input.GetButtonUp("Fire2"))
        if (Input.GetButtonUp("B_1"))

        {
            ShowText("Simple");
            motionType = MotionType.SIMPLE;
        }
        else if (Input.GetButtonUp("X_1"))
        {
            ShowText("Direction");
            motionType = MotionType.AUTO_DIRECTION;
        }
        else if (Input.GetButtonUp("Y_1"))
        {
            ShowText("Direction et Rotation");
            motionType = MotionType.AUTO_CENTER;
        }

        if (motionType == MotionType.SIMPLE)
        {
            UpdateSimple();
        } else
        {
            UpdateAutoCenter();
        }

        if ((text != null) && (textToGo > 0.0f))
        {
            textToGo -= Time.deltaTime;
            if (textToGo <= 0.0f)
            {
                text.gameObject.SetActive(false);
            }
        }
    }

    void ShowText(string value)
    {
        if (text == null) { return; }
        text.text = value;
        textToGo = textDuration;
        text.gameObject.SetActive(true);
    }

    bool UpDownAndStraff()
    {
        if (Input.GetButton("A_1"))
        {
            target.Translate(
                translationSpeed * Time.deltaTime *
                (Vector3.up * -Input.GetAxis(AxeVertical)
                + Vector3.right * Input.GetAxis(AxeHorizontal)));
            return true;
        }
        return false;
    }

    void UpdateSimple()
    {
        if (target == null) { return; }

        if (UpDownAndStraff()) { return; }

        float dp = rotationSpeed * Time.deltaTime * Input.GetAxis(AxeHorizontal);
        target.RotateAround(Head.position, target.up, dp);
        float df = translationSpeed * Time.deltaTime * -Input.GetAxis(AxeVertical);
        Vector3 direction = target.forward;
        target.Translate(direction * df, Space.World);
    }

    void UpdateAutoCenter()
    {
        if (target == null ) { return; }

        if (UpDownAndStraff()) { return; }

        float deltaAngle = rotationSpeed * Time.deltaTime * Input.GetAxis(AxeHorizontal);
        target.RotateAround(Head.position, target.up, deltaAngle);

        float deltaForward = translationSpeed * Time.deltaTime * -Input.GetAxis(AxeVertical);
        Vector3 direction = target.forward;
        if (Head != null)
        {
            direction = new Vector3(Head.forward.x, 0.0f, Head.forward.z);
            direction.Normalize();
            if ( (deltaForward > Mathf.Epsilon) && (motionType == MotionType.AUTO_CENTER)) //si on marche en avant
            {
                float angle = Mathf.SmoothDampAngle(target.eulerAngles.y, Head.eulerAngles.y, ref yVelocity, smoothTime);
                target.RotateAround(Head.position, target.up, Mathf.DeltaAngle(target.eulerAngles.y, angle));
            }
            else
            {
                yVelocity = 0.0f;
            }
        }
        target.Translate(direction * deltaForward, Space.World);
    }
}
