using DoomDeathmatch.Script.Condition;
using DoomDeathmatch.Script.Provider;
using Engine.Scene;
using OpenTK.Mathematics;

namespace DoomDeathmatch.Script;

public class ObjectSpawner : IUpdate
{
  public event Action<GameObject>? OnSpawned;

  private readonly IValueProvider<GameObject> _gameObjectProvider;
  private readonly IValueProvider<Vector3> _positionProvider;
  private readonly ICondition _condition;

  public ObjectSpawner(IValueProvider<GameObject> parGameObjectProvider, IValueProvider<Vector3> parPositionProvider,
    ICondition parCondition)
  {
    _gameObjectProvider = parGameObjectProvider;
    _positionProvider = parPositionProvider;
    _condition = parCondition;

    _condition.OnTrue += Spawn;
  }

  public void Update(double parDeltaTime)
  {
    _condition.Update(parDeltaTime);
  }

  private void Spawn()
  {
    var gameObject = _gameObjectProvider.GetValue();
    var position = _positionProvider.GetValue();
    gameObject.Transform.Translation = position;

    OnSpawned?.Invoke(gameObject);
    _condition.Reset();
  }
}