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

public class AreaSelectChapterEffect
{
	private static readonly Vector3 EFFECT_SCALE = new Vector3(320f, 320f, 320f);

	private AreaSelectUI _areaSelectUI;

	private List<string> _loadedResources = new List<string>();

	private bool _isLoadEnd = true;

	private Transform _effectParent;

	private Dictionary<string, ParticleSystem> _effectList = new Dictionary<string, ParticleSystem>();

	private string _playingEffect = "";

	public bool GetLoadEnd()
	{
		return _isLoadEnd;
	}

	public void Init(AreaSelectUI areaselectUI, Transform effectParent)
	{
		_areaSelectUI = areaselectUI;
		_effectParent = effectParent;
		_playingEffect = "";
	}

	public void Term()
	{
		foreach (KeyValuePair<string, ParticleSystem> effect in _effectList)
		{
			ParticleSystem value = effect.Value;
			if (!(null == value))
			{
				Object.Destroy(value.gameObject);
			}
		}
		_effectList.Clear();
		_playingEffect = "";
	}

	public void LoadEffect(List<StoryChapterData> chapterDataList)
	{
		_isLoadEnd = false;
		string path = "";
		int i = 0;
		for (int count = chapterDataList.Count; i < count; i++)
		{
			path = chapterDataList[i].ChapterEffectPath;
			if (!string.IsNullOrEmpty(path) && !_effectList.ContainsKey(path))
			{
				_effectList.Add(path, null);
				_loadedResources.Add(Toolbox.ResourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.Effect2D));
			}
		}
		_areaSelectUI.StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadedResources, delegate
		{
			List<GameObject> list = new List<GameObject>(_effectList.Count);
			int j = 0;
			for (int count2 = chapterDataList.Count; j < count2; j++)
			{
				path = chapterDataList[j].ChapterEffectPath;
				if (!string.IsNullOrEmpty(path) && !(null != _effectList[path]))
				{
					GameObject gameObject = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true)) as GameObject;
					if (!(null == gameObject))
					{
						_effectList[path] = Object.Instantiate(gameObject).GetComponent<ParticleSystem>();
						_effectList[path].transform.parent = _effectParent;
						_effectList[path].transform.localPosition = Vector3.zero;
						_effectList[path].transform.localScale = EFFECT_SCALE;
						_effectList[path].gameObject.SetActive(value: false);
						list.Add(_effectList[path].gameObject);
					}
				}
			}
			_loadedResources.AddRange(GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(list, delegate
			{
				_isLoadEnd = true;
			}));
		}));
	}

	public void UnLoadEffect()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResources);
		_loadedResources.Clear();
	}

	public void PlayEffect(string path, Vector3 pos)
	{
		if (!string.IsNullOrEmpty(path) && !(_playingEffect == path) && _effectList.ContainsKey(path) && !(null == _effectList[path]))
		{
			_effectList[path].gameObject.SetActive(value: true);
			_effectList[path].Play();
			_effectList[path].transform.localPosition = pos;
			_playingEffect = path;
			SetParticleSystemsSpeed(1f);
		}
	}

	public void StopEffect(float? simulationSpeedAfterStop)
	{
		if (_effectList.ContainsKey(_playingEffect) && !(null == _effectList[_playingEffect]))
		{
			if (simulationSpeedAfterStop.HasValue)
			{
				SetParticleSystemsSpeed(simulationSpeedAfterStop.Value);
			}
			_effectList[_playingEffect].Stop();
			_playingEffect = "";
		}
	}

	public string GetPlayingEffect()
	{
		return _playingEffect;
	}

	private void SetParticleSystemsSpeed(float speed)
	{
		ParticleSystem.MainModule main = _effectList[_playingEffect].main;
		main.simulationSpeed = speed;
		ParticleSystem[] componentsInChildren = _effectList[_playingEffect].GetComponentsInChildren<ParticleSystem>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			ParticleSystem.MainModule main2 = componentsInChildren[i].main;
			main2.simulationSpeed = speed;
		}
	}
}
