using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using Wizard.Battle.View.Vfx;

namespace Wizard.Battle.Player.ClassCharacter;

public class Class3dCharacterBase : ClassCharacterBase
{
	protected Material _class3DMaterial;

	private Camera _norCamera;

	private Camera _evoCamera;

	protected GameObject _root;

	protected Charactor3dInformation _charaInfo;

	private Effect3dInformation _effectInfo;

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

	protected List<int> _idleStateHash = new List<int>();

	private readonly Vector3 PP_PANEL_POSITION = new Vector3(0f, -34.22f, -29.25f);

	private readonly Vector3 WIDGET_POSITION = new Vector3(0f, 490f, 30f);

	private readonly int WIDGET_OFFSET_TOP = -1162;

	private readonly int WIDGET_OFFSET_BOTTOM = 195;

	private readonly string IDLE_NAME = "Base Layer.idle";

	protected int _classCharacterId = 3604;

	public bool IsPlayer = true;

	protected const int RENDER_QUEUE = 2999;

	protected override Vector3 MessagePosition => _initPosition + new Vector3(0f, -0.3f, 0f);

	public override VfxBase CreateLoadResouceVfx()
	{
		SequentialVfxPlayer sequentialVfxPlayer = SequentialVfxPlayer.Create();
		sequentialVfxPlayer.Register(LoadResouceVfx());
		return sequentialVfxPlayer;
	}

	private VfxBase LoadResouceVfx()
	{
		return InstantVfx.Create(delegate
		{
			DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
			int num = (IsPlayer ? dataMgr.GetPlayerSkinId() : dataMgr.GetEnemySkinId());
			if (_classCharacterId != num)
			{
				_classCharacterId = num;
			}
			ClassCharacterMasterData charaPrmByCharaId = GameMgr.GetIns().GetDataMgr().GetCharaPrmByCharaId(_classCharacterId);
			GameObject gobj = new ClassCardCreator(Toolbox.ResourcesManager.LoadObject<GameObject>(Toolbox.ResourcesManager.GetAssetTypePath(charaPrmByCharaId.path, ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true))).LoadRootObject();
			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_uma_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_uma_Outside", ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true));
			MotionUtils.SetLayerAll(child.gameObject, 10);
			RenderTextureFormat lutFormat = GetLutFormat();
			RenderTexture renderTexture = new RenderTexture(2048, 2048, 1, lutFormat);
			Shader shader = Shader.Find("UI/Default");
			_class3DMaterial = new Material(shader);
			_class3DMaterial.name = "ClassEvolveMaterial";
			_class3DMaterial.mainTexture = renderTexture;
			Material source = new Material(Shader.Find("Custom/ModelShaderBattle"))
			{
				name = "ClassFlameMaterial",
				mainTexture = renderTexture
			};
			_root = new GameObject();
			_root.name = (IsPlayer ? "Player3DChara" : "Enemy3DChara");
			Transform transform2 = _root.transform;
			transform2.localPosition = Vector3.zero;
			float width = (float)Screen.width / (float)Screen.height;
			_norCamera = new GameObject
			{
				name = "Player3DCamera"
			}.AddComponent<Camera>();
			_norCamera.backgroundColor = new Color(0f, 0f, 0f, 0f);
			_norCamera.nearClipPlane = 1f;
			_norCamera.farClipPlane = 40f;
			_norCamera.fieldOfView = 33f;
			_norCamera.transform.parent = transform2;
			_norCamera.targetTexture = renderTexture;
			_norCamera.cullingMask = 1;
			_norCamera.transform.localPosition = new Vector3(0f, 0.09718f, -3.338f);
			_norCamera.transform.localEulerAngles = new Vector3(2.5f, 0f, 0f);
			Class3dPostImageEffect class3dPostImageEffect = null;
			if (IsPlayer)
			{
				GameObject gameObject5 = new GameObject
				{
					name = "Evo3DCamera"
				};
				_evoCamera = gameObject5.AddComponent<Camera>();
				_evoCamera.backgroundColor = new Color(0f, 0f, 0f, 0f);
				_evoCamera.nearClipPlane = 1f;
				_evoCamera.farClipPlane = 40f;
				_evoCamera.fieldOfView = 21f;
				_evoCamera.rect = new Rect(0f, 0f, width, 1f);
				_evoCamera.transform.parent = transform2;
				_evoCamera.allowMSAA = false;
				_evoCamera.depth = 30f;
				_evoCamera.cullingMask = 1;
				_evoCamera.clearFlags = CameraClearFlags.Depth;
				_evoCamera.gameObject.SetActive(value: false);
				_evoCamera.transform.localPosition = new Vector3(0f, 0.12f, 0f);
				_evoCamera.transform.localEulerAngles = new Vector3(2.5f, 0f, 0f);
				class3dPostImageEffect = gameObject5.AddComponent<Class3dPostImageEffect>();
				class3dPostImageEffect.Initialize();
			}
			GameObject gameObject6 = null;
			gameObject6 = Object.Instantiate(Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(_classCharacterId.ToString(), ResourcesManager.AssetLoadPathType.ClassChara3D, isfetch: true)), transform2) as GameObject;
			_effectInfo = gameObject6.GetComponent<Effect3dInformation>();
			_charaInfo = gameObject6.GetComponent<Charactor3dInformation>();
			_charaInfo.InitializeMesh(this, class3dPostImageEffect);
			_charaInfo.Quad.SetLayer(15, isSetChildren: true);
			Transform transform3 = _charaInfo.Quad.transform;
			transform3.transform.parent = transform.parent;
			float y = (IsPlayer ? 0.2f : (-0.41f));
			transform3.localPosition = new Vector3(0f, y, -0.35f);
			float y2 = (IsPlayer ? 5.7f : 6.1725f);
			transform3.localScale = new Vector3(5.7f, y2, 1f);
			Vector3 battleCameraPos = BattleManagerBase.GetIns().Camera.BattleCameraPos;
			battleCameraPos.x = transform3.position.x;
			transform3.LookAt(battleCameraPos);
			MeshRenderer component = _charaInfo.Quad.GetComponent<MeshRenderer>();
			component.sharedMaterial = new Material(source);
			component.sharedMaterial.renderQueue = 2999;
			CardTemplate cardTemplate = gameObject2.AddComponent<CardTemplate>();
			cardTemplate.CardWrapObjTemp = gameObject3;
			cardTemplate.Collider = gameObject4.GetComponent<BoxCollider>();
			Transform transform4 = transform.transform;
			transform4.localRotation = ConvertBackPanelRotation(transform4.localRotation);
			GameObject gameObject7 = gameObject3.transform.Find("Shield").gameObject;
			gameObject7.GetComponent<MeshFilter>().sharedMesh = Toolbox.ResourcesManager.LoadObject<Mesh>(Toolbox.ResourcesManager.GetAssetTypePath("md_Encampment_Shield", ResourcesManager.AssetLoadPathType.ClassCharaMesh, isfetch: true));
			gameObject7.GetComponent<MeshRenderer>().sharedMaterial = Toolbox.ResourcesManager.LoadObject<Material>(Toolbox.ResourcesManager.GetAssetTypePath("mt_Encampment_Shield", ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true));
			gameObject7.GetComponent<MeshRenderer>().sharedMaterial.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath("tx_Encampment_Shield", ResourcesManager.AssetLoadPathType.ClassCharaTexture, isfetch: true));
			gameObject7.transform.localPosition = GetShieldPosition();
			gameObject7.transform.localScale = new Vector3(1.5f, 1.5f, 1f);
			GameMgr.GetIns().GetEffectMgr().SetParticleShader(gameObject7);
			MeshRenderer component2 = transform.GetComponent<MeshRenderer>();
			MeshRenderer component3 = child.GetComponent<MeshRenderer>();
			Material material = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath("mt_Encampment_uma_Frame", ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true)) as Material;
			if (material != null)
			{
				material.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath("tx_Encampment_uma_Frame", ResourcesManager.AssetLoadPathType.ClassCharaFrameTexture, isfetch: true));
			}
			CardCreatorBase.SetupClassMaterialToCenterCharacterMesh(component2, component3, new Material(Toolbox.ResourcesManager.LoadObject<Material>(Toolbox.ResourcesManager.GetAssetTypePath(GetTextureName(), ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true)))
			{
				mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath(((int)charaPrmByCharaId.ClassColorId).ToString("00"), ResourcesManager.AssetLoadPathType.ClassCharaEncampment, isfetch: true))
			}, material);
			gameObject7.SetActive(value: true);
			SBattleLoad sBattleLoad = BattleManagerBase.GetIns().SBattleLoad;
			GameObject gameObject8 = 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(gameObject8, null, isBattle: true, isField: true);
			gameObject8.transform.parent = gameObject3.transform;
			gameObject8.transform.localPosition = new Vector3(0f, 0.1f, 0.15f);
			gameObject8.transform.localRotation = GetMaskImageRotation();
			gameObject8.transform.localScale = new Vector3(6.4f, 6.4f, 20.48f);
			gameObject8.SetActive(value: false);
			cardTemplate.FrameEffectNormal = gameObject8;
			GameObject gameObject9 = GameMgr.GetIns().GetPrefabMgr().CloneObjectToParent(sBattleLoad.LifePoolIcon, BattleManagerBase.GetIns().Battle3DContainer);
			gameObject9.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
			gameObject9.transform.parent = gameObject7.transform;
			gameObject9.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 = gameObject9.transform.Find("LifeLabel").GetComponent<UILabel>()).text = cardParameterFromId.Life.ToString();
			SpriteRenderer spriteRenderer = new GameObject().AddComponent<SpriteRenderer>();
			spriteRenderer.sprite = Toolbox.ResourcesManager.LoadObject<Sprite>(Toolbox.ResourcesManager.GetAssetTypePath("tx_Encampment_uma_mask", ResourcesManager.AssetLoadPathType.ClassCharaTexture, isfetch: true));
			Material material2 = Object.Instantiate(Toolbox.ResourcesManager.LoadObject<Material>(Toolbox.ResourcesManager.GetAssetTypePath("mt_encampment_mask", ResourcesManager.AssetLoadPathType.ClassCharaMaterial, isfetch: true)));
			material2.SetInt("_Stencil", GetStencil());
			spriteRenderer.material = material2;
			spriteRenderer.transform.parent = gameObject3.transform;
			spriteRenderer.transform.localPosition = GetMaskImagePosition();
			spriteRenderer.transform.localScale = Vector3.one * 0.46f;
			spriteRenderer.gameObject.SetLayer(15, isSetChildren: true);
			spriteRenderer.sortingOrder = GetMaskSortingOrder();
			spriteRenderer.name = "SpineMask";
			spriteRenderer.transform.localRotation = GetMaskImageRotation();
			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);
		});
	}

	private RenderTextureFormat GetLutFormat()
	{
		RenderTextureFormat renderTextureFormat = RenderTextureFormat.ARGB32;
		if (!IsRenderTextureFormatSupportedForLinearFiltering(renderTextureFormat))
		{
			renderTextureFormat = RenderTextureFormat.ARGB64;
			if (!IsRenderTextureFormatSupportedForLinearFiltering(renderTextureFormat))
			{
				renderTextureFormat = RenderTextureFormat.ARGBHalf;
			}
			if (!IsRenderTextureFormatSupportedForLinearFiltering(renderTextureFormat))
			{
				Debug.LogError("RenderTexture not Supported.");
			}
		}
		return renderTextureFormat;
	}

	private static bool IsRenderTextureFormatSupportedForLinearFiltering(RenderTextureFormat format)
	{
		return SystemInfo.IsFormatSupported(GraphicsFormatUtility.GetGraphicsFormat(format, RenderTextureReadWrite.Linear), FormatUsage.Linear);
	}

	public void EnableEvolve(bool enable)
	{
		_charaInfo.Quad.SetActive(!enable);
		_evoCamera.gameObject.SetActive(enable);
	}

	public override void PlayMotion(ClassCharaPrm.MotionType motionType)
	{
		if (BattleManagerBase.GetIns().IsRecovery || !_isAnimation)
		{
			return;
		}
		_currentMotion = motionType;
		Animator[] animators = _charaInfo.Animators;
		foreach (Animator animator in animators)
		{
			if (_currentMotion == ClassCharaPrm.MotionType.damage || motionType == ClassCharaPrm.MotionType.damage)
			{
				animator.ResetTrigger(ClassCharaPrm.MotionType.idle.ToString());
				string trigger = ClassCharaPrm.MotionType.damage.ToString();
				animator.SetTrigger(trigger);
			}
			else if (animator.GetCurrentAnimatorStateInfo(0).IsName(IDLE_NAME))
			{
				animator.ResetTrigger(ClassCharaPrm.MotionType.idle.ToString());
				animator.SetTrigger(motionType.ToString());
				_effectInfo.PlayEffect(motionType);
			}
			else
			{
				BattleCoroutine.GetInstance().StartCoroutine(WaitMotionEnd(animator, motionType));
			}
		}
	}

	public override void ResetMotion()
	{
		_currentMotion = ClassCharaPrm.MotionType.idle;
		Animator[] animators = _charaInfo.Animators;
		foreach (Animator obj in animators)
		{
			obj.ResetTrigger(ClassCharaPrm.MotionType.idle.ToString());
			obj.SetTrigger(ClassCharaPrm.MotionType.idle.ToString());
		}
	}

	private IEnumerator WaitMotionEnd(Animator animator, ClassCharaPrm.MotionType motionType)
	{
		animator.SetTrigger(ClassCharaPrm.MotionType.idle.ToString());
		_currentMotion = ClassCharaPrm.MotionType.idle;
		yield return new WaitForSeconds(0.1f);
		animator.ResetTrigger(ClassCharaPrm.MotionType.idle.ToString());
		animator.SetTrigger(motionType.ToString());
		_effectInfo.PlayEffect(motionType);
	}

	public override bool IsAnimationEnable()
	{
		return _isAnimation;
	}

	protected override string GetTagName()
	{
		return "Enemy";
	}

	protected override Quaternion ConvertBackPanelRotation(Quaternion originalRotation)
	{
		return Quaternion.Euler(0f, -180f, 180f);
	}

	protected override Vector3 GetPosition()
	{
		return Global.CLASS_BATTLE_POSITION_ENEMY;
	}

	protected override void SetUpAnchor(GameObject o)
	{
		o.transform.localPosition = WIDGET_POSITION;
		UIWidget uIWidget = o.AddComponent<UIWidget>();
		uIWidget.topAnchor.relative = 1f;
		uIWidget.topAnchor.absolute = WIDGET_OFFSET_TOP;
		Transform target = BattleManagerBase.GetIns().Battle3DContainer.transform.Find("Camera");
		uIWidget.topAnchor.target = target;
		uIWidget.bottomAnchor.target = target;
		uIWidget.bottomAnchor.relative = 0.5f;
		uIWidget.bottomAnchor.absolute = WIDGET_OFFSET_BOTTOM;
	}

	protected override void AttachOtherUI(GameObject o)
	{
		BattleManagerBase.GetIns().BattleEnemy.BattleView.EpPanel.transform.parent = o.transform;
		EnemyStatusPanelControl enemyStatusPanelControl = BattleManagerBase.GetIns().BattleEnemy.StatusPanelControl as EnemyStatusPanelControl;
		if ((bool)enemyStatusPanelControl)
		{
			enemyStatusPanelControl.ChangePPPanelParent(o.transform, PP_PANEL_POSITION);
		}
	}

	public override Vector3 GetSpinePosition()
	{
		return new Vector3(0f, -0.36f, 0.17f);
	}

	public override Vector3 GetMaskImagePosition()
	{
		return new Vector3(0f, 0.04f, 0f);
	}

	protected override Vector3 GetShieldPosition()
	{
		return new Vector3(1.86f, 0.48f, 0f);
	}

	protected override Vector3 GetLifeIconPosition()
	{
		return new Vector3(0f, 0f, 0.1f);
	}

	protected override string GetTextureName()
	{
		return "mt_Encampment_Chara_1_Rev";
	}

	public override Quaternion GetMaskImageRotation()
	{
		return Quaternion.Euler(new Vector3(0f, 0f, 180f));
	}

	public override Vector3 ConvertSpineScale(Vector3 originalScale)
	{
		return originalScale;
	}

	protected int GetSpineSortingOrder()
	{
		return -1;
	}

	protected int GetMaskSortingOrder()
	{
		return -1;
	}

	protected override int GetEmoteLabelDepth()
	{
		return 0;
	}

	protected override int GetCharaId()
	{
		return GameMgr.GetIns().GetDataMgr().GetEnemyCharaId();
	}

	public override int GetSkinId()
	{
		return GameMgr.GetIns().GetDataMgr().GetEnemySkinId();
	}

	public override int GetStencil()
	{
		return 2;
	}

	public override void SetAnimationEnable(bool enable)
	{
		_isAnimation = enable;
		if (enable)
		{
			PlayMotion(ClassCharaPrm.MotionType.idle);
			SetAnimatorSpeed(1f);
			return;
		}
		Animator[] animators = _charaInfo.Animators;
		for (int i = 0; i < animators.Length; i++)
		{
			animators[i].Play(ClassCharaPrm.MotionType.idle.ToString(), -1, 0f);
		}
		SetAnimatorSpeed(0f);
	}

	private void SetAnimatorSpeed(float speed)
	{
		Animator[] animators = _charaInfo.Animators;
		for (int i = 0; i < animators.Length; i++)
		{
			animators[i].speed = speed;
		}
	}

	public override float GetCurrentClipTime()
	{
		return 0f;
	}

	public override bool GetCurrentClipIsName(ClassCharaPrm.MotionType motionType)
	{
		return false;
	}

	protected override IEnumerator WaitReturnFrame()
	{
		yield return null;
	}

	public override void OutFrame()
	{
	}

	public override int GetSpineSortingOrder(bool isBack = false)
	{
		if (!isBack)
		{
			return -1;
		}
		return -9;
	}

	public override int GetMaskSortingOrder(bool isBack = false)
	{
		if (!isBack)
		{
			return -1;
		}
		return -9;
	}

	public override void SetTrigger(string str)
	{
	}

	public override IEnumerator WaitChangeFace(ClassCharaPrm.FaceType faceType)
	{
		yield return null;
	}

	public override IEnumerator WaitMotionEnd()
	{
		yield return null;
	}

	public override void ChangeFace(ClassCharaPrm.FaceType faceType)
	{
	}

	public override void ClearResourceObject()
	{
		if (_class3DMaterial != null)
		{
			Object.Destroy(_class3DMaterial);
			_class3DMaterial = null;
		}
		if (_norCamera != null && _norCamera.targetTexture != null)
		{
			_norCamera.targetTexture.Release();
			_norCamera.targetTexture = null;
		}
		if (_effectInfo != null)
		{
			_effectInfo.DestroyEffect();
			_effectInfo = null;
		}
		if (_charaInfo != null)
		{
			_charaInfo.Destroy();
			_charaInfo = null;
		}
		if (_root != null)
		{
			Object.Destroy(_root);
			_root = null;
		}
		Toolbox.ResourcesManager.RemoveAsset(Toolbox.ResourcesManager.GetAssetTypePath(_classCharacterId.ToString(), ResourcesManager.AssetLoadPathType.ClassChara3D));
	}

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

	public override bool IsNoEvolveShift()
	{
		return false;
	}

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