using System.Collections;
using System.Collections.Generic;
using Cute;
using Spine;
using UnityEngine;
using Wizard.Battle.Resource;
using Wizard.Battle.View.Vfx;

namespace Wizard.Battle.Player.ClassCharacter;

public abstract class HighRankSpineClassCharacter : ClassCharacterBase
{
	public const string EFFECT_FILE_PATH = "Jpn/Effect/Effects/";

	protected readonly float ScreenWidthFraction = 0.5f;

	protected readonly float ScreenHeightFraction = 0.05f;

	public SpineObject _backSpineObject;

	public SpineObject _frontSpineObject;

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

	public List<HighRankEffectInfo> EvolveEffects = new List<HighRankEffectInfo>();

	public bool IsEvolveSkin { get; private set; }

	protected override Vector3 MessagePosition => UIManager.GetInstance().getCamera().ScreenToWorldPoint(new Vector3((float)Screen.width * ScreenWidthFraction, (float)Screen.height * ScreenHeightFraction, 0f));

	public HighRankSpineClassCharacter()
	{
	}

	public static Vector3 BoneWorldToGlobalPosConsiderRotate(Bone _bone, Transform _position, Vector3 _lossyScale)
	{
		return _position.position + Quaternion.Euler(_position.eulerAngles) * (Vector3.Scale(new Vector3(_bone.WorldX, _bone.WorldY, 0f), _lossyScale) - Vector3.Scale(_position.localPosition, _position.lossyScale));
	}

	public IEnumerator TrackTarget(bool isFront, GameObject effect, Vector3 absolutePosition, bool _followX = true, bool _followY = true, Bone bone = null, bool trackRotation = false, Transform traskScale = null, float _coefficient = 1f)
	{
		GameObject spine = (isFront ? _frontSpineObject._spineObject : _backSpineObject._spineObject);
		Transform transform = effect.transform;
		Vector3 baseLocalScale = transform.localScale;
		while (this != null && !(spine == null) && !(transform == null))
		{
			if (_followX && _followY)
			{
				transform.position = BoneWorldToGlobalPosConsiderRotate(bone, spine.transform, spine.transform.lossyScale);
			}
			else if (!_followX)
			{
				Vector3 position = transform.position;
				position.y = BoneWorldToGlobalPosConsiderRotate(bone, spine.transform, spine.transform.lossyScale).y;
				transform.position = position;
			}
			else if (!_followY)
			{
				Vector3 position2 = transform.position;
				position2.x = BoneWorldToGlobalPosConsiderRotate(bone, spine.transform, spine.transform.lossyScale).x;
				transform.position = position2;
			}
			if (trackRotation && bone != null)
			{
				float num = (((bone.Skeleton.ScaleX < 0f) ^ (bone.Skeleton.ScaleY < 0f) ^ (spine.transform.localScale.x < 0f)) ? (-1f) : 1f);
				Vector3 eulerAngles = transform.localRotation.eulerAngles;
				transform.localRotation = Quaternion.Euler(eulerAngles.x, eulerAngles.y, bone.WorldRotationX * num);
			}
			if (traskScale != null)
			{
				transform.localScale = Vector3.Scale(baseLocalScale * _coefficient, traskScale.localScale);
			}
			yield return null;
		}
	}

	public static bool IsEvolveMotion(ClassCharaPrm.MotionType motionType)
	{
		if (motionType == ClassCharaPrm.MotionType.extra || (uint)(motionType - 15) <= 2u)
		{
			return true;
		}
		return false;
	}

	public static bool IsOpponentEvolveMotion(ClassCharaPrm.MotionType motionType)
	{
		if ((uint)(motionType - 18) <= 2u)
		{
			return true;
		}
		return false;
	}

	public override void PlayMotion(ClassCharaPrm.MotionType motionType)
	{
		if ((BattleManagerBase.GetIns() != null && BattleManagerBase.GetIns().IsRecovery) || (_frontSpineObject._animator == null && _backSpineObject._animator == null))
		{
			return;
		}
		if (!_isAnimation)
		{
			if (!IsEvolveSkin)
			{
				return;
			}
			bool num = IsEvolveMotion(motionType);
			bool flag = IsOpponentEvolveMotion(motionType);
			if (num || flag)
			{
				_currentMotion = motionType;
				if (_frontSpineObject._animator != null)
				{
					Animator animator = _frontSpineObject._animator;
					animator.Play(ClassCharaPrm.MotionType.z_idle.ToString(), 0, animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
				}
				if (_backSpineObject._animator != null)
				{
					Animator animator2 = _backSpineObject._animator;
					animator2.Play(ClassCharaPrm.MotionType.z_idle.ToString(), 0, animator2.GetCurrentAnimatorStateInfo(0).normalizedTime);
				}
			}
			else if (motionType == ClassCharaPrm.MotionType.z_idle && _currentMotion != motionType)
			{
				_currentMotion = motionType;
				if (_frontSpineObject._animator != null)
				{
					_frontSpineObject._animator.Play(ClassCharaPrm.MotionType.z_idle.ToString(), 0, 0f);
				}
				if (_backSpineObject._animator != null)
				{
					_backSpineObject._animator.Play(ClassCharaPrm.MotionType.z_idle.ToString(), 0, 0f);
				}
			}
			return;
		}
		_currentMotion = motionType;
		bool isEvolve = IsEvolveMotion(motionType);
		if (_frontSpineObject._animator != null && !isEvolve)
		{
			_frontSpineObject._animator.SetTrigger(motionType.ToString());
		}
		if (_backSpineObject._animator != null && !isEvolve)
		{
			_backSpineObject._animator.SetTrigger(motionType.ToString());
		}
		BattleCoroutine.GetInstance().StartCoroutine(WaitMotionEnd());
		if (isEvolve)
		{
			float normalizedTime = 1f / _frontSpineObject._animator.GetCurrentAnimatorClipInfo(0)[0].clip.frameRate;
			if (_frontSpineObject._animator != null)
			{
				_frontSpineObject._animator.Play(motionType.ToString(), 0, normalizedTime);
			}
			if (_backSpineObject._animator != null)
			{
				_backSpineObject._animator.Play(motionType.ToString(), 0, normalizedTime);
			}
		}
		if (BattleManagerBase.GetIns() == null)
		{
			new BattleResourceMgr();
		}
		else
		{
			_ = BattleManagerBase.GetIns().BattleResourceMgr;
		}
		SequentialVfxPlayer sequentialVfxPlayer = SequentialVfxPlayer.Create();
		ParallelVfxPlayer parallelVfxPlayer = ParallelVfxPlayer.Create();
		Transform effectTransform = ((_frontSpineObject._spineObject != null) ? _frontSpineObject._spineObject.transform : _backSpineObject._spineObject.transform);
		foreach (HighRankEffectInfo effect in EvolveEffects)
		{
			if (!effect.IsEffectMotion(motionType) || !(effect.Prefab != ""))
			{
				continue;
			}
			Object original = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(effect.Prefab, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true));
			EffectBattle effObject = (Object.Instantiate(original) as GameObject).AddComponent<EffectBattle>();
			effObject.gameObject.SetActive(value: false);
			if (!isEvolve)
			{
				effObject.transform.SetParent(_frontSpineObject._skeletonMecanim.transform);
			}
			SequentialVfxPlayer sequentialVfxPlayer2 = SequentialVfxPlayer.Create();
			if (BattleManagerBase.GetIns() != null)
			{
				if (Global.PreLoadSkinId.Contains(GetSkinId()) && this is PlayerHighRankSpineClassCharacter)
				{
					sequentialVfxPlayer2.Register(InstantVfx.Create(delegate
					{
						GameMgr.GetIns().GetEffectMgr().SetOnlyUIParticleShader(effObject.gameObject);
					}));
				}
				else
				{
					sequentialVfxPlayer2.Register(InstantVfx.Create(delegate
					{
						GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(effObject.gameObject, delegate
						{
						}, isBattle: true);
					}));
				}
			}
			if (effect.Delay > 0)
			{
				sequentialVfxPlayer2.Register(WaitVfx.Create((float)effect.Delay / 30f));
			}
			sequentialVfxPlayer2.Register(new DelaySetupVfx(() => new SkinEffectVfx(() => effObject, effectTransform, effect.Layer, isEvolve)));
			if (effect.TargetBoneName != "")
			{
				sequentialVfxPlayer2.Register(InstantVfx.Create(delegate
				{
					BattleCoroutine.GetInstance().StartCoroutine(TrackTarget(isFront: true, effObject.gameObject, Vector3.zero, _followX: true, _followY: true, _frontSpineObject._skeletonMecanim.Skeleton.FindBone(effect.TargetBoneName)));
				}));
			}
			parallelVfxPlayer.Register(sequentialVfxPlayer2);
		}
		sequentialVfxPlayer.Register(parallelVfxPlayer);
		if (BattleManagerBase.GetIns() != null)
		{
			BattleManagerBase.GetIns().VfxMgr.RegisterImmediateVfx(sequentialVfxPlayer);
		}
	}

	public override void ResetMotion()
	{
		_currentMotion = ClassCharaPrm.MotionType.idle;
		if (_frontSpineObject._animator != null)
		{
			_frontSpineObject._animator.Play(ClassCharaPrm.MotionType.idle.ToString(), 0, 0f);
		}
		if (_backSpineObject._animator != null)
		{
			_backSpineObject._animator.Play(ClassCharaPrm.MotionType.idle.ToString(), 0, 0f);
		}
	}

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

	private void Load()
	{
		DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
		ClassCharacterMasterData charaPrmByCharaId = dataMgr.GetCharaPrmByCharaId(GetCharaId());
		bool isPlayer = this is PlayerHighRankSpineClassCharacter;
		IsEvolveSkin = dataMgr.IsEvolveSkin(isPlayer);
		GameObject gobj = new ClassCardCreator(Toolbox.ResourcesManager.LoadObject<GameObject>(Toolbox.ResourcesManager.GetAssetTypePath(charaPrmByCharaId.path, ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true))).LoadRootObject();
		int skinId = GetSkinId();
		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");
		transform.GetComponent<MeshFilter>().sharedMesh = Toolbox.ResourcesManager.LoadObject<Mesh>(Toolbox.ResourcesManager.GetAssetTypePath("md_Encampment_" + skinId + "_Base", ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true));
		MotionUtils.SetLayerAll(transform.gameObject, 15);
		Transform child = transform.transform.GetChild(0);
		child.GetComponent<MeshFilter>().sharedMesh = Toolbox.ResourcesManager.LoadObject<Mesh>(Toolbox.ResourcesManager.GetAssetTypePath("md_Encampment_" + skinId + "_Outside", 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>();
		Material material = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath("mt_Encampment_" + skinId + "_Frame", ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true)) as Material;
		if (material != null)
		{
			material.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath("tx_Encampment_" + skinId + "_Frame", 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();
		_frontSpineObject = new SpineObject();
		_frontSpineObject.LoadAndSet(skinId.ToString(), this, gameObject3.transform);
		_backSpineObject = new SpineObject();
		_backSpineObject.LoadAndSet(skinId.ToString(), this, gameObject3.transform, isBack: true);
		string key = "class_" + skinId;
		if (Data.Master.HighRankEffect.ContainsKey(key))
		{
			EvolveEffects = Data.Master.HighRankEffect[key];
		}
		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 VfxBase CreateLoadResouceVfx()
	{
		return InstantVfx.Create(delegate
		{
			Load();
		});
	}

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

	protected override IEnumerator WaitReturnFrame()
	{
		BattleCoroutine.GetInstance().StartCoroutine(_backSpineObject.WaitReturnFrame(GetSpinePosition(), isHighRank: true));
		yield return _frontSpineObject.WaitReturnFrame(GetSpinePosition(), isHighRank: true);
	}

	public override void OutFrame()
	{
		if (_isAnimation)
		{
			_frontSpineObject.OutFrame(isHighRank: true);
			_backSpineObject.OutFrame(isHighRank: true);
		}
	}

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

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

	public override void SetAnimationEnable(bool enable)
	{
		bool isAnimation = _isAnimation;
		_isAnimation = enable;
		BattleManagerBase ins = BattleManagerBase.GetIns();
		bool isPlayer = this is PlayerHighRankSpineClassCharacter;
		bool isEvolved = false;
		if (ins != null)
		{
			isEvolved = ins.GetBattlePlayer(isPlayer).IsSkinEvolved;
		}
		_frontSpineObject.SetAnimationEnable(_isAnimation, isAnimation, IsEvolveSkin, isEvolved);
		_backSpineObject.SetAnimationEnable(_isAnimation, isAnimation, IsEvolveSkin, isEvolved);
	}

	public override bool IsAnimationEnable()
	{
		return _isAnimation;
	}

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

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

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

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

	public override bool IsNoEvolveShift()
	{
		return false;
	}

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