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

public class VacuumMiniGame : MonoBehaviour
{
    [SerializeField] float speed, angularSpeed, vacuumLenght;
    [SerializeField] int scorePerDust;
    [SerializeField] GameObject dustParent, miniGameParent;
    [SerializeField] GameObject doneMarker;
    [SerializeField] GameObject gameWindow;
    [SerializeField] GameObject shine;
    [SerializeField] GameObject middle;
    Renderer rend;
    int amoutOfDust;
    int dustVacuumed;
    float shakeTimer = 0;
    Vector3 gameWindowOrigPos;
    Vector3 vacuumOrigPos;
    Quaternion vacuumOrigRot;

    public UnityEvent VacuumDoneEvent = new UnityEvent();


    ParticleSystem[] dustBits;
	// Use this for initialization
	void Start ()
    {
        dustBits = dustParent.GetComponentsInChildren<ParticleSystem>();
        amoutOfDust = dustBits.Length;
        rend = GetComponent<Renderer>();
        gameWindowOrigPos = gameWindow.transform.position;
        vacuumOrigPos = transform.position;
        vacuumOrigRot = transform.rotation;
        RandomizeDust();
    }

    private void OnEnable()
    {
        RandomizeDust();
        transform.position = vacuumOrigPos;
        transform.rotation = vacuumOrigRot;
        gameWindowOrigPos = gameWindow.transform.position;
    }

    private void RandomizeDust()
    {
            for (int i = 0; i < dustBits.Length; i++)
            {
                dustBits[i].gameObject.SetActive(true);
                dustBits[i].transform.position = miniGameParent.transform.position + new Vector3(Random.Range(-3, 6), Random.Range(-4, 5), -miniGameParent.transform.position.z);
            
        }

    }
	
	// Update is called once per frame
	void Update ()
    {
        Movement();
        VacuumCounter();
        Vacuum();
    }

    private void Movement()
    {
       //TODO: add limits on borders
       if (Vector3.Distance(transform.position + transform.up * Input.GetAxis("Vertical") * Time.deltaTime * speed, middle.transform.position) < 10)
        {
            transform.position += transform.up * Input.GetAxis("Vertical") * Time.deltaTime * speed;
        }
        if (shakeTimer < 2)
        {
            gameWindow.transform.position += Random.insideUnitSphere * Input.GetAxis("Vertical") * Time.deltaTime * 10;
            shakeTimer += Time.deltaTime;
        }
        else
        {
            shakeTimer = 0;
            gameWindow.transform.position = gameWindowOrigPos;
        }
        if (Vector3.Angle(Vector3.up, Quaternion.Euler(0, 0, -Input.GetAxis("RightJoystickX") * Time.deltaTime * angularSpeed) * transform.up) < 45)
        {
            transform.Rotate(0, 0, -Input.GetAxis("RightJoystickX") * Time.deltaTime * angularSpeed);
        }
    }

    private void Vacuum()
    {
        for (int i = 0; i < dustBits.Length; i++)
        {
            Vector3 dustPos = dustBits[i].transform.position;
            Vector3 vacuumPos = transform.position;
            Vector2 leftPos = vacuumPos;
            Vector2 rightPos = vacuumPos;
            leftPos.x = vacuumPos.x + rend.bounds.size.x / 2;
            rightPos.x = vacuumPos.x + rend.bounds.size.x / 2;
            if (Vector3.Distance(dustPos, vacuumPos) < vacuumLenght ||
                Vector3.Distance(dustPos, leftPos) < vacuumLenght ||
                Vector3.Distance(dustPos, rightPos) < vacuumLenght)
            {
                ParticleSystem.Particle[] m_particles = new ParticleSystem.Particle[dustBits[i].particleCount];
                dustBits[i].GetParticles(m_particles);
                for (int j = 0; j < m_particles.Length; j++)
                {
                    m_particles[j].velocity = (vacuumPos - m_particles[j].position) * 2;
                }
                dustBits[i].SetParticles(m_particles, m_particles.Length);
                
            }
        }
    }

    private void VacuumCounter()
    {
        if (dustVacuumed == amoutOfDust)
        {
            dustVacuumed = 0;
            VacuumDoneEvent.Invoke();
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        collision.gameObject.SetActive(false);
        dustVacuumed++;
        Score.Instance.UpdateScore(scorePerDust);
        Instantiate(shine, collision.transform.position, Quaternion.identity);
    }
}
