using System.Collections.Generic;
using UnityEngine;

namespace Wizard;

public class UIParticleEffectManager : MonoBehaviour
{
	[SerializeField]
	private GameObject _groupPrefab;

	private static readonly Vector3 START_POSITION = new Vector3(4000f, 0f, 0f);

	private static readonly Vector3 OFFSET_POSITION = new Vector3(4000f, 0f, 0f);

	private int _groupCount;

	private List<UIParticleEffectGroup> _groupList = new List<UIParticleEffectGroup>();

	public static UIParticleEffectManager Instance { get; private set; }

	private void Awake()
	{
		Instance = this;
	}

	public UIParticleEffectGroup StartEffect(Effect2dCreateParam param, UITexture uiTexture, bool isEnableUpdateReposition)
	{
		UIParticleEffectGroup uIParticleEffectGroup = CreateNewGroup(uiTexture);
		uIParticleEffectGroup.StartEffect(param, isEnableUpdateReposition);
		return uIParticleEffectGroup;
	}

	public UIParticleEffectGroup AddEffect(GameObject effect, UITexture uiTexture)
	{
		UIParticleEffectGroup uIParticleEffectGroup = CreateNewGroup(uiTexture);
		uIParticleEffectGroup.SetEffect(effect);
		return uIParticleEffectGroup;
	}

	public void Remove(UIParticleEffectGroup group)
	{
		if (_groupList.Remove(group))
		{
			group.OnBeforeDestroy();
			Object.Destroy(group.gameObject);
		}
	}

	public UIParticleEffectGroup CreateNewGroup(UITexture uiTexture)
	{
		GameObject obj = NGUITools.AddChild(base.gameObject, _groupPrefab);
		UIParticleEffectGroup component = obj.GetComponent<UIParticleEffectGroup>();
		component.Initialize(uiTexture);
		obj.transform.localPosition = START_POSITION + OFFSET_POSITION * _groupCount;
		_groupCount++;
		_groupList.Add(component);
		return component;
	}

	public void OnChangeScene()
	{
		foreach (UIParticleEffectGroup group in _groupList)
		{
			Object.Destroy(group.gameObject);
		}
		_groupList.Clear();
		_groupCount = 0;
	}
}
