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

namespace RollingRockRumble {
    public class CreatureHandler : MonoBehaviour {

        public int creatureLevel;
        private int levelMax = 6;
        private float evolutionSpeed = 5f;
        private float nextEvolution;

        public GameObject prefabSpikes;
        public GameObject objectBoulder;
        public GameObject prefabParticleCreatureKilled;

        private float creatureLifeStartTime;


        void Start() {
            creatureLifeStartTime = Time.time;
            creatureLevel = Mathf.FloorToInt(Random.value * 1.9999f + 1f);
            nextEvolution = Time.time + evolutionSpeed;
            ChangeColor();
            objectBoulder = GameObject.Find("Boulder");
        }



        void Update() {
            if (nextEvolution < Time.time) {
                creatureLevel ++;
                if (creatureLevel > levelMax) {
                    creatureLevel = 1;
                    ThrowSpikes();
                }
                nextEvolution = Time.time + evolutionSpeed;
                ChangeColor();
            }
        }



        public void HasBeenHit(int loss) {
            creatureLevel = Mathf.Max(creatureLevel - loss, 1);
            nextEvolution = Time.time + evolutionSpeed;
            AudioSource tempSFX = GameObject.Find("SFXCreatureHurt").GetComponent<AudioSource>();
            tempSFX.transform.position = transform.position;
            tempSFX.Play();
            ChangeColor();
            GetComponent<Rigidbody2D>().velocity = new Vector2((this.transform.position.x - objectBoulder.transform.position.x) * 2f,
                                                                Mathf.Max(this.transform.position.y - objectBoulder.transform.position.y, 1f) * 2f);
        }



        public void ChangeColor() {
            float ColorRed = Mathf.Min(2f * creatureLevel / levelMax, 1f);
            float ColorGreen = Mathf.Min(2f - 2f * creatureLevel / levelMax, 1f);
            GetComponent<SpriteRenderer>().color = new Color(ColorRed, ColorGreen, 0f, 1f);
        }



        public void ThrowSpikes() {
            GameObject tempSpike = Instantiate(prefabSpikes, transform.position, Quaternion.identity) as GameObject;

            if (Vector3.Distance(objectBoulder.transform.position, this.transform.position) < 10f) {
                tempSpike.GetComponent<Rigidbody2D>().velocity = new Vector2((objectBoulder.transform.position.x - this.transform.position.x) * 2f,
                                                                             Mathf.Max(objectBoulder.transform.position.y - this.transform.position.y, 1f) * 2f);
            }
            else {
                tempSpike.GetComponent<Rigidbody2D>().velocity = new Vector2((Mathf.Floor(Random.value * 1.999f) * 2f - 1f) * ((Random.value * 2.0f) + 2f), 
                                                                                Random.value * 2f + 2f);
            }
        }



        public void CreatureHit() {
            GameObject tempParticle = Instantiate(prefabParticleCreatureKilled, transform.position, Quaternion.identity) as GameObject;
            tempParticle.GetComponent<ParticleSystem>().startColor = GetComponent<SpriteRenderer>().color;
        }



        public void CreatureKilled() {
            AudioSource tempSFX = GameObject.Find("SFXCreatureCrushed").GetComponent<AudioSource>();
            tempSFX.transform.position = transform.position;
            tempSFX.Play();
            GameObject.Find("GAME").GetComponent<GameHandler>().numberCreaturesKilled++;
            Destroy(this.gameObject);
        }
    }
}
