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

namespace Wizard;

public class SpineDisplay : MonoBehaviour
{
	private const string ANIMATION_TRANSFORM_NAME = "AnimationTransform";

	private const string MASK_MATERIAL = "mt_encampment_mask";

	private const int STENCIL_VALUE = 3;

	[SerializeField]
	private bool _isEnableAspectFix = true;

	[SerializeField]
	private bool _isEnableRenderTextureSizeSetting;

	[SerializeField]
	private int _renderTextureSize = 2048;

	[SerializeField]
	private int _renderTextureMobileSize = 1024;

	private GameObject _spineObject;

	private Animator[] _animators;

	private int _saveScreenWidth;

	private int _saveScreenHeight;

	private RenderTexture _renderTexture;

	private UITexture _uiTexture;

	private Camera _camera;

	private List<Material> _materialList = new List<Material>();

	private const float RENDER_TEXTURE_BASE_SIZE_DEFAULT = 2048f;

	private const float ASPECT_RATIO = 0.5625f;

	private const float ASPECT_RATIO_REVERSE = 1.7777778f;

	private float RenderTextureSize
	{
		get
		{
			if (_isEnableRenderTextureSizeSetting)
			{
				return _renderTextureSize;
			}
			return 2048f;
		}
	}

	public List<string> GetLoadPath(string path, bool fetch)
	{
		return new List<string>
		{
			Toolbox.ResourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.UISpine, fetch),
			Toolbox.ResourcesManager.GetAssetTypePath("mt_encampment_mask", ResourcesManager.AssetLoadPathType.ClassCharaMaterial, fetch),
			Toolbox.ResourcesManager.GetAssetTypePath(MaskTextureName(), ResourcesManager.AssetLoadPathType.ClassCharaTexture, fetch)
		};
	}

	public void Initialize(string path, UITexture texture, Camera camera)
	{
		_uiTexture = texture;
		_camera = camera;
		SpriteRenderer spriteRenderer = base.gameObject.AddComponent<SpriteRenderer>();
		Material material = new Material(Toolbox.ResourcesManager.LoadObject<Material>(Toolbox.ResourcesManager.GetAssetTypePath("mt_encampment_mask", ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true)));
		material.SetInt("_Stencil", 3);
		_materialList.Add(spriteRenderer.material);
		spriteRenderer.material = material;
		_materialList.Add(material);
		spriteRenderer.sprite = Toolbox.ResourcesManager.LoadObject<Sprite>(Toolbox.ResourcesManager.GetAssetTypePath(MaskTextureName(), ResourcesManager.AssetLoadPathType.ClassCharaTexture, isfetch: true));
		GameObject original = Toolbox.ResourcesManager.LoadObject<GameObject>(Toolbox.ResourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.UISpine, isfetch: true));
		_spineObject = Object.Instantiate(original);
		MotionUtils.SetLayerAll(_spineObject, base.gameObject.layer);
		Transform transform = _spineObject.transform;
		transform.parent = base.transform;
		transform.localPosition = Vector3.zero;
		transform.localScale = Vector3.one;
		transform.localEulerAngles = Vector3.zero;
		MeshRenderer component = transform.GetComponentInChildren<SkeletonMecanim>().GetComponent<MeshRenderer>();
		_animators = transform.GetComponentsInChildren<Animator>();
		Animator[] animators = _animators;
		for (int i = 0; i < animators.Length; i++)
		{
			animators[i].speed = 1f;
		}
		StartMotion("NONE");
		SetMaterialInfo(component, 3);
		CreateRenderTexture();
		UpdateUITextureSize(_uiTexture);
	}

	public void StartMotion(string motionName)
	{
		Animator[] animators = _animators;
		for (int i = 0; i < animators.Length; i++)
		{
			animators[i].SetTrigger(motionName);
		}
	}

	public bool IsPlaying()
	{
		Animator[] animators = _animators;
		for (int i = 0; i < animators.Length; i++)
		{
			if ((double)animators[i].GetCurrentAnimatorStateInfo(0).normalizedTime < 1.0)
			{
				return true;
			}
		}
		return false;
	}

	private void CreateRenderTexture()
	{
		if (!(_renderTexture != null))
		{
			if (_isEnableAspectFix)
			{
				_renderTexture = new RenderTexture((int)RenderTextureSize, (int)(RenderTextureSize * 0.5625f), 0, RenderTextureFormat.ARGB32);
			}
			else
			{
				_renderTexture = new RenderTexture((int)RenderTextureSize, (int)RenderTextureSize, 0, RenderTextureFormat.ARGB32);
			}
			_renderTexture.name = "SpineDisplay";
			_camera.targetTexture = _renderTexture;
			_uiTexture.mainTexture = _renderTexture;
		}
	}

	private void OnDestroy()
	{
		if (_renderTexture != null)
		{
			_renderTexture.Release();
			Object.Destroy(_renderTexture);
			_renderTexture = null;
		}
		foreach (Material material in _materialList)
		{
			Object.Destroy(material);
		}
		_materialList.Clear();
	}

	private string MaskTextureName()
	{
		return "tx_encampment_mask";
	}

	private void Update()
	{
		if (!(_camera == null) && !(_uiTexture == null) && (UIManager.GetInstance().FrontCameraPixelWidth != _saveScreenWidth || UIManager.GetInstance().FrontCameraPixelHeight != _saveScreenHeight))
		{
			UpdateUITextureSize(_uiTexture);
		}
	}

	private void UpdateUITextureSize(UITexture uiTexture)
	{
		Camera frontCamera = UIManager.GetInstance().FrontCamera;
		Vector3 localScale = uiTexture.transform.localScale;
		uiTexture.transform.localScale = Vector3.one;
		_saveScreenWidth = UIManager.GetInstance().FrontCameraPixelWidth;
		_saveScreenHeight = UIManager.GetInstance().FrontCameraPixelHeight;
		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.transform.localScale = localScale;
		float num = 0.5625f;
		float num2 = frontCamera.pixelHeight;
		float num3 = frontCamera.pixelWidth;
		if (num2 / num3 < num)
		{
			float num4 = vector2.x - vector.x;
			float num5 = num4 * num;
			uiTexture.width = (int)num4;
			uiTexture.height = (int)num5;
		}
		else
		{
			float num6 = vector.y - vector2.y;
			float num7 = num6 * 1.7777778f;
			uiTexture.width = (int)num7;
			uiTexture.height = (int)num6;
		}
	}

	private void SetMaterialInfo(MeshRenderer meshRenderer, int stencil)
	{
		Material material = new Material(meshRenderer.material);
		material.SetInt("_Stencil", stencil);
		_materialList.Add(meshRenderer.material);
		meshRenderer.material = material;
		_materialList.Add(material);
		for (int i = 0; i < meshRenderer.materials.Length; i++)
		{
			Material material2 = meshRenderer.materials[i];
			if (material2 != null && material2.GetInt("_Stencil") != stencil)
			{
				meshRenderer.materials[i] = new Material(material2);
				meshRenderer.materials[i].SetInt("_Stencil", stencil);
				_materialList.Add(meshRenderer.materials[i]);
			}
		}
	}
}
