﻿using UnityEngine;
using System.Collections;

public class GravitySwitch : MonoBehaviour
{
	
	public float GravityStrength;
	float momentum;
	float click;
	public bool GameState;
	bool GravityState;
	public GameObject Explosion;
	public GameObject[] Jets;
	public GameObject[] CharacterPieces;
	public Vector3 hitposition;
	public GameObject EndScreen;
	public UILabel BestScore;

	void Start()
	{
		GravityStrength = 0;
		BestScore.text = "Best:" + PlayerPrefs.GetFloat("BestScore").ToString();
		GameState = true;
	}
	void Update ()
	{
		Gravity();
		Controls();
		if (GameState == false)
		{
			transform.position = hitposition;
			EndScreen.SetActive(true);
		}
	}

	void Gravity()
	{
		//transform.Translate(new Vector3(3,0) * Time.deltaTime);
		switch (GravityState)
		{
		case true: Physics2D.gravity = new Vector2(momentum,GravityStrength); /*transform.localEulerAngles = new Vector2(-180, 0);*/ break;
		case false: Physics2D.gravity = new Vector2(momentum,-GravityStrength); /*transform.localEulerAngles = new Vector2(0, 0);*/ break;
		}
		if (GravityStrength == 0)
		{
			transform.position = new Vector3(0,-5.5f,0);
		}

	}
	void Controls()
	{
		if (Input.GetKeyDown(KeyCode.Space))
		{
			GravityState = !GravityState;
		}
		if (Input.GetMouseButtonDown(0))
		{
			GravityState = !GravityState;
			if (click == 0)
			{
				momentum = 2;
				GravityStrength = 27;
				foreach(GameObject jet in Jets)
				{
					jet.SetActive(true);
				}
			}
			click+=1;
		}
	}
	void OnCollisionEnter2D(Collision2D other)
	{
		//Application.LoadLevel(Application.loadedLevel);
		if (GameState == true)
		{
			foreach (ContactPoint2D contact in other.contacts)
			{
				GameObject Hit = Instantiate (Explosion, contact.point, Quaternion.identity) as GameObject;
				hitposition = Hit.transform.position;
				GameState = false;

			}
			foreach (GameObject self in CharacterPieces)
			{
				self.SetActive(false);
			}
		}
	}

}
