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

public class Ball : MonoBehaviour {

	public List<BaseStatAddOn> baseStatAddOns = new List<BaseStatAddOn>();

	[SerializeField]
	Transform face;

	[SerializeField]
	Material material;

	[SerializeField]
	AudioClip[] inGameSoundEffects;

	[SerializeField]
	ColliderManager colliderManager;

	[SerializeField]
	public Color[] choiceOfColours;

	[SerializeField]
	SpriteRenderer spriteRenderer;

	[SerializeField]
	FaceChanger faceChanger;
	
	public void OnHit(Vector2 position, float strength)
	{
		//Vector2 direction = new Vector2( transform.position.x, transform.position.y)  -  new Vector2(position.x, position.y);

		MoveBallInDirection(Vector2.up, position, strength);

		for(int i = 0; i < baseStatAddOns.Count; i++)
		{
			baseStatAddOns[i].OnTap();
		}

		int randomNumber = Random.Range(0, inGameSoundEffects.Length);

		AudioSource.PlayClipAtPoint(inGameSoundEffects[randomNumber], Vector3.zero);
	}

	void DestroyAddOns()
	{
		for(int i = 0; i <baseStatAddOns.Count; i++)
		{
			Destroy(baseStatAddOns[i]);
		}

		baseStatAddOns.Clear();
	}

	public void MoveBallInDirection(Vector2 direction, Vector2 position, float strength)
	{
		StartCoroutine(MoveBallInDirectionCoroutine(direction, position, strength));
	}

	public IEnumerator MoveBallInDirectionCoroutine(Vector2 direction, Vector2 position, float strength)
	{
		yield return new WaitForFixedUpdate();
		rigidbody2D.AddForceAtPosition(direction * strength , position, ForceMode2D.Impulse);
	}
	
	public void ScaleBall(float minimumBallSize, float maximumBallSize)
	{
		float randomScale = Random.Range(minimumBallSize, maximumBallSize);
		
		//activeBall.ScaleBall(randomScale);

		transform.localScale = new Vector3(randomScale, randomScale);

		float newScaleForFace = 1 / randomScale;

		face.transform.localScale = new Vector3(newScaleForFace, newScaleForFace);


	}

	public void MoveFace(Vector3 offset)
	{
		face.transform.localPosition = Vector3.zero + offset;
	}

	public void SetAlphaMaterial(Texture2D alphaTex)
	{
		material.SetTexture("_AlphaTex", alphaTex);
	}

	public void RotateBall(float strength)
	{
		rigidbody2D.angularVelocity = rigidbody2D.angularVelocity + strength;
	}

	public Collider2D ChangeCollider(string colliderName)
	{
		return colliderManager.SetCollider(colliderName);
	}

	public Color ChooseColor()
	{
		int randomNumber = Random.Range(0, choiceOfColours.Length);
		spriteRenderer.color = choiceOfColours[randomNumber];
		return spriteRenderer.color;
	}

	public void SetSprite(Sprite sprite)
	{
		spriteRenderer.sprite = sprite;
	}

	public void SetColor(Color colour)
	{
		spriteRenderer.color = colour;
	}

	public Sprite GetFaceSprite()
	{
		return faceChanger.GetFace();
	}

	public void SetFaceSprite(Sprite faceSprite)
	{
		faceChanger.SetFace(faceSprite);
	}

	public void ChangeFace()
	{
		faceChanger.ChangeFace();
	}

}
