using DoomDeathmatch.Component.UI;
using DoomDeathmatch.Scene.Leaders;
using DoomDeathmatch.Scene.Rules;
using Engine.Asset.Font;
using Engine.Input;
using Engine.Scene;
using Engine.Scene.Component.BuiltIn;
using Engine.Scene.Component.BuiltIn.Renderer;
using OpenTK.Mathematics;

namespace DoomDeathmatch.Scene.Main;

public static class MainScene
{
  public static Engine.Scene.Scene Create(Engine.Engine parEngine)
  {
    var scene = new Engine.Scene.Scene();

    var (cameraObject, camera) = UiUtil.CreateOrthographicCamera(scene);

    AddMainUi(parEngine, scene, camera);

    return scene;
  }

  private static void AddMainUi(Engine.Engine parEngine, Engine.Scene.Scene parScene, Camera parCamera)
  {
    var (uiContainerObject, uiContainer) = UiUtil.CreateBackgroundUi(parScene, parCamera);

    var (playUiObject, playUi) =
      UiUtil.CreateTextUi(parScene, uiContainer, UiUtil.GetDoomFont(parEngine), "Играть");

    var (leadersUiObject, leadersUi) =
      UiUtil.CreateTextUi(parScene, uiContainer, UiUtil.GetDoomFont(parEngine), "Лидеры");

    var (rulesUiObject, rulesUi) =
      UiUtil.CreateTextUi(parScene, uiContainer, UiUtil.GetDoomFont(parEngine), "Правила");

    var (exitUiObject, exitUi) =
      UiUtil.CreateTextUi(parScene, uiContainer, UiUtil.GetDoomFont(parEngine), "Выход");

    var (stackObject, stack) = UiUtil.CreateStackUi(parScene, uiContainer,
      [playUi, leadersUi, rulesUi, exitUi]);
    stack.Offset = new Vector2(0, -1f);
    stackObject.Transform.Size = new Vector3(2f, 5f, 1f);

    playUi.OnClick += _ => Console.WriteLine("Play");
    leadersUi.OnClick += _ => parEngine.SceneManager.TransitionTo(LeadersScene.Create(parEngine));
    rulesUi.OnClick += _ => parEngine.SceneManager.TransitionTo(RulesScene.Create(parEngine));
    exitUi.OnClick += _ => parEngine.Close();

    var (logoObject, logoUi) = UiUtil.CreateLogoUi(parEngine, parScene, uiContainer);

    var selectorObject = new GameObject
    {
      Transform = { Translation = new Vector3(0, 0, -1), Size = new Vector3(0.5f, 1f, 1f) }
    };
    selectorObject.AddComponent(new SelectorComponent
    {
      Children = { playUi, leadersUi, rulesUi, exitUi },
      SelectKey = KeyboardButtonCode.Space,
      NextKey = KeyboardButtonCode.Down,
      PrevKey = KeyboardButtonCode.Up,
    });
    selectorObject.AddComponent(new Box2DRenderer { Color = new Vector4(1, 0, 0, 1) });

    parScene.AddChild(uiContainerObject, selectorObject);

    parScene.SetChild(uiContainerObject, stackObject);
    parScene.SetChild(uiContainerObject, logoObject);

    parScene.SetChild(stackObject, playUiObject);
    parScene.SetChild(stackObject, leadersUiObject);
    parScene.SetChild(stackObject, rulesUiObject);
    parScene.SetChild(stackObject, exitUiObject);
  }
}