using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameStage : MonoBehaviour {
	public static int ROW = 5;
	public static int COL = 8;

	private static Vector3 STAGE_OFFSET = new Vector3(-15, 0, 15);

#region PREFABS
	[SerializeField] GameTile _tilePrefab;

#endregion

	GameTile[] _tiles;


	// Use this for initialization
	void Start () {
		GameTile gt = null;
		this._tiles = new GameTile[ROW*COL];
		for (int i = 0; i < GameStage.ROW * GameStage.COL; i++)
		{
			int row = i / GameStage.COL;
			int col = i % GameStage.COL;
			gt = Instantiate(this._tilePrefab, this.transform);
			gt.row = row;
			gt.col = col;

			gt.transform.localPosition = new Vector3((col * 10), 0,  (row * -10)) + GameStage.STAGE_OFFSET;
			this._tiles[(row * COL) + col] = gt;


			gt.SetGameColor ((col < 4) ? GameColor.Blue : GameColor.Red);

			gt.name = "TILE "+col+","+row;
		}
	}

	public void ResetTiles()
	{
		foreach (GameTile tile in this._tiles)
		{
			tile.StopChanging();
			tile.SetGameColor((tile.col < 4) ? GameColor.Blue : GameColor.Red);
		}
	}

	public void StopChangingTiles()
	{		
		foreach (GameTile tile in this._tiles)
		{
			tile.StopChanging();
		}
	}

	public GameTile GetIndex(int col, int row)
	{
		int index = col + (row * COL);

		return GetIndex(index);
	}

	public GameTile GetIndex(int index)
	{
		if (index < 0 || index >= this._tiles.Length) return null;

		return this._tiles[index];
	}

	public void RemoveTile(int index)
	{
		if (index < 0 || index >= this._tiles.Length) return;
		
		this._tiles[index].SetToRemove();
		

	}

	
	public int indexOf(GameTile tile)
	{
		return System.Array.IndexOf(this._tiles, tile);
	}
	
}
