using UnityEngine;
using System.Collections;

public class MummyScript : MonoBehaviour {

	public float max_runspeed;
	public float current_speed;
	public float jump_force;
	bool facingRight = true; // for anims

	public bool onGround = false;
	public Transform groundCheck;
	float groundRadius = 0.2f;
	public LayerMask groundFilter;
	public LayerMask fireFilter;
	public LayerMask waterFilter;

	public bool onFire;
	public float fire_delay_timer;
	public bool wet;
	float wet_timer;
	float max_wet_timer;
	public float health;
	public float max_health;

	public Rigidbody2D rbREF;

	public GameObject firePSO;
	public GameObject smokePSO;
	public GameObject steamPSO;
	public GameObject wetPSO;

	ParticleSystem fire_PS;
	ParticleSystem smoke_PS;
	ParticleSystem steam_PS;
	ParticleSystem wet_PS;
	public float steamTimer;
	float max_steamTimer;

	public AudioClip steamSFX;
	public AudioClip splashSFX;
	public AudioClip fireSFX;
	public AudioClip jumpSFX;
	AudioSource[] AS_REF;

	GUIStyle GS;

	bool alive;
	float dead_fade_timer;

	// Use this for initialization
	void Start () 
	{

		GS = new GUIStyle ();
		GS.fontSize = 50;

	
		rbREF = GetComponent<Rigidbody2D>();
		AS_REF = GetComponents<AudioSource> ();
		AS_REF [1].clip = jumpSFX;
		jump_force = 1200.0f;
		max_runspeed = 15.0f;
		current_speed = 0.0f;
		steamTimer = 0.0f;
		max_steamTimer = 1.0f;
		wet_timer = 0.0f;
		max_wet_timer = 5.0f;
		fire_delay_timer = 0.0f;
		dead_fade_timer = 2.0f;
		wet = false;
		alive = true;
		health = max_health = 10.0f;


		fire_PS = firePSO.GetComponent<ParticleSystem> ();
		smoke_PS = smokePSO.GetComponent<ParticleSystem> ();
		steam_PS = steamPSO.GetComponent<ParticleSystem> ();
		wet_PS = wetPSO.GetComponent<ParticleSystem> ();

	}

	// Update is called once per frame
	void FixedUpdate () 
	{

		if (onFire)
		{
			max_runspeed = 15.0f;
			jump_force = 1500.0f;
			health -= Time.deltaTime;
		}
		else
		{
			max_runspeed = 8.0f;
			jump_force = 1200.0f;
		}

		onGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, groundFilter);

		if(Physics2D.OverlapCircle( new Vector2(rbREF.transform.position.x, rbREF.transform.position.y), 0.45f, waterFilter))
		{
			GetWet();
		}

		if(Physics2D.OverlapCircle( new Vector2(rbREF.transform.position.x, rbREF.transform.position.y), 0.45f, fireFilter))
		{
			TouchFire();
		}

		if(alive)
		{
			bool moving = false;
			if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) 
			{
				if (current_speed < max_runspeed) {
					if (onGround)
						current_speed += 1.5f;
					else
						current_speed += 0.5f;
				}
				moving=true;
			} 

			if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) 
			{
				if (current_speed > -max_runspeed) {
					if (onGround)
						current_speed -= 1.5f;
					else
						current_speed -= 0.5f;
				}
				moving = true;
			} 

			if(!moving) 
			{
				current_speed *= 0.8f;
			}



			if(facingRight && (current_speed > max_runspeed))
			{
				current_speed = max_runspeed;
			}

			if(!facingRight && (current_speed < -max_runspeed))
			{
				current_speed = -max_runspeed;
			}

			rbREF.velocity = new Vector2(current_speed, rbREF.velocity.y);
		}
		else
		{
			onFire = true;
			rbREF.velocity = new Vector2(0.0f, rbREF.velocity.y);
		}


		if(current_speed > 0.0f && !facingRight)
		{
			FlipSprite();
		}
		else if(current_speed < 0.0f && facingRight)
		{
			FlipSprite ();
		}

		if(onFire && !fire_PS.isPlaying)
		{
			fire_PS.Play();
			smoke_PS.Play();
		}

		if(!onFire && fire_PS.isPlaying)
		{
			fire_PS.Stop();
			smoke_PS.Stop();
		}

		if(wet && !wet_PS.isPlaying)
		{
			wet_PS.Play();
		}
		
		if(!wet && wet_PS.isPlaying)
		{
			wet_PS.Stop();
		}

		if(steamTimer > 0.0f && !steam_PS.isPlaying)
		{
			steam_PS.Play();
		}

		if(steamTimer <= 0.0f && steam_PS.isPlaying)
		{
			steam_PS.Stop();
		}

		if(wet_timer <= 0.0f)
		{
			wet_PS.Stop();
			wet = false;
		}

		if(Input.GetKeyDown (KeyCode.P))
		{
			Application.LoadLevel(Application.loadedLevel);
		}
	
	}

	void Update()
	{
		if(alive)
		{
			if((Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow)) && onGround)
			{
				//jump impulse
				AS_REF[1].Play ();
				GetComponent<Rigidbody2D>().AddForce(new Vector2(0.0f, jump_force));
			}
		}

		fire_delay_timer -= Time.deltaTime;
		steamTimer -= Time.deltaTime;
		wet_timer -= Time.deltaTime;

		if(health <= 0.0f)
		{
			alive = false;
			dead_fade_timer -= Time.deltaTime;

			if(dead_fade_timer <= 0.0f)
			{
				Application.LoadLevel(Application.loadedLevel);
			}
		}
	}


	void FlipSprite() 
	{
		facingRight = !facingRight;
		Vector3 spriteScale = transform.localScale;
		spriteScale.x *= -1;
		transform.localScale = spriteScale;
	}

	public void GetWet()
	{



		if (onFire)
		{
			onFire = false;
			steam_PS.Play ();
			steamTimer = max_steamTimer;

			AS_REF[0].Stop();
			AS_REF[0].loop = false;
			AS_REF[0].clip = steamSFX;
			AS_REF[0].Play ();
		}
		else if(!wet)
		{
			AS_REF[0].Stop();
			AS_REF[0].loop = false;
			AS_REF[0].clip = splashSFX;
			AS_REF[0].Play ();
			wet = true;
			wet_timer = max_wet_timer;
		}
		else if(wet)
		{
			wet = true;
			wet_timer = max_wet_timer;
		}
			
	}

	void TouchFire()
	{
		if(fire_delay_timer <= 0.0f)
		{
			if (!wet)
			{
				if(!onFire)
				{
					AS_REF[0].Stop();
					AS_REF[0].loop = true;
					AS_REF[0].clip = fireSFX;
					AS_REF[0].Play ();
				}

				onFire = true;			
			}
			else
			{
				wet = false;
				steamTimer = max_steamTimer;
				steam_PS.Play();
				fire_delay_timer = 1.0f;

				AS_REF[0].Stop();
				AS_REF[0].loop = false;
				AS_REF[0].clip = steamSFX;
				AS_REF[0].Play ();
			}

		}
	}

	void OnGUI()
	{
		if (wet) 
		{
			GUI.contentColor = new Color (0.0f, 0.0f, wet_timer / 10.0f);
		} 
		else if (onFire)
		{
			GUI.contentColor = Color.red;
		}
		else
		{
			GUI.contentColor = Color.green;
		}
		GUI.Label (new Rect (Screen.width/2 - 75, Screen.height - 50, 100, 50), "♥" + ((int)(health * 10.0f)).ToString(), GS);
		GUI.contentColor = Color.white;
	}
}
