using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour 
{
    public GameObject Character;

    private const float MOVEMENT_THRESHOLD = 0.01f;

    private const float MIN_DISTANCE_TO_MOVE = 0.1f;

    private const float MIN_MOVEMENT = 0.1f;
    private const float MAX_MOVEMENT = 0.5f;

    private Vector3 mDefaultDistanceToCharacter;
	
    
    private void Start () 
    {
        mDefaultDistanceToCharacter = transform.position - Character.transform.position;
	}
	
	private void Update () 
    {
        /*Vector3 currentDistance = transform.position - Character.transform.position;
        if
        (
            (Mathf.Abs(Mathf.Abs(currentDistance.x) - Mathf.Abs(mDefaultDistanceToCharacter.x)) > MOVEMENT_THRESHOLD) ||
            (Mathf.Abs(Mathf.Abs(currentDistance.y) - Mathf.Abs(mDefaultDistanceToCharacter.y)) > MOVEMENT_THRESHOLD) ||
            (Mathf.Abs(Mathf.Abs(currentDistance.z) - Mathf.Abs(mDefaultDistanceToCharacter.z)) > MOVEMENT_THRESHOLD)
        )
        {
            transform.position = Character.transform.position + mDefaultDistanceToCharacter;
        }*/

        /*Vector3 targetPosition = Character.transform.position + mDefaultDistanceToCharacter;
        Vector3 currentDistance = transform.position - Character.transform.position;

        if ((transform.position - targetPosition).magnitude > MIN_DISTANCE_TO_MOVE)
        {
            transform.Translate
                (
                    Mathf.Min(Mathf.Sign(currentDistance.x) * MIN_MOVEMENT, Mathf.Max(Mathf.Sign(currentDistance.x) * MAX_MOVEMENT, currentDistance.x)),
                    Mathf.Min(Mathf.Sign(currentDistance.y) * MIN_MOVEMENT, Mathf.Max(Mathf.Sign(currentDistance.y) * MAX_MOVEMENT, currentDistance.y)),
                    Mathf.Min(Mathf.Sign(currentDistance.z) * MIN_MOVEMENT, Mathf.Max(Mathf.Sign(currentDistance.z) * MAX_MOVEMENT, currentDistance.z))
                );
        }*/

        Vector3 currentDistance = transform.position - Character.transform.position;
        if (currentDistance.magnitude > 0.1f)
        {
            float xPos = Mathf.Clamp(Character.transform.position.x + mDefaultDistanceToCharacter.x, 4.75f, 7.63f);
            float yPos = Character.transform.position.y + mDefaultDistanceToCharacter.y;
            float zPos = Mathf.Clamp(Character.transform.position.z + mDefaultDistanceToCharacter.z, -6.76f, 0.34f);
            transform.position = new Vector3(xPos, yPos, zPos);
            //transform.position = Character.transform.position + mDefaultDistanceToCharacter;            
        }
	}
}
