using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public CameraRig m_cameraRig = null;
    public Transform m_camera = null;
    public float m_speed = 3.0f;
    public Animator m_animator = null;
    private MapData m_mapData = null;
    public AudioSource m_audio = null;

	void Start( ) {
        m_mapData = MapData.get( );
	}

	void Update( ) {

        Vector3 direction = Vector3.zero;

        if ( Input.GetKey( KeyCode.Z ) || Input.GetKey( KeyCode.W ) ) {
            direction.z += 1.0f;
        }

        if ( Input.GetKey( KeyCode.S ) ) {
            direction.z -= 1.0f;
        }

        if ( Input.GetKey( KeyCode.A ) || Input.GetKey( KeyCode.Q ) ) {
            direction.x -= 1.0f;
        }

        if ( Input.GetKey( KeyCode.D ) ) {
            direction.x += 1.0f;
        }

        Vector3 forward = m_camera.forward;
        forward.y = 0.0f;
        forward.Normalize( );

        Vector3 right = m_camera.right;
        right.y = 0.0f;
        right.Normalize( );

        Vector3 movement = forward * direction.z + right * direction.x;
        Vector3 oldPosition = transform.position;
        transform.position += movement.normalized * m_speed * Time.deltaTime;

        if ( direction.sqrMagnitude != 0 ) {

            Vector3 up = Vector3.Cross( forward, right );

            Quaternion rotation = Quaternion.identity;
            rotation.SetLookRotation( movement.normalized, up );
            transform.rotation = Quaternion.Slerp( transform.rotation, rotation, 12.0f * Time.deltaTime );
            m_animator.SetBool( "moving", true );

            if ( !m_audio.isPlaying ) {
                m_audio.pitch = Random.Range(.8f, 1.2f);
                m_audio.Play( );
            }

        } else {

            m_animator.SetBool( "moving", false );
            m_audio.Stop( );
        }

        Rect bounds = new Rect( m_mapData.m_size * -0.5f, m_mapData.m_size );

        if ( !bounds.Contains( new Vector2( transform.position.x, transform.position.z ) ) ) {

            movement = transform.position - oldPosition;

            float dot_1 = Vector3.Dot( movement, Vector3.forward );
            float dot_2 = Vector3.Dot( movement, -Vector3.forward );
            float dot_3 = Vector3.Dot( movement, Vector3.right );
            float dot_4 = Vector3.Dot( movement, -Vector3.right );

            float min = Mathf.Min( dot_1, Mathf.Min( dot_2, Mathf.Min( dot_3, dot_4 ) ) );
            // Debug.DrawRay( transform.position, movement.normalized );

            if ( min < 0.0f ) {

                if ( min == dot_1 ) {
                    transform.position = oldPosition + Vector3.forward * dot_1;
                    // Debug.DrawRay( transform.position, -Vector3.forward );
                } else if ( min == dot_2 ) {
                    transform.position = oldPosition - Vector3.forward * dot_2;
                    // Debug.DrawRay( transform.position, Vector3.forward );
                } else if ( min == dot_3 ) {
                    transform.position = oldPosition + Vector3.right * dot_3;
                    // Debug.DrawRay( transform.position, -Vector3.right );
                } else {
                    transform.position = oldPosition - Vector3.right * dot_4;
                    // Debug.DrawRay( transform.position, Vector3.right );
                }
            }

            if ( !bounds.Contains( new Vector2( transform.position.x, transform.position.z ) ) ) {
                transform.position = oldPosition;
            }
        }

        m_cameraRig.update( );
	}
}
