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

public class CombinerScript : MonoBehaviour {

    public ParticleController Pparticle;
    Vector3 frontParticleV = Vector3.zero;
    Vector3 avgParticleV = Vector3.zero;
    Vector3 avgParticleP = Vector3.zero;
    float avgParticleS = 0;
    public float PickupRange = 10f;

    public bool triggerCombine = false;

    public void FixedUpdate()
    {
        if(Input.GetKeyDown( Pparticle.nonWaveButton ) || Input.GetButtonDown("Fire2") || triggerCombine)
        {
            triggerCombine = false;
            if (!Pparticle.gameObject.active && Pparticle.Wave)
            {
                print("tryCombine");
                CombineParticlesV2();
            }
        }
    }


    
    public void CombineParticlesV2()
    {
        
         avgV();

        


        Pparticle.gameObject.SetActive(true);
        Pparticle.transform.position = avgParticleP;
        Pparticle.speed = avgParticleS;
        Pparticle.Direction = avgParticleV;
        Pparticle.Wave = false;
        Pparticle.intensity = 1;

    }

    void avgV ()
    {
        Vector3 answer = Pparticle.subParticles[0].transform.position;
        int ansInt = 0;
        for (int i = 0; i < Pparticle.subParticles.Count; i++)
        {
            if (Pparticle.subParticles[i].transform.position.x > answer.x)
            {
                answer = Pparticle.subParticles[i].transform.position;
                ansInt = i;
            }

        }
        //frontParticleV = Pparticle.subParticles[ansInt].Direction;

        List<ParticleController> inRange = new List<ParticleController>();
        List<int> toRemove  = new List<int>();

        Vector3 avgPos = Vector3.zero;
        Vector3 avgDirection = Vector3.zero;
        float avgParticleSpeed = 0;

        if (Pparticle.subParticles.Count > 0)
        {
            for (int i = 0; i < Pparticle.subParticles.Count; i++)
            {
                if (Pparticle.subParticles[i].transform.position.x >= answer.x - PickupRange && Pparticle.subParticles[i].transform.position.x <= answer.x + PickupRange)
                {
                    Pparticle.subParticles[i].subTarget = Pparticle.gameObject;
                    Pparticle.subParticles[i].Targeting = true;
                    Pparticle.subParticles[i].targetingTimer = 0;
                    inRange.Add(Pparticle.subParticles[i]);
                    avgPos += Pparticle.subParticles[i].transform.position;
                    avgDirection += Pparticle.subParticles[i].Direction;
                    avgParticleSpeed += Pparticle.subParticles[i].speed;
                }
                else
                {
                    toRemove.Add(i);
                }
            }

            avgDirection = avgDirection.normalized;
            avgPos = avgPos / (float)inRange.Count;
            avgParticleSpeed = avgParticleSpeed / (float)inRange.Count;

            avgParticleV = avgDirection;
            avgParticleP = avgPos;
            avgParticleS = avgParticleSpeed;
        }
        else
        {
            Debug.LogError("o noes theres no subParticles");
            avgParticleV = Vector3.right;
            avgParticleP = Vector3.zero;
            avgParticleS = 1000;
        }

        

        for(int j = toRemove.Count - 1; j >= 0; j--)
        {
            Destroy(Pparticle.subParticles[toRemove[j]].gameObject);
            //Pparticle.subParticles.RemoveAt(toRemove[j]);
        }
        Pparticle.subParticles.Clear();
    
    }


}
