using RoR2;
using RoR2.CharacterAI;
using UnityEngine;
using UnityEngine.Networking;

namespace EntityStates.FalseSonBoss;

public class CorruptedPathsDash : BaseState
{
	private Transform modelTransform;

	public static GameObject dashPrefab;

	public static float smallHopVelocity = 0.35f;

	public static float dashPrepDuration = 0.8f;

	public static float dashDuration = 0.05f;

	public static float speedCoefficient = 15f;

	public static float gapToEnemy = 0f;

	public static string beginSoundString;

	public static string endSoundString;

	public static float damageCoefficient = 2f;

	public static float procCoefficient;

	public static GameObject hitEffectPrefab;

	public static float hitPauseDuration;

	private float stopwatch;

	private Vector3 dashVector = Vector3.zero;

	private Animator animator;

	private CharacterModel characterModel;

	private HurtBoxGroup hurtboxGroup;

	private OverlapAttack overlapAttack;

	private ChildLocator childLocator;

	private bool isDashing;

	private bool inHitPause;

	private float hitPauseTimer;

	private CameraTargetParams.AimRequest aimRequest;

	private CharacterBody survivorBody;

	public static GameObject explosionEffect;

	public BlastAttack explosionAttack;

	private float maxHealth;

	private int originalLayer;

	public bool hasHit { get; private set; }

	public int dashIndex { private get; set; }

	public override void OnEnter()
	{
		base.OnEnter();
		dashDuration = Vector3.Distance(base.characterBody.master.GetComponent<BaseAI>().currentEnemy.characterBody.transform.position, base.transform.position) * 0.1f / speedCoefficient - gapToEnemy;
		Util.PlaySound(beginSoundString, base.gameObject);
		modelTransform = GetModelTransform();
		survivorBody = base.gameObject.GetComponent<CharacterBody>();
		if ((bool)base.cameraTargetParams)
		{
			aimRequest = base.cameraTargetParams.RequestAimType(CameraTargetParams.AimType.Aura);
		}
		if ((bool)modelTransform)
		{
			animator = modelTransform.GetComponent<Animator>();
			characterModel = modelTransform.GetComponent<CharacterModel>();
			childLocator = modelTransform.GetComponent<ChildLocator>();
			hurtboxGroup = modelTransform.GetComponent<HurtBoxGroup>();
			_ = (bool)childLocator;
		}
		SmallHop(base.characterMotor, smallHopVelocity);
		PlayAnimation("FullBody, Override", "StepBrothersPrep", "StepBrothersPrep.playbackRate", dashPrepDuration);
		dashVector = base.inputBank.aimDirection;
		overlapAttack = InitMeleeOverlap(0f, hitEffectPrefab, modelTransform, "StepBrothers");
		if (NetworkServer.active)
		{
			base.characterBody.AddBuff(RoR2Content.Buffs.HiddenInvincibility.buffIndex);
		}
	}

	private void CreateDashEffect()
	{
		Transform transform = childLocator.FindChild("DashCenter");
		if ((bool)transform && (bool)dashPrefab)
		{
			Object.Instantiate(dashPrefab, transform.position, Util.QuaternionSafeLookRotation(dashVector), transform);
		}
	}

	public override void FixedUpdate()
	{
		base.FixedUpdate();
		base.characterDirection.forward = dashVector;
		if (stopwatch > dashPrepDuration && !isDashing)
		{
			isDashing = true;
			dashVector = base.inputBank.aimDirection;
			CreateDashEffect();
			PlayCrossfade("FullBody, Override", "StepBrothersLoop", 0.1f);
			originalLayer = base.gameObject.layer;
			base.gameObject.layer = LayerIndex.GetAppropriateFakeLayerForTeam(base.teamComponent.teamIndex).intVal;
			base.characterMotor.Motor.RebuildCollidableLayers();
		}
		if (!isDashing)
		{
			stopwatch += GetDeltaTime();
		}
		else if (base.isAuthority)
		{
			base.characterMotor.velocity = Vector3.zero;
			if (!inHitPause)
			{
				bool num = overlapAttack.Fire();
				stopwatch += GetDeltaTime();
				if (num)
				{
					Vector3 position = base.gameObject.transform.position;
					EffectManager.SpawnEffect(explosionEffect, new EffectData
					{
						origin = position,
						scale = 1f
					}, transmit: true);
					explosionAttack = new BlastAttack();
					explosionAttack.attacker = base.gameObject;
					explosionAttack.inflictor = base.gameObject;
					explosionAttack.teamIndex = TeamComponent.GetObjectTeam(base.gameObject);
					explosionAttack.baseDamage = damageStat * damageCoefficient;
					explosionAttack.position = position;
					explosionAttack.radius = 20f;
					explosionAttack.Fire();
					if (!hasHit)
					{
						hasHit = true;
					}
					inHitPause = true;
					hitPauseTimer = hitPauseDuration / attackSpeedStat;
				}
				base.characterMotor.rootMotion += dashVector * moveSpeedStat * speedCoefficient * GetDeltaTime();
			}
			else
			{
				hitPauseTimer -= GetDeltaTime();
				if (hitPauseTimer < 0f)
				{
					inHitPause = false;
				}
			}
		}
		if (stopwatch >= dashDuration + dashPrepDuration && base.isAuthority)
		{
			outer.SetNextState(GetNextStateAuthority());
		}
	}

	public override void OnExit()
	{
		base.gameObject.layer = originalLayer;
		base.characterMotor.Motor.RebuildCollidableLayers();
		Util.PlaySound(endSoundString, base.gameObject);
		if (base.isAuthority)
		{
			base.characterMotor.velocity *= -0.3f;
			SmallHop(base.characterMotor, smallHopVelocity);
		}
		aimRequest?.Dispose();
		_ = (bool)childLocator;
		PlayAnimation("FullBody, Override", "StepBrothersLoopExit");
		if (NetworkServer.active)
		{
			base.characterBody.RemoveBuff(RoR2Content.Buffs.HiddenInvincibility.buffIndex);
		}
		base.OnExit();
	}

	public override void OnSerialize(NetworkWriter writer)
	{
		base.OnSerialize(writer);
		writer.Write((byte)dashIndex);
	}

	public override void OnDeserialize(NetworkReader reader)
	{
		base.OnDeserialize(reader);
		dashIndex = reader.ReadByte();
	}

	protected virtual EntityState GetNextStateAuthority()
	{
		return new FissureSlam();
	}

	public override InterruptPriority GetMinimumInterruptPriority()
	{
		return InterruptPriority.Frozen;
	}
}
