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

public class PlayerComputerLizerControls : MonoBehaviour {


	[SerializeField]float _timerResetTime = .5f;

	float _movementCooldown = 0;

	GameStage stage;

	Player _opponent;
	Player _player; 

	// Use this for initialization
	void Start () {		
		this.stage = GameManager.Instance.gameStage;
		this._movementCooldown = 1;
	}
	
	void OnEnable()
	{
	
		this._player = GetComponent<Player>();
		this.stage = GameManager.Instance.gameStage;
	}

	public void StartControls()
	{
		print (":D");
		this._movementCooldown = 1f;
		this._opponent = GameObject.Find("Left").GetComponent<Player>();
		this._player = GetComponent<Player>();
		this.stage = GameManager.Instance.gameStage;
	
	}

	void StopMoving()
	{
		enabled = false;
	}
		
	// Update is called once per frame
	void Update () {
		if (this._opponent == null)
		{
			GameObject left = GameObject.Find("Left");
			if (left) this._opponent = left.GetComponent<Player>();
		
			return;
		}

		this._movementCooldown -= Time.deltaTime;
		if (this._movementCooldown < 0)
		{			
			GameTile tile = this.stage.GetIndex(this._player.tileIndex);
			GameTile opponentTile = this.stage.GetIndex(this._opponent.tileIndex);
			GameTile checkTile = null;

			if (opponentTile.row == tile.row)
			{
				//fire!
				this._player.OnPlayerShoot();
				this._movementCooldown = this._player.shootRecharge;	

				if (tile.col != 7)
				{
					GameManager.Instance.OnPlayerMove(this._player, Direction.RIGHT);
					
				}			
			}
			else if (opponentTile.row < tile.row)
			{
				checkTile = this.stage.GetIndex(this._player.tileIndex+ (int)Direction.UP);
				if (checkTile != null && checkTile.gameColor == this._player.color)
				{
					GameManager.Instance.OnPlayerMove(this._player, Direction.UP);
					this._movementCooldown = this._timerResetTime;
				}	
				else
				{
					GameManager.Instance.OnPlayerMove(this._player, Direction.LEFT);
					this._movementCooldown = this._timerResetTime * .5f;
				}
			}
			else
			{
				GameManager.Instance.OnPlayerMove(this._player, Direction.DOWN);
				this._movementCooldown = this._timerResetTime;
			}
		}
	}

}
