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

public class Player : MonoBehaviour {
	public GameObject shot;

	[SerializeField]private Health _health;

	[SerializeField]float _movementCoolDownRate = .5f;

	public float movementCoolDownRate
	{
		get {return this._movementCoolDownRate;}
	}

	[SerializeField]GameColor _color;

	[SerializeField]Material[] _characterMaterials;
	[SerializeField]Material[] _shotMaterials;

	[SerializeField]Renderer _meshRenderer;

	[SerializeField]float _shotRechargeRate;

	[SerializeField]Transform _shotStartingLocation;

	private float _shootRecharge;

	public int tileIndex;

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

	public float shootRecharge
	{
		get {return this._shootRecharge;}
	}

	public void SetColor(GameColor newColor)
	{
		this._color = newColor;

		Material newMaterial = this._characterMaterials[0];
		switch (newColor)
		{
			case GameColor.Red:
				newMaterial = this._characterMaterials[1];
				break;
			case GameColor.Blue:
				newMaterial = this._characterMaterials[2];
				break;
			case GameColor.None:
			default:
				newMaterial = this._characterMaterials[0];
				return;
		}

		if (this._meshRenderer== null) this._meshRenderer = GetComponentInChildren<MeshRenderer>();
		if (this._meshRenderer != null)
		{
			//This below is really sloppy and doesn't allow for multi material meshes
			// - Troy's sorry
			this._meshRenderer.material = newMaterial;			
		}

	}

	public void SetControls(PlayerControls newControls)
	{
		PlayerHumanControls pm = GetComponent<PlayerHumanControls>();
		if (pm != null) pm.SetControls(newControls);
	}

	public void OnPlayerShoot()
	{
		if (this._shootRecharge > 0) return;

		GameObject newShotObject = Instantiate(this.shot, this.transform.parent);
		
		if (_shotStartingLocation)
		{
			newShotObject.transform.position = this._shotStartingLocation.position;
		}
		else
		{

		newShotObject.transform.position = this.transform.position + (this.transform.up * 5) + (this.transform.forward * 5f);
		}

		newShotObject.transform.rotation = this.transform.rotation;

		Shot newShot = newShotObject.GetComponent<Shot>();
		newShot.color = this._color;
		newShot.forward = this.transform.forward;

		this._shootRecharge = newShot.rechargeSpeed;
	}

	public void Damage(int damageValue)
	{
		this._health.Damage(damageValue);

		if (this._health.currentHealth <= 0)
		{
			if (GetComponent<PlayerHumanControls>())
			{
				GameManager.Instance.OnPlayerDeath(this);
			}
			else
			{
				GameManager.Instance.OnEnemyDeath(this);
			}
			this.gameObject.SetActive(false);
		}

		//Do blink effect

	}

	void OnTriggerEnter(Collider other)
	{
		switch (other.tag.ToString().ToLower())
		{
			case "hazard":
				if (other.gameObject.GetComponent<Shot>().color != this._color)
				{
					Damage(other.gameObject.GetComponent<Shot>().damage);				
					Destroy(other.gameObject);
				}
				break;
		}
	}

	public void SetProfile(CharacterProfile newProfile)
	{
		if (!this._health) this._health = this.gameObject.AddComponent<Health>();
		this._health.healthBar = newProfile.healthBar;
	}

	public void SetLocation(int col, int row)
	{
		this.tileIndex = col + (row * GameStage.COL);
	}

	public void SetLocation(int startingIndex)
	{
		this.tileIndex = startingIndex;
	}

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


}
