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

namespace RollingRockRumble {
    public class GameHandler : MonoBehaviour {

        public List<Transform> listSpawner = new List<Transform>();
        public GameObject prefabCreature;
        public GameObject objectCreatureList;
        public GameObject objectGUIGameOver;
        public GameObject objectGUIGameInProgress;

        public GameObject objectBoulder;

        public GameObject prefabDATA;
        public DataHandler dataHandler;

        // TODO : change with difficulty settings
        private float spawnRate = 5f;
        private float spawnRateEvolutionFactor = 0.988f;
        private float nextSpawn = 0f;
        private float nextSpawnCalculation = 0f;

        private int numberPerSpawn = 1;

        public bool pauseGame = false;
        public float pauseDuration = 0f;

        public Text GUIScoreValue;
        public Text GUITimeValue;

        public Text GUIEndScoreValue;
        public Text GUIEndTimeValue;
        public Text GUIEndBestScoreValue;
        public Text GUIEndBestTimeValue;

        public float gameStartTime;


        public int numberCreaturesKilled = 0;

        private bool isDebugModeActivated;

        public float optionVolume = 0.1f;



        void Start() {
            isDebugModeActivated = false;
            GameObject tempDATA;
            gameStartTime = Time.time;
            if (!GameObject.Find("DATA")) {
                tempDATA = Instantiate(prefabDATA, Vector3.zero, Quaternion.identity) as GameObject;
                tempDATA.name = "DATA";
            }
            else {
                tempDATA = GameObject.Find("DATA");
            }
            dataHandler = tempDATA.GetComponent<DataHandler>();
        }



        void Update() {
            if (!pauseGame) {
                if (nextSpawnCalculation + gameStartTime + pauseDuration < Time.time) {
                    //this defines a sort of modulo to define when the next spawn time should be calculated,
                    //and updates the value of the random used
                    spawnRate *= spawnRateEvolutionFactor;
                    nextSpawnCalculation += spawnRate;
                }
                if (Application.loadedLevelName == "game") {
                    if (nextSpawn + gameStartTime + pauseDuration < Time.time) {
                        //select a spawner for the creature to spawn on
                        //The spawner must be far from the player
                        Vector3 randomSpawnerPosition;
                        do{
                            randomSpawnerPosition = listSpawner[Mathf.FloorToInt(Random.value * 0.999999999f * listSpawner.Count)].position;
                        } while (Vector3.Distance(randomSpawnerPosition, objectBoulder.transform.position) < 9f);
                        randomSpawnerPosition += new Vector3(0, 1.5f, 0);
                        //instantiate creature
                        GameObject tempCreature = Instantiate(prefabCreature, randomSpawnerPosition, Quaternion.identity) as GameObject;
                        tempCreature.name = "Creature";
                        tempCreature.transform.parent = objectCreatureList.transform;
                        nextSpawn = pauseDuration + nextSpawnCalculation + Random.value * spawnRate;
                    }
                    //TODO : remove DEBUG tools
                    if (isDebugModeActivated) {
                        if (Input.GetMouseButtonDown(0)) {
                            //select a spawner for the creature to spawn on
                            Vector3 randomSpawnerPosition = listSpawner[Mathf.FloorToInt(Random.value * 0.999999999f * listSpawner.Count)].position;
                            randomSpawnerPosition += new Vector3(0, 1.5f, 0);
                            //instantiate creature
                            GameObject tempCreature = Instantiate(prefabCreature, randomSpawnerPosition, Quaternion.identity) as GameObject;
                            tempCreature.name = "Creature";
                            tempCreature.transform.parent = objectCreatureList.transform;
                            tempCreature.GetComponent<CreatureHandler>().creatureLevel = 4;
                        }
                        if (Input.GetMouseButtonDown(1)) {
                            objectBoulder.GetComponent<RockHandler>().currentLife -= 0.2f;
                        }
                    }
                    GUITimeValue.text = Mathf.FloorToInt(Time.time - gameStartTime).ToString();
                    GUIScoreValue.text = numberCreaturesKilled.ToString();
                }
            }
            else {
                //game is paused
                pauseDuration += Time.deltaTime;
            }
        }


        public void GameOver() {
            objectGUIGameInProgress.SetActive(false);

            int tempCurrentTime = Mathf.FloorToInt(Time.time - gameStartTime);
            int tempCurrentScore = numberCreaturesKilled;
            if(tempCurrentTime>dataHandler.bestTime){
                dataHandler.bestTime = tempCurrentTime;
            }
            if(tempCurrentScore>dataHandler.bestScore){
                dataHandler.bestScore = tempCurrentScore;
            }

            GUIEndTimeValue.text = tempCurrentTime.ToString();
            GUIEndScoreValue.text = tempCurrentScore.ToString();
            GUIEndBestTimeValue.text = "(Best : " + dataHandler.bestTime.ToString() + ")";
            GUIEndBestScoreValue.text = "(Best : " + dataHandler.bestScore.ToString() + ")";

            objectGUIGameOver.SetActive(true);
            pauseGame = true;

        }


        public void ButtonRestart() {
            Application.LoadLevel("game");
        }
    }
}