﻿using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

	public AudioClip music;
	public GameObject currentPlanet;
	public GameObject homePlanet;
	public BlackHole blackHole;
	public UfoControllScript player;
	public MapController mapController;
    public ScorePlotter scorePlotter;
    public GameObject planetPanel;

    [HideInInspector]
    public GameObject CurrentPlanet {get {return currentPlanet;}}

    [HideInInspector]
    public bool GameOver { get; private set; }

	private static GameController _instance = null;
    private bool _inTransit = false;
    private float _timeSinceUpdate;
    private Score _score;
    private PlanetScript _homePlanetScript;

	public static GameController getInstance() {
		return _instance;
	}

	// Use this for initialization
	void Awake () {
		_instance = this;
		MusicController.PlayMusic (music);
        mapController.OnPlanetChosen += MoveToPlanet;
        _timeSinceUpdate = 0;
        _score = new Score();
        GameOver = false;
        _homePlanetScript = homePlanet.GetComponent<PlanetScript>();
	}

    private void MoveToPlanet(GameObject chosenPlanet)
    {
        _inTransit = true;
        currentPlanet = chosenPlanet;
    }
	
	// Update is called once per frame
	void Update () {
        if (GameOver) return;

        if (Time.realtimeSinceStartup > 10 && _homePlanetScript.humans <= 0)
        {
            showGameOver();
        }
        else
        {
            handleTransit();
            handleScoreKeeping();
        }
	}

    private void showGameOver()
    {
        scorePlotter.Humans = _score.Humans;
        scorePlotter.Air= _score.Air;
        scorePlotter.Water = _score.Water;
        scorePlotter.Plants = _score.Plants;
        scorePlotter.Animals = _score.Animals;
        scorePlotter.Visible = true;
        GameOver = true;
        planetPanel.SetActive(false);
    }

    private void handleScoreKeeping()
    {
        _timeSinceUpdate += Time.deltaTime;
        if (_timeSinceUpdate >= 0.1)
        {
            _timeSinceUpdate = 0;
            _score.AddHumanCount(_homePlanetScript.humans);
            _score.AddAnimalsCount(_homePlanetScript.animals);
            _score.AddAirCount(_homePlanetScript.air);
            _score.AddPlantsCount(_homePlanetScript.plants);
            _score.AddWaterCount(_homePlanetScript.water);
        }
    }

    private void handleTransit()
    {
        if (!_inTransit)
        {
            mapController.Visible = Vector3.Distance(currentPlanet.transform.position, player.transform.position) > 20;
        }
        else
        {
            mapController.Visible = false;

            var deltaX = (currentPlanet.transform.position.x - player.transform.position.x) / 50f;
            var deltaY = (currentPlanet.transform.position.y - player.transform.position.y) / 50f;

            player.transform.position += new Vector3(deltaX, deltaY, 0);

            _inTransit = Vector3.Distance(currentPlanet.transform.position, player.transform.position) > 7;
        }
    }	
}
