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

public class SoulFollowEffect : MonoBehaviour
{
    [HideInInspector]
    public float angle;
    public int soulsNumber;
    public float rotateSpeed;
    public GameObject soul;
    public ESpirits_Invisible eSpirits;
    [HideInInspector]
    public List<GameObject> soulsList = new List<GameObject>();
    public float distance;
    public float boomTime;
    public float destroyTime;
    public bool isTransfiguration;
    private void Awake()
    {
        if (!isTransfiguration)
        {
            for (int i = 0; i < eSpirits.followNumber; i++)
            {
                GameObject newSoul = Instantiate(soul.transform.GetChild(0).gameObject);
                newSoul.transform.parent = transform;
                newSoul.SetActive(false);
                soulsList.Add(newSoul);
            }
        }

    }
    private void Update()
    {
        angle += Time.deltaTime * rotateSpeed;
        if(angle >= 360)
        {
            angle -= 360;
        }
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }
    public void Boom()
    {
        GameObject boomSoul = Instantiate(soul.transform.GetChild(1).gameObject);
        boomSoul.transform.position = transform.position;
        SoulBoom soulBoom = boomSoul.GetComponent<SoulBoom>();
        boomSoul.SetActive(true);
    }
    public void ShowSouls(int newNumber)
    {
        if(soulsList.Count < newNumber)
        {
            GameObject newSoul = Instantiate(soul.transform.GetChild(0).gameObject);
            newSoul.transform.parent = transform;
            newSoul.SetActive(false);
            soulsList.Add(newSoul);
        }

        if(newNumber == 0)
        {
            for(int i = 0;i< soulsList.Count; i++)
            {
                soulsList[i].gameObject.SetActive(false);
            }
        }
        else if (newNumber > soulsNumber)
        {
            soulsList[newNumber - 1].gameObject.SetActive(true);
            float angle = 360f / newNumber;
            for (int i = 0; i < newNumber; i++)
            {
                float targetRad = angle * i * Mathf.Deg2Rad;
                soulsList[i].transform.localPosition =
                    new Vector3(distance * Mathf.Cos(targetRad), distance * Mathf.Sin(targetRad), 0);
            }

        }
        else
        {
            soulsList[newNumber].gameObject.SetActive(false);
            float angle = 360f / newNumber;
            for (int i = 0; i < newNumber; i++)
            {
                float targetRad = angle * i * Mathf.Deg2Rad;
                soulsList[i].transform.localPosition =
                    new Vector3(distance * Mathf.Cos(targetRad), distance * Mathf.Sin(targetRad), 0);
            }
        }

        soulsNumber = newNumber;
    }
}
