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

public class ParticleController : MonoBehaviour {

    public static ParticleController player;

    void Awake()
    {
        if (player == null && MainPlayer)
        {
            //DontDestroyOnLoad(gameObject);
            player = this;
        }
        else if (player != this)
        {
           
        }
    }

    public int intensity = 10;
    public int maxIntesity = 40;
    public Vector3 Direction = Vector3.right;
    public float minSpeed = 5f;
    public float speed = 10;
    public float maxSpeed = 15f;
    public float midSpeed;

    public float MediumModifyer = 1;

    public float accel = 1f;
    public float turnFactor = 0.01f;

    public float maxWaveAngle = 45f;
    public float WaveAngle = 20f;
    public float minWaveAngle = 1f;

    public float maxSize = 0.3f;
    public float minSize = 0.1f;

    public KeyCode upButton;
    public KeyCode downButton;
    public KeyCode waveButton;
    public KeyCode nonWaveButton;

    public KeyCode accelButton;
    public KeyCode brakeButton;

    public Rigidbody rb;

    public GameObject subParticle;

    public List<ParticleController> subParticles = new List<ParticleController>();

    public bool Wave = false;

    public Color LightColor;
    public float maxHue = 1;
    public Renderer rend;
    public Light lit;
    public TrailRenderer trailRend;

    public bool MainPlayer = false;

    //for sub particles;

    public GameObject subTarget = null;
    public bool Targeting = false;
    public float targetingTimer = 0;

    public bool control = true; 
    public bool moving = true;


    private void Start()
    {
        midSpeed = (maxSpeed + minSpeed) / 2f;
        Physics.IgnoreLayerCollision(8, 8);
        rb = GetComponent<Rigidbody>();
        rend = GetComponent<Renderer>();
        lit = GetComponent<Light>();
        trailRend = GetComponent<TrailRenderer>();
    }

    private void FixedUpdate()
    {
        LightColor = Color.HSVToRGB((speed - minSpeed) / (maxSpeed - minSpeed) * maxHue, 1, 1);
        rend.material.color = LightColor;
        lit.color = LightColor;
        trailRend.startColor = LightColor;
        rend.material.SetColor("_EmissionColor", LightColor);

        WaveAngle =(1 - ((speed - minSpeed) / (maxSpeed - minSpeed))) * (maxWaveAngle - minWaveAngle) + minWaveAngle;

        if (MainPlayer)
        {
            lit.intensity = (intensity) / (maxIntesity) * 2f + 0.5f;


            if ((Input.GetKeyDown(waveButton) || Input.GetButtonDown("Fire1")) && control)
            {
                if (!Wave)
                {

                    for (int i = 0; i < intensity; i++)
                    {
                        float sAngle = WaveAngle - 2 * WaveAngle * i / intensity;
                        Vector3 SpawnDirection = Quaternion.Euler(0, 0, sAngle) * Direction;
                        GameObject Spawn = Instantiate(subParticle) as GameObject;
                        Spawn.transform.position = transform.position;
                        ParticleController spPC = Spawn.GetComponent<ParticleController>();
                        spPC.Direction = SpawnDirection;
                        spPC.speed = speed;
                        spPC.upButton = upButton;
                        spPC.downButton = downButton;
                        subParticles.Add(spPC);
                    }
                    Wave = true;

                    gameObject.SetActive(false);
                }

            }


            transform.localScale = Vector3.one * (minSize + (maxSize - minSize) * ((float)intensity /(float) maxIntesity));
            trailRend.startWidth = (minSize + (maxSize - minSize) * ((float)intensity / (float)maxIntesity));
        }
        //Time.timeScale = midSpeed / speed;

        float triggerVal = Input.GetAxis("Triggers");
        if (Input.GetKey(accelButton) && control)
        {
            if (speed < maxSpeed)
            {
                speed += accel;
            }
        }

        
        else if(triggerVal > 0)
        {
           float targetSpd = triggerVal * (maxSpeed - minSpeed) + minSpeed;
            if(speed < targetSpd)
            {
                speed += accel;
            }
            if (speed > targetSpd)
            {
                speed -= accel;
            }
        }

        else
        {
            if (speed > minSpeed)
            {
                speed -= accel;
            }
        }
        //if (Input.GetKey(brakeButton) || Input.GetAxis("Horizontal") < -0.5f)
        //{
        //    if (speed > minSpeed)
        //    {
        //        speed -= accel;
        //    }
        //}

        float Hval = Input.GetAxis("Horizontal");
        float Vval = Input.GetAxis("Vertical");

        if ((Hval != 0 || Vval != 0) && control)
        {
            Direction += new Vector3(Hval, Vval).normalized * turnFactor;
        }


        if (Input.GetKey(upButton) && control)
        {
            Direction += turnFactor * Vector3.up;
        }
        else if (Input.GetKey(downButton) && control)
        {
            Direction += turnFactor * Vector3.down;
        }

        Direction = Direction.normalized;

        if (!MainPlayer && Targeting)
        {
            float dist = Vector3.Distance(transform.position, subTarget.transform.position);
            transform.position = transform.position * (0.85f - 0.30f * targetingTimer) + subTarget.transform.position * (0.15f + 0.30f * targetingTimer);
            if (dist < 0.4f)
            {
                player.subParticles.Remove(this);
                player.intensity += 1;
                Destroy(gameObject);
            }
            if (targetingTimer < 1f) {
                targetingTimer += Time.deltaTime /3f;
            }
        }
        else
        {
            if (moving)
            {
                rb.velocity = Direction.normalized * MediumModifyer * speed / 100f;
                Debug.DrawLine(transform.position, transform.position + Direction, Color.red);
            }
        }

        
    }

    public void Hitch()
    {
        StartCoroutine(hitch());
    }

   public IEnumerator hitch()
    {
        print("hitchin");
        moving = false;
        rb.velocity = Vector3.zero;
        yield return new WaitForSeconds(0f);
        moving = true;
    }
}
