using UnityEngine;
using System.Collections;

public class DrawManager : MonoBehaviour {

	public float m_fBlocSize;
	public static DrawManager m_tDrawManager;
	public Transform[] m_tGameModels;
	public Vector3 m_tWorldPosition;
	public Material[] m_tBlindMaterials;
	public Material[] m_tNormalMaterials;


	// Use this for initialization
	void Awake () {

		m_tDrawManager = this;
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void ManageBlockChangingColors(MainBoard Board)
	{
		for(int i = 0; i < MainBoard.m_iSize; i++)
		{
			GlobalVars.BrickType _BrickType = Board.GetBrickTypeByIndex(i);
			if(Board.m_eBoard[i].m_bTypeHasChanged)
			{
				if(_BrickType == GlobalVars.BrickType.EMPTY)
				{
//					Debug.Log ("dissapear");
					//Board.m_eBoard[i].m_Model.renderer.enabled = false;
					Board.m_eBoard[i].m_bTypeHasChanged = false;
				}
				else if(Board.m_eBoard[i].m_bDrawInitialized)
				{
					Board.m_eBoard[i].m_Model.renderer.enabled = true;
					Board.m_eBoard[i].m_bTypeHasChanged = false;
				}
			}
		}
	}

	public void CheckUninitializedBlocks(MainBoard Board)
	{
		for(int i = 0; i < MainBoard.m_iSize; i++)
		{
			GlobalVars.BrickType _BrickType = Board.GetBrickTypeByIndex(i);
			Vector2 _tPos = Board.m_eBoard[i].GetPos();
			if( _BrickType != GlobalVars.BrickType.INVALID && _BrickType != GlobalVars.BrickType.EMPTY && !Board.m_eBoard[i].GetInitialized())
			{
				Transform _Instance = (Transform) Instantiate( m_tDrawManager.m_tGameModels[ (int)_BrickType ],new Vector3( _tPos.x * m_tDrawManager.m_fBlocSize, _tPos.y * m_tDrawManager.m_fBlocSize,0 ) + m_tDrawManager.m_tWorldPosition, Quaternion.identity ) as Transform;
				_Instance.gameObject.name = "brique_" + i ;
				Board.m_eBoard[i].SetModel(_Instance);
			}
			Debug.Log("INIT" + i);
		}
	}

/*	public static void UpdateBlocksPos(MainBoard Board)
	{
		for(int i = 0; i < MainBoard.m_iSize; i++)
		{
			if(Board.m_eBoard[i] != null && Board.m_eBoard[i].m_Model != null)
			{
				Board.m_eBoard[i].m_Model.position = new Vector3( Board.m_eBoard[i].m_Pos.x * m_tDrawManager.m_fBlocSize, Board.m_eBoard[i].m_Pos.y * m_tDrawManager.m_fBlocSize,0 ) + m_tDrawManager.m_tWorldPosition;
			}
		}
	}
	*/

	public static void DrawBoard( MainBoard tGameBoard )
	{
		m_tDrawManager.CheckUninitializedBlocks(tGameBoard);
		//draw game  bord
//		for (int i = 0; i < tGameBoard.m_iSize; i++)
//		{
//			GlobalVars.BrickType _BrickType = tGameBoard.GetBrickTypeByIndex(i);
/*			if( _BrickType != GlobalVars.BrickType.INVALID && _BrickType != GlobalVars.BrickType.EMPTY )
			{
				Vec2 _tPos = MainBoard.GetPosByIndex( i );
				Instantiate( m_tDrawManager.m_tGameModels[ (int)_BrickType ],new Vector3( _tPos.x * m_tDrawManager.m_fBlocSize, _tPos.y * m_tDrawManager.m_fBlocSize,0 ) + m_tDrawManager.m_tWorldPosition, Quaternion.identity );
				//Debug.Log((int)_BrickType);				
				//Debug.Log(_BrickType);				
				Debug.Log(_tPos.x +" " + _tPos.y);
			
			}
			else
				Debug.Log("skip" + _BrickType);
			
*/
//		}

	}


}
