using UnityEngine;
using System.Collections.Generic;

namespace ZyncopeVFXMagicCircleManager
{
    public class MagicCircleManager : MonoBehaviour
    {
        [HideInInspector] public Transform vfxDisplayLoc1, vfxDisplayLoc2;

        [HideInInspector] public List<GameObject> magicCircleInstant = new List<GameObject>();
        [HideInInspector] public List<GameObject> magicCircleLooping = new List<GameObject>();

        List<GameObject> sceneMagicCircleInstant = new List<GameObject>();
        List<GameObject> sceneMagicCircleLooping = new List<GameObject>();
        int currentVFX = 0;
        //[HideInInspector] public Text currentVFXName;
        public bool autoplayVFX;
        float repeatVal = 7;



        void Start()
        {
            for (int x = 0; x < magicCircleInstant.Count; x++)
            {
                GameObject vfxObject1 = Instantiate(magicCircleInstant[x], vfxDisplayLoc1);
                vfxObject1.SetActive(false);
                sceneMagicCircleInstant.Add(vfxObject1);


                GameObject vfxObject2 = Instantiate(magicCircleLooping[x], vfxDisplayLoc2);
                vfxObject2.SetActive(false);
                sceneMagicCircleLooping.Add(vfxObject2);
            }
            DisplayCurrentVFXName();
            PlayVFXAgain();
            if (autoplayVFX)
            {
                InvokeRepeating("NextVFX", repeatVal, repeatVal);
            }
        }

        public void NextVFX()
        {
            HideVFX();
            currentVFX += 1;
            if (currentVFX >= magicCircleInstant.Count)
                currentVFX = 0;
            DisplayVFX();
            DisplayCurrentVFXName();
        }

        public void PreviousVFX()
        {
            HideVFX();
            currentVFX -= 1;
            if (currentVFX < 0)
                currentVFX = magicCircleInstant.Count - 1;
            DisplayVFX();
            DisplayCurrentVFXName();
        }

        public void PlayVFXAgain()
        {
            HideVFX();
            DisplayVFX();
        }

        void HideVFX()
        {
            sceneMagicCircleInstant[currentVFX].SetActive(false);
            sceneMagicCircleLooping[currentVFX].SetActive(false);
        }

        void DisplayVFX()
        {
            sceneMagicCircleInstant[currentVFX].SetActive(true);
            sceneMagicCircleLooping[currentVFX].SetActive(true);
        }

        void DisplayCurrentVFXName()
        {
            string[] displayNameSplit = sceneMagicCircleInstant[currentVFX].name.Split("_");
            string removeUnused = displayNameSplit[3].Replace("(Clone)", "");
            string capitalizedString = char.ToUpper(removeUnused[0]) + removeUnused.Substring(1) + " Magic Circle";
            string displayVFXName = capitalizedString;
            //currentVFXName.text = displayVFXName;
        }
    }
}