using System;
using Godot;

namespace NodeWar.Scripts;

public partial class Pip : Area2D
{
	[Export]
	public Faction Faction;

	[Export]
	public Base Target;

	[Export]
	public float Speed { get; set; } = 100;

	private Sprite2D _gfx;

	public override void _Ready()
	{
		_gfx = GetNode<Sprite2D>("Gfx");

		_gfx.Modulate = Faction switch
		{
			Faction.Neutral => Colors.Gray,
			Faction.Player => Colors.DarkGreen,
			Faction.Computer => Colors.DarkRed,
			_ => throw new ArgumentOutOfRangeException(),
		};

		AreaEntered += OnAreaEntered;
	}

	public override void _Process(double delta)
	{
		if (Target == null)
			return;

		var direction = Target.Position - GlobalPosition;
		Position += direction.Normalized() * Speed * delta.ToFloat();
	}

	private void OnAreaEntered(Node2D body)
	{
		if (body is Base { Faction: Faction.Computer } enteredBase)
		{
			enteredBase.HealthComponent.TakeDamage(1);
			QueueFree();
		}
	}
}
