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

[RequireComponent(typeof(Player))]
public class PlayerHumanControls : MonoBehaviour {
	[SerializeField]private KeyCode _up = KeyCode.W;
	[SerializeField]private KeyCode _down = KeyCode.S;
	[SerializeField]private KeyCode _left = KeyCode.A;
	[SerializeField]private KeyCode _right = KeyCode.D;

	private KeyCode _attackKey;


	float _movementCooldown = 9999f;

	Player _player;

	public GameColor color 
	{
		get {return this._player.color;}
	}

	void Awake()
	{
		this._player = GetComponent<Player>();
	}

	void OnEnable()
	{
		this._player = GetComponent<Player>();
	}

	void Update()
	{
		this._movementCooldown -= Time.deltaTime;

		if (this._movementCooldown < 0)
		{

			if (Input.GetKeyDown(this._up))
			{
				GameManager.Instance.OnPlayerMove(this._player, Direction.UP);
				this._movementCooldown = this._player.movementCoolDownRate;
			} else if (Input.GetKeyDown(this._down))
			{
				GameManager.Instance.OnPlayerMove(this._player, Direction.DOWN);
				this._movementCooldown = this._player.movementCoolDownRate;
			} else if (Input.GetKeyDown(this._right))
			{
				GameManager.Instance.OnPlayerMove(this._player, Direction.RIGHT);
				this._movementCooldown = this._player.movementCoolDownRate;
			}
			else if (Input.GetKeyDown(this._left))
			{
				GameManager.Instance.OnPlayerMove(this._player, Direction.LEFT);
				this._movementCooldown = this._player.movementCoolDownRate;
			}
		}
		
		if (Input.GetKeyDown(this._attackKey))
		{
			//if can shoot
			this._player.OnPlayerShoot();
		}
	}

	public void SetControls(PlayerControls movementControls)
	{
		this._up = movementControls.up;
		this._down = movementControls.down;
		this._left = movementControls.left;
		this._right = movementControls.right;
		this._attackKey = movementControls.attack1;
	}

	public void StartControls()
	{
		this._movementCooldown = 0;
		this.enabled = true;
	}

	public void StopMoving()
	{
		this.enabled = false;
	}
	
}
