using UnityEngine;
using System.Collections;

public class Block 
{
	public Vector2 m_Pos;
	public Vector2 m_TargetPos;

	public bool m_bDrawInitialized = false;
	public bool m_bIsMoving = false;
	public Transform m_Model;

	GlobalVars.BrickType m_eType;
	public GlobalVars.BrickType m_eVirtualType;

	public bool m_bHaveBeenChecked;
	public bool m_bTypeHasChanged;

	public bool m_bHaveLanded;
	public bool m_bHaveReacheNearTarget;



	public void Init(Vector2 Pos, GlobalVars.BrickType Type )
	{
		m_Pos = Pos;
		m_TargetPos = m_Pos;
		m_eType = Type;

		//store virtual type
		if( Type == GlobalVars.BrickType.EMPTY )
			m_eVirtualType = GlobalVars.BrickType.EMPTY;
		else if( Type == GlobalVars.BrickType.BLUE || Type == GlobalVars.BrickType.RED )
			m_eVirtualType = GlobalVars.BrickType.BLACK;
		else
			m_eVirtualType = GlobalVars.BrickType.WHITE;

		Debug.Log("creation : " + m_eVirtualType );

		m_bDrawInitialized = false;
		m_bHaveBeenChecked = false;
		m_bTypeHasChanged = false;
		m_bHaveLanded = false;
		m_bHaveReacheNearTarget = true;	
	}

	public void SetType(GlobalVars.BrickType Type)
	{
		m_eType = Type;
		m_bTypeHasChanged = true;
	}

	public void SetModel( Transform Model )
	{
		m_Model = Model;
		m_bDrawInitialized = true;

		//randomly choose orient
		if( m_Model )
		{
			int rotY = Random.Range(0,3); 
			Debug.Log( rotY + " - " + rotY*90 );
			this.m_Model.transform.eulerAngles = new Vector3( 0,rotY*90,0);
		}
	}


	public bool GetInitialized()
	{
		return this.m_bDrawInitialized;
	}

	public Vector2 GetPos()
	{
		return this.m_Pos;
	}

	public GlobalVars.BrickType GetBlockType()
	{
		/*if( MainBoard.m_bColorBlindActive )
		{
			Debug.Log(m_eVirtualType + " " + m_Pos);
			return m_eVirtualType;
		}*/

		return m_eType;
	}

	public void Update( float fMovmentSpeed, float fFallSpeed )
	{
	
		if( GameStateManager.m_Instance.m_eGameState == GlobalVars.GAME_STATE.GAME_OVER )
			return;

		Vector2 _nextPos = new Vector2( m_Pos.x, m_Pos.y );
		if(!m_bHaveLanded)
		{
	//		fFallSpeed = fFallSpeed/10;
		}
		_nextPos = Vector2.Lerp(m_Pos, m_TargetPos, fMovmentSpeed * Time.deltaTime);


		if(  this.m_eType == GlobalVars.BrickType.EMPTY )
		{	
			//m_Pos = m_TargetPos;
			//m_Model.position = new Vector3( m_Pos.x, m_Pos.y,0 ) +  DrawManager.m_tDrawManager.m_tWorldPosition;
			m_bIsMoving = false;
			m_bHaveLanded = true;
			return;
		}


		if( (m_TargetPos.x - m_Pos.x) * (m_TargetPos.x - m_Pos.x) < 0.005 )
		{
			//move y
			if( (m_TargetPos.y - m_Pos.y) * (m_TargetPos.y - m_Pos.y) > 0.005 && m_TargetPos.y - m_Pos.y < 0)
			{
				_nextPos.y = m_Pos.y - fFallSpeed * Time.deltaTime;				
			}
			else
			{
				_nextPos.y = m_TargetPos.y;
			}

			_nextPos.x = m_TargetPos.x;
		}
		else
			_nextPos.y = m_Pos.y;

		m_Pos = _nextPos;

		if( (m_TargetPos - m_Pos).sqrMagnitude < 0.005 )
		{
			m_bHaveLanded = true;
			m_bIsMoving = false;
			m_Pos= m_TargetPos;
			m_bHaveReacheNearTarget = true;
		}
		else
		{
			m_bIsMoving = true;
			m_bHaveLanded = false;
			//Debug.Log(" keep moving " +  (m_TargetPos - m_Pos).sqrMagnitude);
			//Debug.Log( m_TargetPos + " " + m_Pos + this.m_eType);
		}




		/*m_Pos = Vector2.Lerp(m_Pos, m_TargetPos, fMovmentSpeed * Time.deltaTime);		
		if( (m_TargetPos - m_Pos).sqrMagnitude < 0.1 )
		{
			m_Pos = m_TargetPos;
			m_bIsMoving = false;
			//m_Pos.y = m_TargetPos.y;

		}else
		{
			m_bIsMoving = true;
		}*/
		if(m_Model != null)
		{
			m_Model.position = new Vector3( m_Pos.x, m_Pos.y,0 ) +  DrawManager.m_tDrawManager.m_tWorldPosition;
		}
	}
}
