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

public class Player : Character {
	private float timer;
	private bool skipFrame = false;
	private float invulnerableTime = .65f;

	void Start() {
		base.Start();
		HealthBar.instance.UpdateHealth(maxHealth, health);
	}
	
	// Update is called once per frame
	void Update () {
		base.Update();
		
		if (isInvuln) {
			if (timer == 0) {
				timer = Time.time;
			}

			float timeDelta = Time.time - timer;
			Color tmp = GetComponent<SpriteRenderer>().color;

			if (!skipFrame) {
				if (tmp.a == 1f) {
					tmp.a = .4f;
				} else {
					tmp.a = 1f;
				}
				GetComponent<SpriteRenderer>().color = tmp;
			}

			skipFrame = !skipFrame;

			if (timeDelta >= invulnerableTime) {
				isInvuln = false;
				timer = 0;
				tmp.a = 1f;
				GetComponent<SpriteRenderer>().color = tmp;
			}
		}

		// get input
		if (!isRolling && !isDead) {
			lastPosition = transform.position;
			transform.position = GetUpdatedPosition(GetInputs());

			if (Input.GetButtonDown("Jump") && health > 1) {
				CoinThrow();
			}
		}
	}

	void CoinThrow() {
		// see if we need to spwan a new coin
		GameObject coin = null;
		GameObject[] _coins = GameObject.FindGameObjectsWithTag("Coin");
        foreach (GameObject _coin in _coins) {
            if ((!_coin.activeSelf || !_coin.GetComponent<Coin>().isActive) && !_coin.GetComponent<Coin>().isFlying) {
				coin = _coin;
            }
        }

		if (coin == null) {
			// spawn a new coin
			coin = GameManager.instance.AddCoin(transform.position);
		}

		coin.GetComponent<Coin>().ThrowMe(transform.position, GetMousePosition());

		health -= 1;
		HealthBar.instance.UpdateHealth(maxHealth, health);
	}

	override public void CoinPickup(GameObject coin) {
		Coin c = coin.GetComponent<Coin>();
		health += c.GetValue();
		HealthBar.instance.UpdateHealth(maxHealth, health);
		// play noise
		GameManager.instance.PlayCoinCollect();
		c.Hooray();
	}

	private Vector2 GetInputs() {
		Vector3 keyboard = GetKeyboardInput();
		Vector3 mouse = GetMousePosition();

		target = mouse;

		if (Input.GetMouseButtonDown(0)) {
			ThrowPunchLeft();
		}

		if (Input.GetMouseButtonDown(1)) {
			ThrowPunchRight();
		}

		return keyboard;
	}

	private Vector2 GetKeyboardInput() {
		return new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
	}

	private Vector3 GetMousePosition() {
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		return ray.origin;
	}

	private Vector3 GetUpdatedPosition(Vector2 _keyboard) {
		float x = transform.position.x + (_keyboard.x * movementSpeed * Time.deltaTime);
		float y = transform.position.y + (_keyboard.y * movementSpeed * Time.deltaTime);
		
		if (x < GameManager.instance.screenMin.x) {
			x = GameManager.instance.screenMin.x;
		} else if (x > GameManager.instance.screenMax.x) {
			x = GameManager.instance.screenMax.x;
		}
		
		if (y < GameManager.instance.screenMin.y) {
			y = GameManager.instance.screenMin.y;
		} else if (y > GameManager.instance.screenMax.y) {
			y = GameManager.instance.screenMax.y;
		}

		return new Vector3(x, y);
	}

	override public void YouDied() {
		GameManager.instance.PlayDeath();
		GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1;
		fistLeft.GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1 - 1;
		fistRight.GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1 - 1;

		animator.SetBool("isDead", true);
		fistLeft.GetComponent<Fist>().SetPosition("Dead");
		fistRight.GetComponent<Fist>().SetPosition("Dead");
		isDead = true;
		legs.SetActive(false);

		GameManager.instance.Lose();
	}
}
