using System.Collections.Generic;
using UnityEngine;

namespace Wizard;

public class UIParticleEffectGroup : MonoBehaviour
{
	private struct AutoUpdateInfo
	{
		public GameObject Effect { get; private set; }

		public GameObject Parent { get; private set; }

		public AutoUpdateInfo(GameObject effect, GameObject parent)
		{
			Effect = effect;
			Parent = parent;
		}
	}

	[SerializeField]
	private Camera _camera;

	[SerializeField]
	private GameObject _effectParentRoot;

	private List<AutoUpdateInfo> _updateList = new List<AutoUpdateInfo>();

	private RenderTexture _renderTexture;

	private UITexture _uiTexture;

	private int _saveScreenWidth;

	private int _saveScreenHeight;

	private const string SHADER_NAME = "Cygames/UI/Transparent ColoredAdd";

	private const float RENDER_TEXTURE_BASE_SIZE = 2048f;

	private GameObject CreateParent()
	{
		GameObject obj = new GameObject();
		Transform obj2 = obj.transform;
		obj2.parent = _effectParentRoot.transform;
		obj2.localPosition = Vector3.zero;
		obj2.localScale = Vector3.one;
		obj.layer = base.gameObject.layer;
		return obj;
	}

	public void Initialize(UITexture uiTexture)
	{
		_uiTexture = uiTexture;
	}

	private Vector3 DecideEffectLocalPosition(GameObject target)
	{
		Vector3 zero = Vector3.zero;
		while (target != null)
		{
			zero += target.transform.localPosition;
			target = target.transform.parent.gameObject;
			if (target.GetComponent<Camera>() != null)
			{
				break;
			}
		}
		return zero;
	}

	public void StartEffect(Effect2dCreateParam param, bool isEnableUpdateReposition)
	{
		GameObject parent = param.Parent;
		GameObject gameObject = CreateParent();
		gameObject.transform.localPosition = DecideEffectLocalPosition(param.Parent);
		gameObject.transform.localRotation = param.Parent.transform.localRotation;
		gameObject.transform.localScale = Vector3.one;
		param.Parent = gameObject;
		GameObject gameObject2 = EffectUtility.CreateEffect2D(param);
		gameObject2.SetLayer(gameObject.layer, isSetChildren: true);
		gameObject2.transform.localPosition = Vector3.zero;
		SetEffectCommonSetting(gameObject2);
		gameObject2.SetActive(value: true);
		if (isEnableUpdateReposition)
		{
			_updateList.Add(new AutoUpdateInfo(gameObject, parent));
		}
	}

	public void SetEffect(GameObject effect)
	{
		Vector3 localPosition = effect.transform.localPosition;
		GameObject gameObject = CreateParent();
		effect.transform.parent = gameObject.transform;
		effect.transform.localPosition = localPosition;
		effect.transform.localScale = Vector3.one;
		effect.SetLayer(gameObject.layer, isSetChildren: true);
		SetEffectCommonSetting(effect);
	}

	private void SetEffectCommonSetting(GameObject effect)
	{
		UpdateRenderTexture();
		UpdateUITextureSize(_uiTexture);
	}

	private void Update()
	{
		if (UIManager.GetInstance().FrontCameraPixelWidth != _saveScreenWidth || UIManager.GetInstance().FrontCameraPixelHeight != _saveScreenHeight)
		{
			UpdateRenderTexture();
			UpdateUITextureSize(_uiTexture);
		}
		foreach (AutoUpdateInfo update in _updateList)
		{
			update.Effect.transform.localPosition = DecideEffectLocalPosition(update.Parent);
		}
	}

	private void UpdateRenderTexture()
	{
		if (_renderTexture != null)
		{
			Object.Destroy(_renderTexture);
		}
		_saveScreenWidth = UIManager.GetInstance().FrontCameraPixelWidth;
		_saveScreenHeight = UIManager.GetInstance().FrontCameraPixelHeight;
		float num = UIManager.GetInstance().FrontCameraPixelWidth;
		float num2 = (float)UIManager.GetInstance().FrontCameraPixelHeight / num;
		_renderTexture = new RenderTexture(2048, (int)(2048f * num2), 0, RenderTextureFormat.ARGB32);
		_renderTexture.name = "UIPG" + _uiTexture.name;
		_camera.targetTexture = _renderTexture;
		_uiTexture.mainTexture = _renderTexture;
	}

	private void UpdateUITextureSize(UITexture uiTexture)
	{
		Camera frontCamera = UIManager.GetInstance().FrontCamera;
		Vector3 position = new Vector3(0f, 1f, 0f);
		Vector3 position2 = new Vector3(1f, 0f, 0f);
		Vector3 position3 = frontCamera.ViewportToWorldPoint(position);
		Vector3 position4 = frontCamera.ViewportToWorldPoint(position2);
		Vector3 vector = uiTexture.transform.InverseTransformPoint(position3);
		Vector3 vector2 = uiTexture.transform.InverseTransformPoint(position4);
		uiTexture.width = (int)(vector2.x - vector.x);
		uiTexture.height = (int)(vector.y - vector2.y);
		uiTexture.shader = Shader.Find("Cygames/UI/Transparent ColoredAdd");
	}

	public void OnBeforeDestroy()
	{
		if (_uiTexture != null)
		{
			_uiTexture.mainTexture = null;
			_uiTexture = null;
		}
	}

	private void OnDestroy()
	{
		if (_renderTexture != null)
		{
			Object.Destroy(_renderTexture);
		}
	}
}
