using UnityEngine;
using System.Collections;

enum ParticleMode
{
    PARTICLE_MODE_ATTRACT = 0,
    PARTICLE_MODE_REPEL,
    PARTICLE_MODE_NEAREST_POINTS,
    PARTICLE_MODE_NOISE
};

public class Particle : MonoBehaviour
{
    private GameObject Controller, chasee;
    private bool inited = false;
    
    private float minVelocity;
    private float maxVelocity;
    private float randomness;

    public Vector3 frc;
    public float drag;
    public float uniqueVal;
    public float scale;

    ParticleMode mode;

    void Start()
    {
        StartCoroutine("BoidSteering");

        frc = Vector3.zero;
    }

    public void Init(Transform parent, Vector3 position)
    {
        transform.parent = parent;
        uniqueVal = Random.Range(-10000, 10000);
        transform.localPosition = position;

        rigidbody.velocity.Set(Random.Range(-3.9f, 3.9f), Random.Range(-3.9f, 3.9f), Random.Range(-3.9f, 3.9f));

        frc = Vector3.zero;

        scale = Random.Range(0.5f, 1.0f);

        if (mode == ParticleMode.PARTICLE_MODE_NOISE)
        {
            drag = Random.Range(0.97f, 0.99f);
            rigidbody.velocity.Set(rigidbody.velocity.x, Mathf.Abs(rigidbody.velocity.y) * 3.0f, rigidbody.velocity.z); //make the particles all be going down
        }
        else
        {
            drag = Random.Range(0.95f, 0.998f);
        }
    }

    void SetMode(ParticleMode newMode)
    {
	    mode = newMode;
    }

    IEnumerator BoidSteering()
    {
        while (true)
        {
            if (inited)
            {
                rigidbody.velocity = rigidbody.velocity + Calc() * Time.deltaTime;

                // enforce minimum and maximum speeds for the boids
                float speed = rigidbody.velocity.magnitude;
                if (speed > maxVelocity)
                {
                    rigidbody.velocity = rigidbody.velocity.normalized * maxVelocity;
                }
                else if (speed < minVelocity)
                {
                    rigidbody.velocity = rigidbody.velocity.normalized * minVelocity;
                }
            }

            float waitTime = Random.Range(0.3f, 0.5f);
            yield return new WaitForSeconds(waitTime);
        }
    }

    private Vector3 Calc()
    {
        float dist = Vector3.Distance(Vector3.zero, gameObject.transform.localPosition);
        float x = 0, y = 0;

        if (dist < 500)
        {
            float w = 500 * Mathf.Sqrt(Random.Range(0.1f, 1f));
            float t = 2 * Mathf.PI * Random.Range(0, 1f);
            x = w * Mathf.Cos(t);
            y = w * Mathf.Sin(t);

            return new Vector3(x, y, 0);
        }

        return -gameObject.transform.localPosition;
    }

    public void SetController(GameObject theController)
    {
        Controller = theController;
        BoidController boidController = Controller.GetComponent<BoidController>();
        minVelocity = boidController.minVelocity;
        maxVelocity = boidController.maxVelocity;
        randomness = boidController.randomness;
        inited = true;
    }
}