using DoomDeathmatch.Component.UI;
using Engine.Asset.Font;
using Engine.Graphics.Texture;
using Engine.Scene;
using Engine.Scene.Component.BuiltIn;
using Engine.Scene.Component.BuiltIn.Renderer;
using OpenTK.Mathematics;

namespace DoomDeathmatch;

public static class UiUtil
{
  public static Font GetDoomFont(Engine.Engine parEngine)
  {
    return parEngine.AssetResourceManager.Load<Font>("font/doom");
  }

  public static (GameObject, OrthographicCamera) CreateOrthographicCamera(Engine.Scene.Scene parScene)
  {
    var cameraObject = new GameObject();
    var camera = new OrthographicCamera();
    cameraObject.AddComponent(camera);

    parScene.Add(cameraObject);

    return (cameraObject, camera);
  }

  public static (GameObject, UiComponent) CreateTextUi(Engine.Scene.Scene parScene, UiContainerComponent parContainer,
    Font parFont, string parText)
  {
    var size = parFont.Measure(parText);
    var outerObject = new GameObject
    {
      Transform = { Size = new Vector3(size.X, size.Y, 1f), Translation = new Vector3(0, 0, -1) }
    };
    var uiComponent = new UiComponent { Container = parContainer, Anchor = Anchor.Center };
    outerObject.AddComponent(uiComponent);
    outerObject.AddComponent(new Box2DRenderer { Color = new Vector4(0, 0, 1, 1) });

    var innerObject = new GameObject { Transform = { Translation = new Vector3(0, 0, -1) } };
    innerObject.AddComponent(new TextRenderer { Font = parFont, Text = parText });
    innerObject.AddComponent(new TextAlignComponent { Alignment = TextAlignComponent.Align.Center });

    parScene.Add(outerObject);
    parScene.AddChild(outerObject, innerObject);

    return (outerObject, uiComponent);
  }

  public static (GameObject, UiContainerComponent) CreateBackgroundUi(Engine.Scene.Scene parScene, Camera parCamera)
  {
    var uiContainerObject = new GameObject();
    var uiContainer = new UiContainerComponent { Camera = parCamera };
    uiContainerObject.AddComponent(uiContainer);
    uiContainerObject.AddComponent(new Box2DRenderer { Color = new Vector4(0.1f, 0.1f, 0.1f, 1) });

    parScene.Add(uiContainerObject);

    return (uiContainerObject, uiContainer);
  }

  public static (GameObject, UiComponent) CreateLogoUi(Engine.Engine parEngine, Engine.Scene.Scene parScene,
    UiContainerComponent parContainer)
  {
    var logoObject = new GameObject
    {
      Transform =
      {
        Translation = new Vector3(0, 0, -10), Scale = new Vector3(3), Size = new Vector3(1.6385869565f, 1f, 1f)
      }
    };
    logoObject.AddComponent(new Box2DRenderer
    {
      Texture = parEngine.AssetResourceManager.Load<Texture>("texture/doom_logo.png")
    });

    var uiComponent = new UiComponent { Container = null, Anchor = Anchor.Center };

    logoObject.AddComponent(new UiComponent
    {
      Container = parContainer, Anchor = Anchor.Center, Offset = new Vector2(0, 3f)
    });

    parScene.Add(logoObject);

    return (logoObject, uiComponent);
  }

  public static (GameObject, StackComponent) CreateStackUi(Engine.Scene.Scene parScene,
    UiContainerComponent parContainer, List<UiComponent> parChildren, Orientation parOrientation = Orientation.Vertical)
  {
    var stack = new StackComponent
    {
      Container = parContainer,
      Anchor = Anchor.Center
    };

    stack.Children.AddRange(parChildren);
    foreach (var child in parChildren)
    {
      child.Container = stack;
    }

    var stackObject = new GameObject
    {
      Transform = { Translation = new Vector3(0, 0, -1) }
    };
    stackObject.AddComponent(stack);
    stackObject.AddComponent(new Box2DRenderer { Color = new Vector4(1, 0, 0, 1) });

    parScene.Add(stackObject);

    return (stackObject, stack);
  }
}