using DoomDeathmatch.Component.UI;
using DoomDeathmatch.Scene.Leaders;
using DoomDeathmatch.Scene.Play;
using DoomDeathmatch.Scene.Rules;
using Engine.Scene.Component.BuiltIn;
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);

    var (uiContainerObject, uiContainer) =
      UiUtil.CreateBackgroundUi(scene, new UiContainerComponent { Camera = camera });

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

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

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

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

    var (stackObject, stack) = UiUtil.CreateStackUi(scene,
      new StackComponent
      {
        Offset = new Vector2(0, -1f), Container = uiContainer, Children = { playUi, leadersUi, rulesUi, exitUi }
      });
    stackObject.Transform.Size.Xy = new Vector2(1f, 6f);

    playUi.OnClick += _ => parEngine.SceneManager.TransitionTo(() => PlayScene.Create(parEngine));
    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, scene, uiContainer);
    logoUi.Offset = new Vector2(0, 3f);

    var (selectorObject, selector) = UiUtil.CreateSelectorUi(scene,
      new SelectorComponent { Children = { playUi, leadersUi, rulesUi, exitUi } });

    scene.AddChild(uiContainerObject, selectorObject);

    scene.AddChild(uiContainerObject, stackObject);
    scene.AddChild(uiContainerObject, logoObject);

    scene.AddChild(stackObject, playUiObject);
    scene.AddChild(stackObject, leadersUiObject);
    scene.AddChild(stackObject, rulesUiObject);
    scene.AddChild(stackObject, exitUiObject);

    return scene;
  }
}