using System.Collections;
using Cute;
using UnityEngine;
using Wizard.Battle.View.Vfx;

namespace Wizard.Battle.Player.ClassCharacter;

public abstract class SpineClassCharacter : ClassCharacterBase
{
	protected readonly float ScreenWidthFraction = 0.5f;

	protected readonly float ScreenHeightFraction = 0.05f;

	protected bool _isNoEvolveShift;

	protected bool _isOpponentReverse;

	private SpineObject _spine;

	private ClassCharaPrm.MotionType _currentMotion = ClassCharaPrm.MotionType.idle;

	private bool IsSnCollabSkin => Global.IsSnCollabSkin(GetCharaId());

	public SpineClassCharacter()
	{
	}

	public override VfxBase CreateLoadResouceVfx()
	{
		return InstantVfx.Create(delegate
		{
			ClassCharacterMasterData charaPrmByCharaId = GameMgr.GetIns().GetDataMgr().GetCharaPrmByCharaId(GetCharaId());
			GameObject gobj = new ClassCardCreator(Toolbox.ResourcesManager.LoadObject<GameObject>(Toolbox.ResourcesManager.GetAssetTypePath(charaPrmByCharaId.path, ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true))).LoadRootObject();
			_isNoEvolveShift = charaPrmByCharaId.IsNoEvolveShift;
			_isOpponentReverse = this is EnemySpineClassCharacter && charaPrmByCharaId.IsOpponentReverse;
			GameObject gameObject = NGUITools.AddChild(BattleManagerBase.GetIns().Battle3DContainer);
			gameObject.name = "ClassCharacterRoot";
			GameObject gameObject2 = GameMgr.GetIns().GetPrefabMgr().CloneObjectToParent(gobj, gameObject);
			SetUpAnchor(gameObject);
			AttachOtherUI(gameObject);
			GameObject gameObject3 = gameObject2.transform.Find("ClassObj").gameObject;
			GameObject gameObject4 = gameObject2.transform.Find("Collider").gameObject;
			base.GameObject = gameObject2;
			base.GameObject.tag = GetTagName();
			base.GameObject.transform.localPosition = GetPosition();
			_initPosition = base.GameObject.transform.position;
			gameObject3.transform.localScale = Global.CLASS_BATTLE_SCALE;
			gameObject3.tag = GetTagName();
			gameObject4.tag = GetTagName();
			Transform transform = gameObject3.transform.Find("Class");
			string path = (IsSnCollabSkin ? "md_Encampment_sn_Base" : "md_Encampment_Base");
			transform.GetComponent<MeshFilter>().sharedMesh = Toolbox.ResourcesManager.LoadObject<Mesh>(Toolbox.ResourcesManager.GetAssetTypePath(path, ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true));
			MotionUtils.SetLayerAll(transform.gameObject, 15);
			Transform child = transform.transform.GetChild(0);
			string path2 = (IsSnCollabSkin ? "md_Encampment_sn_Outside" : "md_Encampment_outside");
			child.GetComponent<MeshFilter>().sharedMesh = Toolbox.ResourcesManager.LoadObject<Mesh>(Toolbox.ResourcesManager.GetAssetTypePath(path2, ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true));
			MotionUtils.SetLayerAll(child.gameObject, 10);
			CardTemplate cardTemplate = gameObject2.AddComponent<CardTemplate>();
			cardTemplate.CardWrapObjTemp = gameObject3;
			cardTemplate.Collider = gameObject4.GetComponent<BoxCollider>();
			Transform transform2 = transform.transform;
			transform2.localRotation = ConvertBackPanelRotation(transform2.localRotation);
			GameObject gameObject5 = gameObject3.transform.Find("Shield").gameObject;
			gameObject5.GetComponent<MeshFilter>().sharedMesh = Toolbox.ResourcesManager.LoadObject<Mesh>(Toolbox.ResourcesManager.GetAssetTypePath("md_Encampment_Shield", ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true));
			gameObject5.GetComponent<MeshRenderer>().sharedMaterial = Toolbox.ResourcesManager.LoadObject<Material>(Toolbox.ResourcesManager.GetAssetTypePath("mt_Encampment_Shield", ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true));
			gameObject5.GetComponent<MeshRenderer>().sharedMaterial.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath("tx_Encampment_Shield", ResourcesManager.AssetLoadPathType.ClassCharaTexture, isfetch: true));
			gameObject5.transform.localPosition = GetShieldPosition();
			gameObject5.transform.localScale = new Vector3(1.5f, 1.5f, 1f);
			GameMgr.GetIns().GetEffectMgr().SetParticleShader(gameObject5);
			MeshRenderer component = transform.GetComponent<MeshRenderer>();
			MeshRenderer component2 = child.GetComponent<MeshRenderer>();
			string path3 = (IsSnCollabSkin ? "mt_Encampment_sn_Frame" : "mt_Encampment_Frame");
			Material material = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(path3, ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true)) as Material;
			if (material != null)
			{
				string path4 = (IsSnCollabSkin ? "tx_Encampment_sn_Frame" : "tx_Encampment_Frame");
				material.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath(path4, ResourcesManager.AssetLoadPathType.ClassCharaFrameTexture, isfetch: true));
			}
			Material material2 = Toolbox.ResourcesManager.LoadObject<Material>(Toolbox.ResourcesManager.GetAssetTypePath(GetTextureName(), ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true));
			material2.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath(((int)charaPrmByCharaId.ClassColorId).ToString("00"), ResourcesManager.AssetLoadPathType.ClassCharaEncampment, isfetch: true));
			CardCreatorBase.SetupClassMaterialToCenterCharacterMesh(component, component2, material2, material);
			gameObject5.SetActive(value: true);
			SBattleLoad sBattleLoad = BattleManagerBase.GetIns().SBattleLoad;
			GameObject gameObject6 = GameMgr.GetIns().GetPrefabMgr().CloneObjectToParent(Toolbox.ResourcesManager.LoadObject<GameObject>(Toolbox.ResourcesManager.GetAssetTypePath("cmn_frame_class_1", ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true)), BattleManagerBase.GetIns().Battle3DContainer);
			GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(gameObject6, null, isBattle: true, isField: true);
			gameObject6.transform.parent = gameObject3.transform;
			gameObject6.transform.localPosition = new Vector3(0f, 0.1f, 0.15f);
			gameObject6.transform.localRotation = GetMaskImageRotation();
			gameObject6.transform.localScale = new Vector3(6.4f, 6.4f, 20.48f);
			gameObject6.SetActive(value: false);
			cardTemplate.FrameEffectNormal = gameObject6;
			GameObject gameObject7 = GameMgr.GetIns().GetPrefabMgr().CloneObjectToParent(sBattleLoad.LifePoolIcon, BattleManagerBase.GetIns().Battle3DContainer);
			gameObject7.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
			gameObject7.transform.parent = gameObject5.transform;
			gameObject7.transform.localPosition = GetLifeIconPosition();
			gameObject3.transform.localScale = new Vector3(80f, 80f, 25f);
			gameObject4.transform.localScale = new Vector3(80f, 80f, 25f);
			CardParameter cardParameterFromId = CardMaster.GetInstanceForBattle().GetCardParameterFromId(0);
			(cardTemplate.LifeLabelTemp = gameObject7.transform.Find("LifeLabel").GetComponent<UILabel>()).text = cardParameterFromId.Life.ToString();
			_spine = new SpineObject();
			_spine.LoadAndSet(GetSkinId().ToString(), this, gameObject3.transform);
			GameMgr.GetIns().GetPrefabMgr().Load("UI/Battle/EmotionMessage");
			_emotionLabel = NGUITools.AddChild(BattleManagerBase.GetIns().BtlUIContainer, GameMgr.GetIns().GetPrefabMgr().Get("UI/Battle/EmotionMessage"));
			_emotionLabel.transform.Find("Scale").GetComponent<UIPanel>().depth = GetEmoteLabelDepth();
			_emotionLabel.SetActive(value: false);
			GameMgr.GetIns().GetPrefabMgr().Load("UI/Battle/EnvironmentMessage");
			_enviromentLabel = NGUITools.AddChild(BattleManagerBase.GetIns().BtlUIContainer, GameMgr.GetIns().GetPrefabMgr().Get("UI/Battle/EnvironmentMessage"));
			_enviromentLabel.SetActive(value: false);
			base.GameObject.SetActive(value: false);
		});
	}

	public override void ClearResourceObject()
	{
		if (_spine != null)
		{
			_spine.Clear();
		}
	}

	public override void PlayMotion(ClassCharaPrm.MotionType motionType)
	{
		if (!BattleManagerBase.GetIns().IsRecovery)
		{
			_currentMotion = motionType;
			if (_isAnimation && !(_spine._animator == null))
			{
				_spine._animator.SetTrigger(motionType.ToString());
				BattleCoroutine.GetInstance().StartCoroutine(WaitMotionEnd());
			}
		}
	}

	public override void ResetMotion()
	{
		_currentMotion = ClassCharaPrm.MotionType.idle;
		_spine._animator.Play(ClassCharaPrm.MotionType.idle.ToString(), 0, 0f);
		_spine.WaitChangeFace(ClassCharaPrm.FaceType.skin_01.ToString());
	}

	public override void SetTrigger(string str)
	{
		_spine._animator.SetTrigger(str);
	}

	public override ClassCharaPrm.MotionType GetMotion()
	{
		return _currentMotion;
	}

	public override void OutFrame()
	{
		if (_isAnimation)
		{
			_spine.OutFrame(isHighRank: false);
		}
	}

	public override void ChangeFace(ClassCharaPrm.FaceType faceType)
	{
		if (BattleManagerBase.GetIns() != null && !BattleManagerBase.GetIns().IsRecovery && _isAnimation && !(_spine._skeletonMecanim == null))
		{
			BattleCoroutine.GetInstance().StartCoroutine(WaitChangeFace(faceType));
		}
	}

	public override IEnumerator WaitChangeFace(ClassCharaPrm.FaceType faceType)
	{
		yield return null;
		string faceStr = faceType.ToString();
		_spine.WaitChangeFace(faceStr);
	}

	public override void SetAnimationEnable(bool enable)
	{
		bool isAnimation = _isAnimation;
		_isAnimation = enable;
		_spine.SetAnimationEnable(_isAnimation, isAnimation);
	}

	public override bool IsAnimationEnable()
	{
		return _isAnimation;
	}

	public override IEnumerator WaitMotionEnd()
	{
		yield return null;
		float num = ((!GetCurrentClipIsName(ClassCharaPrm.MotionType.idle)) ? _spine._animator.GetCurrentAnimatorStateInfo(0).length : (_spine._animator.GetCurrentAnimatorStateInfo(0).length * 0.5f));
		num -= Time.deltaTime * 2f;
		yield return new WaitForSeconds(num);
		ChangeFace(ClassCharaPrm.FaceType.skin_01);
	}

	public IEnumerator WaitReturnOnAnimation()
	{
		float seconds = (float)((Toolbox.QualityManager.GetFrameRate() == 60) ? 85 : 86) / 30f;
		yield return new WaitForSeconds(seconds);
		_spine.SetDefaultLayer(isHighRank: false);
	}

	protected override IEnumerator WaitReturnFrame()
	{
		yield return _spine.WaitReturnFrame(GetSpinePosition(), isHighRank: false);
	}

	public override float GetCurrentClipTime()
	{
		if (_spine._animator == null)
		{
			return 0f;
		}
		return _spine._animator.GetCurrentAnimatorStateInfo(0).length;
	}

	public override bool GetCurrentClipIsName(ClassCharaPrm.MotionType motionType)
	{
		if (_spine._animator == null)
		{
			return false;
		}
		return _spine._animator.GetCurrentAnimatorStateInfo(0).IsName(motionType.ToString());
	}

	public override bool IsNoEvolveShift()
	{
		return _isNoEvolveShift;
	}

	public override bool IsOpponentReverse()
	{
		return _isOpponentReverse;
	}
}
