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

public class PlayerController : MonoBehaviour {

	public GameObject gameController;
	public GameObject coconutSpawner;
	public GameObject playerInventory;
	public float minX;
	public float moveSpeed;
	public bool reverseInput;
	public float drunkTreshold;
	public AudioClip runningSound;
	public AudioClip drunkSound;
	public float coconutSpawnDelay;

	private bool gameOver = false;
	private bool holdingObject = false;
	private bool acceptInput = false;
	private bool isMoving = false;
	private bool spawningCoconut = false;
	private AudioSource audio;
	private float standingTime;

	void Start () {
		gameObject.GetComponent<Rigidbody2D>().freezeRotation = true;
		audio = gameObject.GetComponent<AudioSource> ();
	}

	void Awake() {
		if (PlayerPrefs.GetInt ("reverseControl") == 1)
			reverseInput = true;
		else
			reverseInput = false;
	}
	
	void Update () {
		playerInventory.transform.position = new Vector2( gameObject.transform.position.x, gameObject.transform.position.y + 0.9f);
		if(!gameOver && acceptInput) {
			// Process Input
			if(reverseInput) {
				if (Input.GetKey ("space") || Input.touchCount > 0) {
					if (gameObject.transform.position.x >= minX) {
						gameObject.transform.position = new Vector2 (gameObject.transform.position.x - moveSpeed, gameObject.transform.position.y);
						gameObject.transform.localScale = new Vector4 (-1,3,1);
						if (!audio.isPlaying) {
							audio.clip = runningSound;
							audio.Play ();
						}
					}
					if (isMoving && gameObject.transform.position.x < minX) {
						if (holdingObject) { 
							gameController.GetComponent<GameController> ().addScore (1);
							Destroy (playerInventory.transform.GetChild (0).gameObject);
							holdingObject = false;
						}
						isMoving = false;
						standingTime = Time.time;
						spawningCoconut = true;
						audio.Stop();
					}

					if (spawningCoconut) {
						if (Time.time - standingTime > coconutSpawnDelay) {
							coconutSpawner.GetComponent<CoconutSpawner> ().spawnCoconut ();
							spawningCoconut = false;
						}
					}
				} 
				else {
					gameObject.transform.position = new Vector2 (gameObject.transform.position.x + moveSpeed, gameObject.transform.position.y);
					gameObject.transform.localScale = new Vector4 (1,3,1);
					isMoving = true;
					spawningCoconut = false;
					if (!audio.isPlaying) {
						audio.clip = runningSound;
						audio.Play ();
					}
				}
			}
			else {
				if(Input.GetKey("space") || Input.touchCount > 0) {
					gameObject.transform.position = new Vector2(gameObject.transform.position.x + moveSpeed, gameObject.transform.position.y);
					gameObject.transform.localScale = new Vector4 (1,3,1);
					isMoving = true;
					spawningCoconut = false;
					if (!audio.isPlaying) {
						audio.clip = runningSound;
						audio.Play ();
					}
				}
				else if(gameObject.transform.position.x >= minX) {
					gameObject.transform.position = new Vector2(gameObject.transform.position.x - moveSpeed, gameObject.transform.position.y);
					gameObject.transform.localScale = new Vector4 (-1,3,1);
					if (!audio.isPlaying) {
						audio.clip = runningSound;
						audio.Play ();
					}
				}
				else {
					if (holdingObject) { 
						gameController.GetComponent<GameController> ().addScore (1);
						Destroy (playerInventory.transform.GetChild (0).gameObject);
						holdingObject = false;
					}
					if (isMoving) {
						isMoving = false;
						standingTime = Time.time;
						spawningCoconut = true;
					}

					if (spawningCoconut) {
						if (Time.time - standingTime > coconutSpawnDelay) {
							coconutSpawner.GetComponent<CoconutSpawner> ().spawnCoconut ();
							spawningCoconut = false;
						}
					}
					audio.Stop();
				}
			}
		}
	}

	void OnCollisionEnter2D (Collision2D col)
    {
		if(!gameOver) {
			// Check if player get object
			if(!holdingObject && col.gameObject.tag == "floatingObject")
			{
				holdingObject = true;
				col.gameObject.transform.SetParent(playerInventory.transform);
				col.gameObject.transform.localPosition = new Vector2(0,0.4f);
				col.gameObject.transform.rotation = Quaternion.identity;
				Destroy(col.gameObject.GetComponent<Rigidbody2D>());
			}

			if (col.gameObject.tag == "coconut") {
				gameOver = true;
				gameObject.transform.localScale = new Vector4 (1,3,1);

				audio.Stop ();
				// Freefall everything
				if(playerInventory.transform.childCount > 0) {
					playerInventory.transform.GetChild(0).gameObject.AddComponent<Rigidbody2D>();
					playerInventory.transform.GetChild(0).transform.parent = null;
				}
				gameObject.GetComponent<Rigidbody2D>().freezeRotation = false;
				gameObject.transform.Rotate (new Vector3 (0, 0, 20.0f));
			}
		}
    }

	void OnTriggerStay2D (Collider2D col) {
		if(!gameOver) {
			if(col.gameObject.tag == "water") {
				if (gameObject.transform.position.y - col.gameObject.transform.position.y < drunkTreshold) {
					gameOver = true;
					gameObject.transform.localScale = new Vector4 (-1,3,1);
					audio.Stop ();
					// Freefall everything
					if(playerInventory.transform.childCount > 0) {
						playerInventory.transform.GetChild(0).gameObject.AddComponent<Rigidbody2D>();
						playerInventory.transform.GetChild(0).transform.parent = null;
					}
					gameObject.GetComponent<Rigidbody2D>().freezeRotation = false;
					audio.PlayOneShot (drunkSound, 0.7f);
					
				}
			}
		}
	}

	public bool isGameOver() {
		return gameOver;
	}

	public void setGameOver(bool newVal) {
		gameOver = newVal;
		if (gameOver) {
			audio.Stop ();
		}
	}

	public void setAcceptInput(bool newVal) {
		acceptInput = newVal;
	}
}
