using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {
	
	public float movespeed = 2F;
	public float jumpspeed = 4F;
	public bool isdreaming = false;
	public bool ishurt = false;
	public bool haswon = false;
	public bool gameover = false;

	public Sprite[] playsprite = new Sprite[16];
	public SpriteRenderer cursprite;
	public bool transition = false;
	public int transtime = 150;
	public int idletime = 0;
	public int animstep = 0;
	public int hurtdelay = 0;
	public bool faceright = true;
	
	public bool sleepright = true;
	public GameObject sleepingbody;
	public FlashScript fs;

	public Sprite[] bricksprite = new Sprite[12];
	public Material[] backgrounds = new Material[6];

	public float energy = 1F;
	public Transform enbar;
	public float stress = 0F;
	public Transform stbar;
	public Renderer spotlight;

	public GameObject miasma;
	public GameObject shade;
	public GameObject poof;
	public GameObject reliever;
	public GameObject relieveget;
	public int levelleft, levelright;  //  Add relievers between these two points
	public Sprite shade1,shade2;

	public AudioSource mus1,mus2,mus3,mus4,mus5;
	public bool pm1 = true,pm2 = true,pm3 = false,pm4 = false,pm5 = false;
	public AudioClip[] sfx = new AudioClip[18];

	// Remove Relievers
	void RemoveRelievers () {
		GameObject[] allrev = GameObject.FindGameObjectsWithTag ("Reliever");
		foreach (GameObject onerev in allrev) {
			DestroyObject (onerev);
		}
	}

	// Add Relievers
	void AddRelievers () {
		int rx,ry,rcount = 0;
		do {
			rx = (int)((Random.value * (levelright - levelleft)) + levelleft);
			ry = (int)((Random.value * 9) - 1);
			if (Vector3.Distance(transform.position,new Vector3((float)rx,(float)ry,0)) > 2F) {
				rcount++;
				Instantiate (reliever,new Vector3((float)rx,(float)ry,-0.5F),Quaternion.identity);
			}
		} while (rcount < ((levelright - levelleft) / 2));
	}

	// Spikes!
	void OnCollisionEnter2D(Collision2D coll) {
		if ((coll.gameObject.tag == "Spikes") && (!gameover) && (!ishurt)) {
			ishurt = true;
			AudioSource.PlayClipAtPoint (sfx[(int)((Random.value * 5F) + 9)],transform.position);
		} else if (coll.gameObject.tag == "Reliever") {
			Instantiate (relieveget,coll.gameObject.transform.position,Quaternion.identity);
			AudioSource.PlayClipAtPoint (sfx[17],transform.position);
			DestroyObject (coll.gameObject);
			stress -= 0.005F;
			if (stress <= 0) {
				stress = 0;
			}
		}
	}

	// Use this for initialization
	void Start () {
		Collider2D[] colls = GetComponents<Collider2D>();
		foreach (Collider2D thiscoll in colls) {
			Physics2D.IgnoreCollision(sleepingbody.collider2D,thiscoll);
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (!gameover) {
			if ((!transition) && (!ishurt)) {
				// Check to see if player fell off the bottom
				if (transform.position.y <= -2.5F) {
					ishurt = true;
					AudioSource.PlayClipAtPoint (sfx[(int)((Random.value * 5F) + 9)],transform.position);
				} else {
					// Player movement (unless in mid-transition)
					float xvel;
					if ((isdreaming) || (energy > 0.2F)) {
						xvel = (movespeed * Input.GetAxis ("Horizontal"));
					} else {
						xvel = ((movespeed / 2F) * Input.GetAxis ("Horizontal"));
					}
					// Update facing based on direction just moved
					if (xvel < 0) {
						faceright = false;
					} else if (xvel > 0) {
						faceright = true;
					}
					float yvel = rigidbody2D.velocity.y;
					if (isdreaming) {
						yvel = (movespeed * Input.GetAxis ("Vertical"));
					} else if ((Input.GetButtonDown ("Jump")) && (yvel == 0) && (energy > 0.2F)) {
						yvel = jumpspeed;
						AudioSource.PlayClipAtPoint (sfx[18],transform.position);
						energy -= 0.025F;
						stress += 0.0025F;
						if (stress > 1F) {
							stress = 1F;
						}
					}
					rigidbody2D.velocity = new Vector2(xvel,yvel);
					// Update animations
					if (faceright) {
						cursprite.transform.localScale = new Vector3(3.5F,3.5F,1F);
					} else {
						cursprite.transform.localScale = new Vector3(-3.5F,3.5F,1F);
					}
					if (isdreaming) {
						if ((xvel == 0) && (yvel < (movespeed / 2))) {
							if (idletime > 135) {
								cursprite.sprite = playsprite[10];
							} else {
								cursprite.sprite = playsprite[8];
							}
						} else {
							cursprite.sprite = playsprite[9];
						}
					} else if (energy > 0.2F) {
						if ((xvel == 0) && (yvel == 0)) {
							if (idletime > 135) {
								cursprite.sprite = playsprite[2];
							} else {
								cursprite.sprite = playsprite[0];
							}
						} else if (yvel > 0) {
							cursprite.sprite = playsprite[1];
						} else {
							if (animstep > 10) {
								cursprite.sprite = playsprite[0];
							} else {
								cursprite.sprite = playsprite[1];
							}
						}
					} else {
						if ((xvel == 0) && (yvel == 0)) {
							if (idletime > 135) {
								cursprite.sprite = playsprite[6];
							} else {
								cursprite.sprite = playsprite[4];
							}
						} else if (yvel > 0) {
							cursprite.sprite = playsprite[5];
						} else {
							if (animstep > 10) {
								cursprite.sprite = playsprite[4];
							} else {
								cursprite.sprite = playsprite[5];
							}
						}
					}
					// Force wake up and force sleep
					if ((isdreaming) && (!transition) && (Vector3.Distance (sleepingbody.transform.position,transform.position) < 0.65F) && (Input.GetButtonDown ("Change"))) {
						stress += 0.1F;
						if (stress > 1F) {
							stress = 1F;
						}
						transition = true;
						AudioSource.PlayClipAtPoint (sfx[(int)((Random.value * 3F) + 14)],transform.position);
						fs.BeginFlash ();
						RemoveRelievers ();
						if (stress >= 1F) {
							pm1 = false;
							pm2 = false;
							pm3 = false;
							pm4 = false;
							pm5 = true;
						} else {
							pm1 = true;
							pm2 = true;
							pm3 = false;
							pm4 = false;
							pm5 = false;
						}
						transform.position = sleepingbody.transform.position;
						sleepingbody.transform.position = new Vector3(0,10F,0F);
						sleepingbody.rigidbody2D.isKinematic = true;
						if (sleepingbody.transform.localScale.x == 1) {
							faceright = true;
							cursprite.transform.localScale = new Vector3(3.5F,3.5F,1F);
						} else {
							faceright = false;
							cursprite.transform.localScale = new Vector3(-3.5F,3.5F,1F);
						}
					} else if ((!isdreaming) && (!transition) && (stress < 1F) && (Input.GetButtonDown ("Change"))) {
						stress += 0.1F;
						if (stress > 1F) {
							stress = 1F;
						}
						transition = true;
						AudioSource.PlayClipAtPoint (sfx[(int)((Random.value * 3F) + 14)],transform.position);
					}
				}
			} else {
				if (isdreaming) {
					rigidbody2D.velocity = new Vector2(0F,0F);
				} else {
					rigidbody2D.velocity = new Vector2(0F,rigidbody2D.velocity.y);
				}
				if (transtime < 50) {
					if (!isdreaming) {
						cursprite.sprite = playsprite[14];
					} else {
						cursprite.sprite = playsprite[4];
					}
				} else if (transtime < 100) {
					cursprite.sprite = playsprite[12];
				} else if (!isdreaming) {
					cursprite.sprite = playsprite[7];
				} else {
					cursprite.sprite = playsprite[13];
				}
			}
		}
		if (Input.GetKeyDown ("escape")) {
			Application.LoadLevel ("title");
		}
	}

	// OnGUI
	void OnGUI () {
		if (!gameover) {
			GUI.Box (new Rect(0,50,65,25),"ENERGY");
			GUI.Box (new Rect((Screen.width - 65),50,65,25),"STRESS");
		}
	}

	// FixedUpdate
	void FixedUpdate()
	{
		if (!gameover) {
			stbar.localScale = new Vector3((stress * 5F),1F,1F);
			if (((isdreaming) && (!transition)) || (stress >= 1F)) {
				spotlight.material.color = new Color(1F,1F,1F,stress);
			} else {
				spotlight.material.color = new Color(1F,1F,1F,0);
			}
			if (ishurt) {
				if (isdreaming) {
					rigidbody2D.velocity = new Vector2(0F,0F);
				} else {
					rigidbody2D.velocity = new Vector2(0F,rigidbody2D.velocity.y);
				}
				cursprite.sprite = playsprite[11];
				hurtdelay++;
				if (hurtdelay == 25) {
					hurtdelay = 0;
					stress += 0.2F;
					if (stress > 1F) {
						stress = 1F;
					}
					if (isdreaming) {
						transition = true;
						fs.BeginFlash ();
						RemoveRelievers ();
						if (stress >= 1F) {
							pm1 = false;
							pm2 = false;
							pm3 = false;
							pm4 = false;
							pm5 = true;
						} else {
							pm1 = true;
							pm2 = true;
							pm3 = false;
							pm4 = false;
							pm5 = false;
						}
						transform.position = sleepingbody.transform.position;
						sleepingbody.transform.position = new Vector3(0,10F,0F);
						sleepingbody.rigidbody2D.isKinematic = true;
						if (sleepingbody.transform.localScale.x == 1) {
							faceright = true;
							cursprite.transform.localScale = new Vector3(3.5F,3.5F,1F);
						} else {
							faceright = false;
							cursprite.transform.localScale = new Vector3(-3.5F,3.5F,1F);
						}
					} else {
						gameover = true;
						fs.whiteout = true;
						pm1 = false;
						pm2 = false;
						pm3 = false;
						pm4 = false;
						pm5 = false;
					}
					ishurt = false;
				}
			} else {
				if (transition) {
					// Waking up or falling asleep (no energy/stress change during transition)
					if (transtime > 0) {
						transtime -= 1;
					} else {
						transition = false;
						transtime = 150;
						if (isdreaming) {
							isdreaming = false;
						} else {
							sleepingbody.transform.position = transform.position;
							sleepingbody.rigidbody2D.isKinematic = false;
							if (faceright) {
								sleepingbody.transform.localScale = new Vector3(1F,1F,1F);
							} else {
								sleepingbody.transform.localScale = new Vector3(-1F,1F,1F);
							}
							isdreaming = true;
							fs.BeginFlash ();
							AddRelievers ();
							if (stress < 0.25F) {
								pm1 = true;
								pm2 = false;
								pm3 = true;
								pm4 = false;
								pm5 = false;
							} else if (stress < 0.75F) {
								pm1 = true;
								pm2 = false;
								pm3 = false;
								pm4 = true;
								pm5 = false;
							} else {
								pm1 = false;
								pm2 = false;
								pm3 = false;
								pm4 = false;
								pm5 = true;
							}
						}
					}
				} else {
					// Adjust step if necessary
					if (((rigidbody2D.velocity.x == 0) && (rigidbody2D.velocity.y == 0) && (!isdreaming)) || ((rigidbody2D.velocity.x == 0) && (rigidbody2D.velocity.y < (movespeed / 2F)) && (isdreaming))) {
						idletime++;
						if (idletime >= 150) {
							idletime = 0;
						}
					} else if ((!isdreaming) && (rigidbody2D.velocity.y == 0)) {
						animstep++;
						if (animstep >= 20) {
							animstep = 0;
						}
						idletime = 0;
					} else {
						idletime = 0;
						animstep = 0;
					}
					// Adjust energy
					if (!isdreaming) {
						// Drain energy as player is awake
						float drain = 0.0005F;
						energy -= drain;
						if (energy <= 0F) {
							// Fall asleep
							energy = 0F;
							transition = true;
							AudioSource.PlayClipAtPoint (sfx[(int)((Random.value * 3F) + 14)],transform.position);
						}
						// Build stress
						stress += drain / 10;
						if (stress > 1F) {
							stress = 1F;
						}
					} else {
						// Recover energy as player is asleep
						energy += 0.0005F;
						if (energy >= 1F) {
							// Wake up
							energy = 1F;
							if (stress < 0) {
								stress = 0;
							}
							transition = true;
							AudioSource.PlayClipAtPoint (sfx[(int)((Random.value * 3F) + 14)],transform.position);
							fs.BeginFlash ();
							RemoveRelievers ();
							if (stress >= 1F) {
								pm1 = false;
								pm2 = false;
								pm3 = false;
								pm4 = false;
								pm5 = true;
							} else {
								pm1 = true;
								pm2 = true;
								pm3 = false;
								pm4 = false;
								pm5 = false;
							}
							transform.position = sleepingbody.transform.position;
							sleepingbody.transform.position = new Vector3(0,10F,-1F);
							sleepingbody.rigidbody2D.isKinematic = true;
							if (sleepingbody.transform.localScale.x == 1) {
								faceright = true;
								cursprite.transform.localScale = new Vector3(3.5F,3.5F,1F);
							} else {
								faceright = false;
								cursprite.transform.localScale = new Vector3(-3.5F,3.5F,1F);
							}
						}
					}
					enbar.localScale = new Vector3((energy * 5F),1F,1F);
				}
			}
		} else if (haswon) {
			cursprite.sprite = playsprite[3];
			rigidbody2D.velocity = new Vector2(0F,rigidbody2D.velocity.y);
		} else {
			cursprite.sprite = playsprite[15];
			rigidbody2D.velocity = new Vector2(0F,rigidbody2D.velocity.y);
		}
		// Adjust music volumes
		if (pm1) {
			mus1.volume += 0.02F;
		} else {
			mus1.volume -= 0.02F;
		}
		if (pm2) {
			mus2.volume += 0.02F;
		} else {
			mus2.volume -= 0.02F;
		}
		if (pm3) {
			mus3.volume += 0.02F;
		} else {
			mus3.volume -= 0.02F;
		}
		if (pm4) {
			mus4.volume += 0.02F;
		} else {
			mus4.volume -= 0.02F;
		}
		if (pm5) {
			mus5.volume += 0.02F;
		} else {
			mus5.volume -= 0.02F;
		}
	}
}
