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

public class Coin : MonoBehaviour {

	[SerializeField] int value = 1;
	[SerializeField] float flyTime = 1f;
	[SerializeField] AnimationCurve disappearCurve;
	[SerializeField] AnimationCurve flyCurve;
	[SerializeField] Transform shadow;

	public bool isActive = false;
	public bool isFlying = false;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1;
	}

	public int GetValue() {
		return value;
	}

	public void Hooray() {
		StartCoroutine(GetInMyWallet());
	}

	public IEnumerator GetInMyWallet() {
		isFlying = true;
		float startTime = Time.time;
		float timeDelta;
		Vector3 ogPosition = transform.position;
		Vector3 upPosition = new Vector3(ogPosition.x, ogPosition.y + .5f, ogPosition.z);
		isActive = false;
		GameManager.instance.SetCoins();
		shadow.gameObject.SetActive(false);
		GetComponent<Animator>().speed = 2;

		while (isFlying) {
			timeDelta = Time.time - startTime;
			if (timeDelta < flyTime) {
				timeDelta = timeDelta / flyTime;
			} else {
				timeDelta = 1;
			}

			Color tmp = GetComponent<SpriteRenderer>().color;
			tmp.a = 1 - (disappearCurve.Evaluate(timeDelta) * 10);
			GetComponent<SpriteRenderer>().color = tmp;

			float moveX = (upPosition.x - transform.position.x) * disappearCurve.Evaluate(timeDelta) + transform.position.x;
			float moveY = (upPosition.y - transform.position.y) * disappearCurve.Evaluate(timeDelta) + transform.position.y;
			Vector3 newLocation = new Vector3(moveX, moveY, transform.position.z);
			transform.position = newLocation;

			if (transform.position == upPosition) {
				isFlying = false;
			}

			yield return new WaitForEndOfFrame();
		}
		// gameObject.SetActive(false);
	}

	public void ThrowMe(Vector3 center, float radius) {
		transform.position = new Vector3(center.x, center.y, transform.position.z);
		bool redo = false;
		Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0,Screen.width), Random.Range(0,Screen.height), 0));
		while (!redo) {
			if (Mathf.Sqrt(Mathf.Pow(Mathf.Abs(screenPosition.x - center.x), 2) + Mathf.Pow(Mathf.Abs(screenPosition.y - center.y), 2)) < radius) {
				redo = false;
				screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0,Screen.width), Random.Range(0,Screen.height), 0));
			} else {
				redo = true;
			}
		}

		GetComponent<Animator>().speed = .75f;
		Coin c = GetComponent<Coin>();
		Color tmp = GetComponent<SpriteRenderer>().color;
		tmp.a = 1;
		GetComponent<SpriteRenderer>().color = tmp;

		StartCoroutine(Fly(new Vector3(screenPosition.x, screenPosition.y, transform.position.z)));
	}

	public void ThrowMe(Vector3 from, Vector3 to) {
		transform.position = new Vector3(from.x, from.y, transform.position.z);

		GetComponent<Animator>().speed = .75f;
		Coin c = GetComponent<Coin>();
		Color tmp = GetComponent<SpriteRenderer>().color;
		tmp.a = 1;
		GetComponent<SpriteRenderer>().color = tmp;

		StartCoroutine(Fly(new Vector3(to.x, to.y, transform.position.z)));
	}

	IEnumerator Fly(Vector3 position) {
		float startTime = Time.time;
		float timeDelta;
		float speed = Random.Range(.6f, 8f);
		isFlying = true;

		while (transform.position != position) {
			timeDelta = Time.time - startTime;
			if (timeDelta < speed) {
				timeDelta = timeDelta / speed;
			} else {
				timeDelta = 1;
			}

			// float moveX = (position.x - transform.position.x) * flyCurve.Evaluate(timeDelta) + transform.position.x;
			// float moveY = (position.y - transform.position.y) * flyCurve.Evaluate(timeDelta) + transform.position.y;
			float moveX = (position.x - transform.position.x) * timeDelta + transform.position.x;
			float moveY = (position.y - transform.position.y) * timeDelta + transform.position.y;
			Vector3 newLocation = new Vector3(moveX, moveY, transform.position.z);
			transform.position = newLocation;

			yield return new WaitForEndOfFrame();
		}

		isFlying = false;
		isActive = true;
		GameManager.instance.SetCoins();
	}

}
