using UnityEngine;
using System.Collections;


public class SourisBehaviour : MonoBehaviour {

    public Transform m_orientation = null;
    public float m_mouseMoveDuration = 0.75f;
    public float m_clickDuration = 0.25f;
    private Vector3 m_endPosition = Vector3.zero;
    private Vector3 m_startPosition = Vector3.zero;
    private int m_targetX = 0;
    private int m_targetY = 0;
    private Timer m_timer = new Timer( );
    private Timer m_pauseTimer = new Timer( );
    private Timer m_clickTimer = new Timer( );
    private Timer m_hideLastTimer = new Timer( );
    private MapData m_mapData = null;
    private float m_mouseZOffset = 5.0f;
    private float m_maxMagnitude = 1.0f;
    private float m_maxDistance = 1.0f;
    private float m_pauseDuration = 0.0f;
    private bool m_outOfField = false;
    private Vec2i m_lastWanted = new Vec2i( );
    public bool m_paused = true;

    void Start( ) {
        m_mapData = MapData.get( );
        m_pauseTimer.start( 0.001f );
        m_maxMagnitude = 40.0f * 40.0f + 40.0f * 40.0f;
        m_maxDistance = Mathf.Sqrt( 50.0f * 50.0f + 50.0f * 50.0f );
    }

    public void reset( ) {

        m_timer = new Timer( );
        m_pauseTimer = new Timer( );
        m_clickTimer = new Timer( );
        m_hideLastTimer = new Timer( );
        m_pauseDuration = 0.0f;
        m_outOfField = false;
        m_lastWanted = new Vec2i( );
        m_targetX = 0;
        m_targetY = 0;

        m_pauseTimer.start( 0.001f );
    }

	void Update( ) {

        if ( m_paused ) {
            return;
        }

        m_timer.step( );
        m_clickTimer.step( );
        m_pauseTimer.step( );
        m_hideLastTimer.step( );

        if ( m_timer.isRunning( ) ) {
            transform.position = Vector3.Lerp( m_startPosition, m_endPosition, m_timer.getPercentage( ) );
        }

        if ( m_timer.isEndedOnce( ) ) {

            m_pauseTimer.start( m_pauseDuration );
            m_clickTimer.start( m_clickDuration );
            m_startPosition = m_endPosition;
            m_endPosition = m_startPosition + new Vector3( 0.0f, -m_mouseZOffset );
            Audio.play( Audio.get( ).click_simple );
        }

        if ( m_clickTimer.isRunning( ) ) {

            Quaternion orientation = Quaternion.AngleAxis( 45.0f, m_orientation.right );
            float t = m_clickTimer.getPercentage( );

            if ( t < 0.5f ) {
                transform.position = Vector3.Lerp( m_startPosition, m_endPosition, t * 2.0f );
                m_orientation.rotation = Quaternion.Slerp( Quaternion.identity, orientation, t * 2.0f );
            } else {
                transform.position = Vector3.Lerp( m_endPosition, m_startPosition, ( t - 0.5f ) * 2.0f );
                m_orientation.rotation = Quaternion.Slerp( orientation, Quaternion.identity, ( t - 0.5f ) * 2.0f );
            }

        } else if ( m_clickTimer.isEndedOnce( ) ) {

            transform.position = m_startPosition;
            m_orientation.rotation = Quaternion.identity;
            Field field = m_mapData.getField( m_targetX, m_targetY );

            if ( !m_mapData.isFieldEnabled( m_targetX, m_targetY ) ) {

                m_mapData.m_enabledFields[ m_targetY * m_mapData.m_fieldCountX + m_targetX ] = true;
                field.m_wanted = ( CropType ) Random.Range( 1, 4 );
                field.appear( );
                Main.get( ).showWanted( field );
                m_lastWanted.x = m_targetX;
                m_lastWanted.y = m_targetY;

            } else {

                field.setBasketIcon( );
            }
        }

        if ( m_pauseTimer.isEndedOnce( ) ) {

            Vec2i v =  m_mapData.getClosestHarvestableField( transform.position );

            if ( v.x != -1 && v.y != -1 ) {
                setNewTarget( v.x, v.y );
                m_pauseDuration = 0.25f;
            } else {
                findNewTarget( );
            }
        }

        if ( m_hideLastTimer.isEndedOnce( ) ) {
            Main.get( ).hideWanted( m_mapData.getField( m_lastWanted.x, m_lastWanted.y ) );
        }

        if ( m_outOfField ) {

            if ( !m_hideLastTimer.isRunning( ) ) {
                m_hideLastTimer.start( 1.0f );
            }

            if ( !m_pauseTimer.isRunning( ) ) {
                m_pauseTimer.start( m_timer.m_duration );
            }
        }
    }

    public void findNewTarget( ) {

        if ( m_mapData.hasFreeField( ) ) {

            int randomX = Random.Range( 0, 5 );
            int randomY = Random.Range( 0, 5 );

            while ( m_mapData.isFieldEnabled( randomX, randomY )
                || ( randomX == m_mapData.m_houseX && randomY == m_mapData.m_houseY ) ){

                randomX = Random.Range( 0, 5 );
                randomY = Random.Range( 0, 5 );
            }

            setNewTarget( randomX, randomY );

        } else {

            m_outOfField = true;
        }
    }

    public void setNewTarget( int x, int y ) {

        m_targetX = x;
        m_targetY = y;
        m_startPosition = transform.position;
        m_endPosition = m_mapData.getFieldPosition( x, y ) + new Vector3( 0.0f, m_startPosition.y );

        float factor = ( m_endPosition - m_startPosition ).sqrMagnitude / m_maxMagnitude;
        m_pauseDuration = Random.Range( 3.0f, 5.0f ) + ( m_maxDistance / 6.0f ) * factor;
        m_timer.start( m_mouseMoveDuration );
    }
}
