﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum EventType { AirPlatform = 0, Hole, Box, Spikes }

public class BottomLevelGenerator : Singleton<BottomLevelGenerator> {

	public List<GameObject> pickups;

	public List<GameObject> events;

	public List<int> probabilities; 
	private Queue<TerrainEvent> _events = new Queue<TerrainEvent>();
	public float speed;
	public delegate void ChangeVelocityHandler(float newVelocity);
	public ChangeVelocityHandler ChangeVelocity;
	public float spawnTime = 0.66f;
	public float zAxis = 0;
	public float topLimit;
	public float bottomLimit;
	public float xAxis;
	public float platformThickness = 0.5f;
	public float eventTime = 10f;
	private EventType _currentEventType;
	private int _currentProbability;
	private float _eventTime;
	private bool eventRunning = false;
	public float secondAirPlatformProb = 50;
    //private bool _eventFinished = true;
    //private float _time = 0;
    //private float _normalPlatformTime = 0;
	private TerrainEvent _currentEvent;
	private bool _initializeEvent = false;
	private GameObject _currentEventGameObject;

	public GameObject lastPlatform;
	public float magicNumber = 0.1f;
    

	void Update()
	{
		if (_currentEvent != null && eventRunning && _currentEvent.State == EventState.Finished)
			eventRunning = false;

		if (!eventRunning)
			_eventTime += Time.deltaTime;

		if (_eventTime >= eventTime)
		{
            //Debug.Log("Starting event");
			eventRunning = true;
			_initializeEvent = true;
			_eventTime = 0;
			AddEvent();

		}

		if (_initializeEvent)
		{
            //Debug.Log("initialize event");
			_initializeEvent = false;
			_currentEvent = _events.Dequeue() as TerrainEvent;
            //Debug.Log(_currentEvent.State);
			_currentEvent.State = EventState.Running;
			if (_currentEventType == EventType.Hole)
				_currentEvent.State = EventState.Finished;
			//_currentEvent.Execute();
            //Debug.Log(_currentEvent.State);
			if (_currentEventType != EventType.Box && _currentEventType != EventType.AirPlatform)
				lastPlatform = _currentEventGameObject;
		}



		if (!eventRunning)
		{
			/*if (_normalPlatformTime >= spawnTime)
			{
				BottomPlatformManager.Instance.SpawnPlatform(0, new Vector3(xAxis, bottomLimit, zAxis));
				_normalPlatformTime = 0;
			}
			_normalPlatformTime += Time.deltaTime;*/

			if (lastPlatform.transform.position.x + 2 * lastPlatform.collider.bounds.extents.x <= xAxis)
			{
				lastPlatform = BottomPlatformManager.Instance.SpawnPlatform(0, new Vector3(lastPlatform.transform.position.x + 2 * lastPlatform.collider.bounds.extents.x - magicNumber, bottomLimit, zAxis));
			}
		}
	}

	public void AddEvent()
	{
		_currentProbability = Random.Range (0, 100);
		for (int i = 0; i < probabilities.Count; i++)
		{
			if (probabilities[i] >= _currentProbability)
			{
				SpawnEvent((EventType)i);
				break;
			}

		}

	}

	public void SpawnEvent(EventType type)
	{
		if (type == EventType.AirPlatform)
		{
			float x = lastPlatform.transform.position.x + 2 * lastPlatform.collider.bounds.extents.x - magicNumber;
			float height = BottomPlatformManager.Instance.airDistance;
			_currentEventGameObject = BottomPlatformManager.Instance.SpawnPlatform(3, new Vector3(x, bottomLimit + height, zAxis));
			eventRunning = false;
			_initializeEvent = false;
			if (secondAirPlatformProb >= Random.Range(0, 100))
			{
				float nextPlatform = _currentEventGameObject.collider.bounds.extents.x * 2;
				Vector3 newPlatformPosition = new Vector3(x + nextPlatform, bottomLimit + 2 * height, zAxis);
				BottomPlatformManager.Instance.SpawnPlatform(3, new Vector3(x + nextPlatform, bottomLimit + 2 * height, zAxis));
				_eventTime = -5f;
				newPlatformPosition.y += 1.5f;
				newPlatformPosition.x += 4f;//2 * pickups[0].collider.bounds.extents.y;	
				Instantiate(pickups[Random.Range(0, pickups.Count)], newPlatformPosition, Quaternion.identity);
			}

		}
		if (type == EventType.Hole)
		{
			float x = lastPlatform.transform.position.x + 2 * lastPlatform.collider.bounds.extents.x - magicNumber;
			_currentEventGameObject = Instantiate(events[0], new Vector3(x ,bottomLimit, zAxis), Quaternion.identity) as GameObject;
			_events.Enqueue(_currentEventGameObject.GetComponent<TerrainEvent>());
			lastPlatform = _currentEventGameObject;    
			_currentEventType = EventType.Hole;
		}
		else if (type == EventType.Box)
		{
			float x = lastPlatform.transform.position.x + 2 * lastPlatform.collider.bounds.extents.x - magicNumber;
			float y = platformThickness + bottomLimit + events[(int) type].transform.localScale.x;
			_currentEventGameObject = Instantiate(events[1], new Vector3(x, y, zAxis), Quaternion.identity) as GameObject;
			_events.Enqueue(_currentEventGameObject.GetComponent<TerrainEvent>());
			eventRunning = false;
			_currentEventType = EventType.Box;
		}
	}
}