using UnityEngine;
using System.Collections;

public class Difficulty : MonoBehaviour {

	private static Difficulty Instance;
	private static int Diff = 0;

	private static bool IncDiff = false;

	public static Difficulty GetInstance(){
		if ( Instance == null ) {
			Instance = GameObject.FindObjectOfType<Difficulty>();
			DontDestroyOnLoad( Instance.gameObject );
		}
		return Instance;
	}

	// Use this for initialization
	void Awake () {
		if ( Instance == null ) {
			Instance = this;
			DontDestroyOnLoad(this);
		} else {
			if ( this != Instance ) {
				Destroy(this);
			}
		}
	}

	void OnLevelWasLoaded(int level) {
		if ( Instance == this ) {
			if ( IncDiff ) {
				Diff ++;
				IncDiff = false;
			}
			SetupDiff(Diff);
		}
	}


	public static int Value(){
		return Diff;
	}

	public static void Increment(){
		IncDiff = true;
	}


	void SetupDiff( int D ) {
		Debug.Log("Difficulty now: " + D);

		if ( D > 0 ) {
			GameObject Ship = GameObject.Find("Ship");
			Vector3 InitialOffset = new Vector3( Random.Range( -.1f, .1f ), Random.Range( -.1f, .1f ), Random.Range( -.1f, .1f ) );
			Ship.rigidbody.AddForce( Random.Range( -10f, 10f ), Random.Range( -10f, 10f ), Random.Range( -10f, 10f ) );
			Ship.rigidbody.MovePosition( Ship.transform.position + InitialOffset );
			GameObject.Find("Ship-Ring").transform.position += InitialOffset;

			GameObject Station = GameObject.Find("Space Station");
			switch( D ) {
			case 1:
				Station.transform.position += new Vector3( 20f, 0f, 0f );
				break;
			case 2:
				Station.transform.position += new Vector3( 30f, Random.Range( -3f, 3f ), Random.Range( -3f, 3f ) );
				Station.rigidbody.AddTorque( Station.transform.up*1300f, ForceMode.Impulse );
				break;
			case 3:
				Station.transform.position += new Vector3( Random.Range( -3f, 3f )-5f, Random.Range( -3f, 3f ), -60f );
				Station.rigidbody.AddForce( Station.transform.right*-300f, ForceMode.Impulse );
				Station.rigidbody.AddForce( Station.transform.forward*Random.Range( -20f, 20f ), ForceMode.Impulse );
				Station.rigidbody.AddTorque( Station.transform.up*13000f, ForceMode.Impulse );
				break;
			default:
				Diff = 0;
				SetupDiff (0);
				break;
			}

		}
	}

}
