using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GlobalScript : MonoBehaviour {

	public static GlobalScript __self;

	public static bool controlEnabled;
	public static bool planetaryMovementEnabled;
	public static bool orbiting;
	static string afterFade;
	public CanvasGroup fadePanel;
	static float fadeDuration;
	float fadeTimer;
	static float fadeDirection = 0;
	bool[] joinedPlanets;
	public GameObject player;
	public GameObject CeresPrefab;
	public GameObject obliqueTarget;
	public GameObject[] planets;
	public static bool finale;
	float acceleration = .1f;
	int finaleStage = 0;

	public static float attachDelay = 0f;


	// Use this for initialization
	void Start () {
		__self = this;
		afterFade = "";
		finale = false;//true;//
		joinedPlanets = new bool[9];
		joinedPlanets[0] = false;//m
		joinedPlanets[1] = false;//v
		joinedPlanets[2] = false;//e
		joinedPlanets[3] = false;//M
		joinedPlanets[4] = false;//p-always false
		joinedPlanets[5] = false;//j
		joinedPlanets[6] = false;//s
		joinedPlanets[7] = false;//u
		joinedPlanets[8] = false;//n
		controlEnabled = false;
		planetaryMovementEnabled = false;
		orbiting = false;
		fadePanel.alpha = 1f;
	}
	
	// Update is called once per frame
	void Update () {
		if(fadeDirection != 0f){
			//Debug.Log(fadeDirection);
			fadeTimer += Time.deltaTime;
			//fadePanel.alpha += Time.deltaTime * fadeDuration * fadeDirection;
			if(fadeDirection > 0.1f){
				fadePanel.alpha = fadeTimer / fadeDuration;
				if(fadeTimer >= fadeDuration){
					fadePanel.alpha = 1f;
					fadeDirection = 0f;
					fadeTimer = 0f;
					if(afterFade != "")
						SceneManager.LoadScene(afterFade);
				}
			}else if(fadeDirection < 0.1f){
				fadePanel.alpha = 1f - (fadeTimer / fadeDuration);
				if(fadeTimer >= fadeDuration){
					fadePanel.alpha = 0f;
					fadeDirection = 0f;
					fadeTimer = 0f;
					if(afterFade != "")
						SceneManager.LoadScene(afterFade);
				}
			}
		}

		if(attachDelay > 0f)
			attachDelay -= Time.deltaTime;

	}

	void LateUpdate(){
		if(fadePanel.alpha > 0f)
			return;
		if(finale){
			controlEnabled = false;
			if(finaleStage == 0){
				acceleration += Time.deltaTime * .1f;
				if(acceleration >= 1.0f)
					acceleration = 1.0f;
				Vector3 oldPos = player.transform.position;
				Vector3 newPos = Vector3.MoveTowards(oldPos, planets[4].transform.position, Time.deltaTime * 80f * acceleration);
				player.transform.LookAt(newPos);
				player.transform.position = newPos;
				if((player.transform.position - planets[4].transform.position).magnitude < planets[4].transform.lossyScale.x)
					finaleStage++;
			}else if(finaleStage == 1){
				GameObject go = Instantiate<GameObject>(CeresPrefab, planets[4].transform.position, Quaternion.identity);
				planets[4].SetActive(false);
				planets[4] = go;
				player.transform.FindChild("Pluto").localScale *= 0.5f;
				acceleration = .1f;
				finaleStage++;
			}else if(finaleStage == 2){
				acceleration += Time.deltaTime * .1f;
				if(acceleration >= 1.0f)
					acceleration = 1.0f;
				player.transform.position = Vector3.MoveTowards(player.transform.position, obliqueTarget.transform.position, Time.deltaTime * 80f * acceleration);
				player.transform.LookAt(planets[4].transform);
				if((player.transform.position - obliqueTarget.transform.position).magnitude < .5f){
					OrbitScript o = player.AddComponent<OrbitScript>();
					o.distance = player.transform.position.magnitude;
					o.rotateSpeed = 0f;
					o.speed = 2.6f;
					EndComplete();
					finaleStage++;
				}
			}else if(finaleStage == 3){
				player.transform.LookAt(Vector3.zero);
			}
		}
	}

	public static void FadeIn(float speed){
		fadeDirection = -1f;
		fadeDuration = speed;
	}

	public static void FadeOut(float speed){
		fadeDirection = 1f;
		fadeDuration = speed;
	}

	public static void EndShattered(){
		controlEnabled = false;
		afterFade = "ShatteredScene";
		FadeOut(5f);
	}

	public static void EndComplete(){
		controlEnabled = false;
		afterFade = "CompletedScene";
		FadeOut(10f);
	}

	public static void JoinPluto(int p){
		__self.planets[p].SetActive(false);
		__self.joinedPlanets[p] = true;
	}

	public static bool hasJoined(int planet){
		return __self.joinedPlanets[planet];
	}

	public static GameObject getPlayer(){
		return __self.player;
	}

	public static void Finale(){
		finale = true;
	}

}
