using DoomDeathmatch.Component.MVC.Model;
using Engine.Input;

namespace DoomDeathmatch.Component.MVC.Controller;

public class PlayerController : Engine.Scene.Component.Component
{
  private readonly IInputHandler _inputHandler = Engine.Engine.Instance.InputHandler!;

  private HealthController _healthController = null!;
  private WeaponController _weaponController = null!;
  private ScoreController _scoreController = null!;

  public override void Awake()
  {
    _healthController = GameObject.GetComponent<HealthController>()!;
    _weaponController = GameObject.GetComponent<WeaponController>()!;
    _scoreController = GameObject.GetComponent<ScoreController>()!;

    ArgumentNullException.ThrowIfNull(_healthController);
    ArgumentNullException.ThrowIfNull(_weaponController);
    ArgumentNullException.ThrowIfNull(_scoreController);
  }

  public override void Update(double parDeltaTime)
  {
    if (_inputHandler.IsKeyJustPressed(KeyboardButtonCode.C))
      _weaponController.AddWeapon(WeaponData.Shotgun);

    for (var i = 0; i < 10; i++)
    {
      if (KeyboardButtonCode.D1 + i > KeyboardButtonCode.D0)
        break;

      if (!_inputHandler.IsKeyJustPressed(KeyboardButtonCode.D1 + i))
        continue;

      _weaponController.SelectWeapon(i);
      break;
    }

    if (_inputHandler.IsKeyJustPressed(KeyboardButtonCode.Space))
      _weaponController.TryShoot();
  }
}