using UnityEngine;
using System.Collections;

public class Blackhole : MonoBehaviour 
{
    public enum Polarity
    {
        ANTIMATTER = 0,
        MATTER = 1
    }

    public GameObject inner, outer, outerDisk, innerDisk;
    public float maxhealth = 100;
    public float healthDepletionSpeed = 10f;

    public float currentHP { get; set; }

    Polarity currentState = Polarity.MATTER;

    Color currentColor = new Color(0.5f, 0.5f, 0.5f);
    float colorTimer = 0f;

    float timerPolarSwitch = 0f;
	
    void Start () 
    {
        currentHP = maxhealth;
	}
	
	// Update is called once per frame
	void Update () {
        inner.transform.localRotation = Quaternion.Euler(0, Time.realtimeSinceStartup * 10, 0);
        outer.transform.localRotation = Quaternion.Euler(0, -Time.realtimeSinceStartup * 10, 0);

        currentHP -= (healthDepletionSpeed * Time.deltaTime);

        if ((timerPolarSwitch -= Time.deltaTime) <= 0f)
        {
            if (currentState == Polarity.MATTER)
            {
                currentState = Polarity.ANTIMATTER;
            }
            else
            {
                currentState = Polarity.MATTER;
            }
            colorTimer = 0;

            newPolarSwitchTimer();
        }

        if (currentState == Polarity.MATTER)
        {
            currentColor = Color.Lerp(currentColor, new Color(0.9f, 0.9f, 0.9f), colorTimer);
            innerDisk.renderer.material.color = currentColor;
            colorTimer += Time.deltaTime;
        }
        else
        {
            currentColor = Color.Lerp(currentColor, new Color(0.1f, 0.1f, 0.1f), colorTimer);
            innerDisk.renderer.material.color = currentColor;
            colorTimer += Time.deltaTime;
        }

        transform.localScale = Vector3.Lerp(transform.localScale, new Vector3(Mathf.Max(30f * (currentHP / maxhealth), 10f), 1, Mathf.Max(30f * (currentHP / maxhealth), 10f)), Time.deltaTime);

        if (currentHP <= 0) Main.Instance.GameOver(87, 33);

        //transform.localScale = new Vector3(Mathf.Max(30f * (currentHP / maxhealth), 10f), 1, Mathf.Max(30f * (currentHP / maxhealth), 10f));
	}

    public void newPolarSwitchTimer()
    {
        timerPolarSwitch = Random.Range(10f, 30f);
    }

    public void ParticleEaten(GameObject go)
    {
        if (go.name == "Antimatter" && currentState == Polarity.ANTIMATTER)
        {
            currentHP = Mathf.Min(currentHP + 3, maxhealth);
            Main.Instance.highscore += 10;
        }
        else if (go.name == "Antimatter" && currentState == Polarity.MATTER)
        {
            Mg_ParticleSystem.Instance.PlayExplosion(go.transform.position, new Color(0.1f, 0.1f, 0.1f));
            currentHP = Mathf.Min(currentHP - 5, maxhealth);
            if (outerDisk.renderer.IsVisibleFrom(Camera.main))
                Mg_Camera.Instance.Shake(3f, 1f);

            Main.Instance.highscore -= 2;
        }
        else if (go.name == "Matter" && currentState == Polarity.MATTER)
        {
            currentHP = Mathf.Min(currentHP + 3, maxhealth);
            Main.Instance.highscore += 10;
        }
        else if (go.name == "Matter" && currentState == Polarity.ANTIMATTER)
        {
            Mg_ParticleSystem.Instance.PlayExplosion(go.transform.position, new Color(0.9f, 0.9f, 0.9f));
            currentHP = Mathf.Min(currentHP - 5, maxhealth);
            if (outerDisk.renderer.IsVisibleFrom(Camera.main))
                Mg_Camera.Instance.Shake(3f, 1f);

            Main.Instance.highscore -= 2;
        }
    }

    void OnTriggerStay(Collider other)
    {
        if (other == null) return;

        Particle obj = other.gameObject.GetComponent<Particle>();
        if (obj == null) return;

        Vector3 attractPt = gameObject.transform.localPosition;
        obj.frc = (attractPt - other.gameObject.transform.localPosition); 

        float dist = Vector3.Distance(attractPt, other.gameObject.transform.localPosition);
        if (dist < 12)
        {
            obj.frc.Normalize();
            other.rigidbody.velocity += -obj.frc * 0.2f;
        }
        else
        {
            obj.frc.x += Mathf.PerlinNoise(obj.uniqueVal, obj.uniqueVal);
            obj.frc.y += Mathf.PerlinNoise(obj.uniqueVal, obj.uniqueVal);
            other.rigidbody.velocity += obj.frc * (dist * 0.01f); //apply force
        }
    }
}
