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

namespace Wizard;

public class GachaLayoutEffect : MonoBehaviour
{
	[SerializeField]
	private GameObject _effectRoot;

	[SerializeField]
	private UITexture _maskTexture;

	private bool _isCompleteSetup;

	private bool _isChangingPack;

	private GameObject _effectObj;

	private const int EFFECT_SORTING_ORDER = 1;

	public void Setup(string effectName, int stencil, List<string> resourceList)
	{
		if (!_isChangingPack)
		{
			_isChangingPack = true;
			if (_isCompleteSetup)
			{
				Object.Destroy(_effectObj);
				_effectObj = null;
				_isCompleteSetup = false;
			}
			if (stencil > 0)
			{
				SetupStencilMaskMaterial(stencil);
			}
			UIManager.GetInstance().StartCoroutine(SetupEffect(effectName, stencil, resourceList));
		}
	}

	private void SetupStencilMaskMaterial(int stencil)
	{
		_maskTexture.material = MaterialDefine.CreateNguiStencilMaskMaterial(stencil);
	}

	private IEnumerator SetupEffect(string effectName, int stencil, List<string> resourceList)
	{
		bool isCompleteMaterialSet = false;
		List<string> effectAssets = new List<string> { Toolbox.ResourcesManager.GetAssetTypePath(effectName, ResourcesManager.AssetLoadPathType.Effect2D) };
		yield return Toolbox.ResourcesManager.LoadAssetGroup(effectAssets, delegate
		{
			resourceList.AddRange(effectAssets);
		});
		_effectObj = EffectUtility.CreateEffect2D(new Effect2dCreateParam
		{
			Parent = _effectRoot,
			EffectName = effectName,
			ColorCode = null,
			InitActive = false,
			MaterialSetEndCallback = delegate
			{
				isCompleteMaterialSet = true;
			},
			UnloadAssetList = resourceList
		});
		while (!isCompleteMaterialSet)
		{
			yield return null;
		}
		_effectObj.SetActive(value: true);
		if (stencil > 0)
		{
			ParticleSystemRenderer[] componentsInChildren = _effectObj.GetComponentsInChildren<ParticleSystemRenderer>();
			foreach (ParticleSystemRenderer obj in componentsInChildren)
			{
				obj.sortingOrder = 1;
				obj.material = new Material(obj.material);
			}
			GameMgr.GetIns().GetEffectMgr().ChangeMaskShader(_effectObj, stencil);
		}
		float num2 = 1f / _effectObj.transform.localScale.x;
		Transform[] componentsInChildren2 = _effectObj.GetComponentsInChildren<Transform>();
		foreach (Transform obj2 in componentsInChildren2)
		{
			Vector3 localPosition = obj2.localPosition;
			localPosition *= num2;
			obj2.localPosition = localPosition;
		}
		_effectObj.SetLayer(base.gameObject.layer, isSetChildren: true);
		_isCompleteSetup = true;
		_isChangingPack = false;
	}
}
