using DoomDeathmatch.Component.UI;
using Engine.Input;

namespace DoomDeathmatch.Component.MVC.Controller;

public class GameController : Engine.Scene.Component.Component
{
  public bool IsPaused { get; set; } = false;

  public PlayerController PlayerController => _playerController;
  public ScoreController ScoreController => _scoreController;

  private ScoreController _scoreController = null!;
  private TimerController _timerController = null!;
  private PlayerController _playerController = null!;

  private readonly MenuControllerComponent _menuController;
  private readonly IInputHandler _inputHandler = Engine.Engine.Instance.InputHandler!;

  public GameController(MenuControllerComponent parMenuController)
  {
    _menuController = parMenuController;
  }

  public override void Awake()
  {
    _scoreController = GameObject.GetComponent<ScoreController>()!;
    _timerController = GameObject.GetComponent<TimerController>()!;
    _playerController = GameObject.Scene!.FindFirstComponent<PlayerController>()!;

    ArgumentNullException.ThrowIfNull(_scoreController);
    ArgumentNullException.ThrowIfNull(_timerController);
    ArgumentNullException.ThrowIfNull(_playerController);
  }

  public void Unpause()
  {
    GameObject.Scene!.TimeScale = 1.0f;
    IsPaused = false;
    _menuController.SelectMenuItem("play");
  }

  public void Pause()
  {
    GameObject.Scene!.TimeScale = 0.0f;
    IsPaused = true;
    _menuController.SelectMenuItem("escape");
  }

  public override void Update(double parDeltaTime)
  {
    if (_inputHandler.IsKeyJustPressed(KeyboardButtonCode.Escape))
    {
      if (IsPaused)
      {
        Unpause();
      }
      else
      {
        Pause();
      }
    }
  }
}