using System.Collections.Generic;
using System.ComponentModel;
using Godot;
using Godot.Bridge;
using Godot.Collections;
using Godot.NativeInterop;

[ScriptPath("res://Scripts/Bullets/RulerBehavior.cs")]
public partial class RulerBehavior : Node2D
{
	private Entity entity;

	[Export(PropertyHint.None, "")]
	private Node2D bullet;

	[Export(PropertyHint.None, "")]
	private Array<int> exceptions = new Array<int> { 1 };

	private IBullet bulletData;

	public static List<Node2D> active = new List<Node2D>();

	private float cd;

	private bool hard;

	private static readonly Vector2 off = new Vector2(300f, 0f);

	public override void _EnterTree()
	{
		if (BattleDR.current == null)
		{
			QueueFree();
			return;
		}
		entity = GetParent<Entity>();
		bulletData = bullet as IBullet;
	}

	public override void _Process(double delta)
	{
		if (BattleDR.current != null && GodotObject.IsInstanceValid(BattleDR.current.activeBoard) && BattleDR.current?.activeBoard?.caller?.entity != entity)
		{
			BattleDR current = BattleDR.current;
			if (current != null && current.activeBoard?.timer > 1f)
			{
				BattleDR current2 = BattleDR.current;
				if (current2 != null && current2.activeBoard.IsInsideTree())
				{
					if (exceptions != null && (exceptions.Contains(BattleDR.current.activeBoard.id) || active.Count >= 2) && !active.Contains(bulletData.node))
					{
						return;
					}
					if (!active.Contains(bulletData.node))
					{
						entity.Modulate = Main.colorWhite;
						hard = entity.battleData.internalFlags.Contains("Bad");
						base.Visible = true;
						base.GlobalPosition = BattleDR.current.activeBoard.GlobalPosition;
						bulletData.node.Position = off;
						if (active.Count == 0)
						{
							base.GlobalRotationDegrees = GD.RandRange(0, 360);
						}
						else
						{
							base.GlobalRotationDegrees = active[0].GlobalRotationDegrees + 180f;
						}
						Main.SetActive(bulletData.node, state: true);
						active.Add(bulletData.node);
						if (active[0] == bulletData.node)
						{
							Audio.PlaySound("snd_arrow.wav");
						}
						cd = 0f;
						return;
					}
					cd += Main.deltaTime;
					if (cd < 30f)
					{
						bulletData.node.Position = off.Lerp(Vector2.Zero, Mathf.SmoothStep(0f, 1f, cd / 30f));
					}
					else if (cd < 40f)
					{
						if (active[0] == bulletData.node)
						{
							Audio.PlaySound("snd_impact_ch1.wav");
						}
						cd = 50f;
					}
					else if (cd < 55f)
					{
						bulletData.node.Position = Vector2.One * GD.RandRange(-1, 1);
					}
					else if (BattleDR.current.enemies.Count == 1)
					{
						bulletData.node.Position = bulletData.node.Position.Lerp(new Vector2(0f, 0f - Mathf.Abs(Mathf.Sin((cd - 55f) * 0.025f) * (float)(hard ? 65 : 45))), Main.deltaTime * 0.1f);
						base.GlobalRotationDegrees += Main.deltaTime * 0.5f;
					}
					else if (active[0] == bulletData.node)
					{
						bulletData.node.Position = bulletData.node.Position.Lerp(new Vector2(0f, Mathf.Sin((cd - 55f) * ((active.Count == 1) ? 0.05f : 0.025f)) * (float)(hard ? 35 : 25)), Main.deltaTime * 0.1f);
					}
					else
					{
						bulletData.node.Position = new Vector2(0f, 0f - active[0].Position.Y);
					}
					if (Engine.GetProcessFrames() % 10 == 0L)
					{
						bulletData.grazed = false;
					}
					return;
				}
			}
		}
		if (base.Visible)
		{
			base.Visible = false;
			active.Remove(bulletData.node);
			Main.SetActive(bullet, state: false);
		}
	}

	}