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

public class BallS : MonoBehaviour {

	public const float velocityMult = 3f;
	public const float maxVelocity = 10;
	public const float maxArrowLen = maxVelocity / velocityMult;

	public Sprite wmelonSprite;
	public Sprite melonSprite;
	public Sprite wmelonBrokenSprite;
	public Sprite melonBrokenSprite;

	public static Vector3 startPosition;

	public CatcherS catcher;
	public Rigidbody2D rb2d;
	public LineRenderer lineRenderer;
	public BrokenBallS brokenBall;
	public StateHandlerS stateHandler;
	public string color;
	public bool thrown;

	void Awake () {
		rb2d = GetComponent<Rigidbody2D> ();
		lineRenderer = GetComponent<LineRenderer> ();
		lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
		lineRenderer.material.color = Color.gray;
	}

	void Start() {
		startPosition = transform.position;
	}

	void Update () {
		if (stateHandler.currentState == StateHandlerS.DURING_LEVEL) {
			if (!thrown) {
				Vector3 clickedPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
				Vector3 point2 = new Vector3 (clickedPoint.x, clickedPoint.y, -1);
				Vector3 point1 = new Vector3 (transform.position.x, transform.position.y, -1);
				if (point1.x > point2.x) {
					point2.x = point1.x;
				}
				if (Input.GetMouseButton (0)) {
					lineRenderer.enabled = true;
					drawArrow (point1, point2);
				} else if (Input.GetMouseButtonUp (0)) {
					rb2d.gravityScale = 1;
					rb2d.velocity = (point2 - point1) * velocityMult;
					if (rb2d.velocity.SqrMagnitude () > maxVelocity) {
						rb2d.velocity = Vector2.ClampMagnitude (rb2d.velocity, maxVelocity);
					}
					lineRenderer.enabled = false;
					thrown = true;
				} else {
					lineRenderer.enabled = false;
				}
			}
		}
	}

	void OnTriggerEnter2D(Collider2D col) {
		if (col.gameObject.tag == "GROUND" && !catcher.catched) {
			ColliderDistance2D colDist = col.Distance (GetComponent<CircleCollider2D>());;
			Vector2 collisionVector = colDist.pointB - colDist.pointA;
			bool wallHit = false;
			if (Mathf.Abs (collisionVector.x) > Mathf.Abs (collisionVector.y)) {
				wallHit = true;
			}
			GameObject brokenClone = Instantiate (brokenBall.gameObject);
			brokenClone.transform.position = transform.position;
			if (wallHit) {
				brokenClone.transform.Rotate (0, 0, 90);
			}
			transform.position = BallS.startPosition;
			if (color == "RED") {
				brokenClone.GetComponent<SpriteRenderer> ().sprite = wmelonBrokenSprite;
			} else if (color == "YELLOW") {
				brokenClone.GetComponent<SpriteRenderer> ().sprite = melonBrokenSprite;
			}
			rb2d.velocity = Vector2.zero;
			rb2d.angularVelocity = 0;
			rb2d.gravityScale = 0;
			setRandomColor ();
			thrown = false;
			stateHandler.crushCount++;
		}
	}

	public void setRandomColor() {
		if (Random.value < 0.5) {
			setColor ("RED");
		} else {
			setColor ("YELLOW");
		}
	}

	public void setColor(string color) {
		this.color = color;
		if (color == "RED") {
			GetComponent<SpriteRenderer> ().sprite = wmelonSprite;
		} else if (color == "YELLOW") {
			GetComponent<SpriteRenderer> ().sprite = melonSprite;
		}
	}

	private void drawArrow(Vector3 start, Vector3 end) {
		Vector3 arrowVector = end - start;
		Vector3 clampedArrowVector = Vector3.ClampMagnitude (arrowVector, maxArrowLen);
		end = start + clampedArrowVector;
		lineRenderer.SetPosition (0, start);
		lineRenderer.SetPosition (1, end);
	}

}
