using Godot;

public partial class MainMenu : Node2D
{
   private AnimationPlayer _animationPlayer;

   [Signal]
   public delegate void IntroAnimationsCompletedEventHandler();

   private PlayerInput _player1Input;
   private PlayerInput _player2Input;

   public override void _Ready()
   {
	  GetParent().GetNode<TextureRect>("MainMenu/UIAnimations/LoreSplash").Show();
	  _animationPlayer = GetTree().Root.GetNode<AnimationPlayer>("/root/Main/MainMenu/UIAnimations/AnimationPlayer");
	  _animationPlayer.Play("IntroLore");
	  var bgmPlayer = GetTree().Root.GetNode<BGMPlayer>("BgmPlayer");
	  bgmPlayer.SetBGMFromFilepath("Audio/BGM/TitleTheme.ogg");
	  bgmPlayer.PlayBGM();
	  _player1Input = new Player1Input();
	  _player2Input = new Player2Input();
	  _animationPlayer.AnimationFinished += OnIntroAnimationFinished;
   }

   public override void _UnhandledInput(InputEvent @event)
   {
	  if (_animationPlayer.IsPlaying() && (Input.IsActionJustReleased(_player1Input.Fire()) || Input.IsActionJustReleased(_player2Input.Fire())))
	  {
		 _animationPlayer.Seek(10);
		 GetNode<TextureButton>("NewGame/1Player").GrabFocus();
	  }
   }

   public void OnIntroAnimationFinished(StringName animationName)
   {
	  GetNode<TextureButton>("NewGame/1Player").GrabFocus();
   }


   private void OneSinglePlayerPressed()
   {
	  Hide();
	  var main = GetTree().Root.GetNode<Main>("/root/Main");
	  main.LoadLevel(0, 1);
   }

   private void OnTwoPlayerPressed()
   {
	  Hide();
	  var main = GetTree().Root.GetNode<Main>("/root/Main");
	  main.LoadLevel(0, 2);
   }

   private void OnQuitButtonPressed()
   {
	  GetTree().Quit();
   }
}
