using DoomDeathmatch.Component.Util.Collision;
using Engine.Scene;
using Engine.Scene.Component.BuiltIn;
using Engine.Scene.Component.BuiltIn.Renderer;
using OpenTK.Mathematics;

namespace DoomDeathmatch;

public static class GameObjectUtil
{
  public static GameObject CreateGameObject(Engine.Scene.Scene parScene)
  {
    var gameObject = new GameObject();

    parScene.Add(gameObject);

    return gameObject;
  }

  public static GameObject CreateGameObject(Engine.Scene.Scene parScene, Transform parTransform)
  {
    var gameObject = new GameObject(parTransform);

    parScene.Add(gameObject);

    return gameObject;
  }

  public static GameObject CreateGameObject(Engine.Scene.Scene parScene,
    List<Engine.Scene.Component.Component> parComponents)
  {
    var gameObject = new GameObject();
    foreach (var component in parComponents)
    {
      gameObject.AddComponent(component);
    }

    parScene.Add(gameObject);

    return gameObject;
  }

  public static GameObject CreateGameObject(Engine.Scene.Scene parScene, Transform parTransform,
    List<Engine.Scene.Component.Component> parComponents)
  {
    var gameObject = new GameObject(parTransform);
    foreach (var component in parComponents)
    {
      gameObject.AddComponent(component);
    }

    parScene.Add(gameObject);

    return gameObject;
  }

  public static GameObject CreateColliderForceField(Engine.Scene.Scene parScene, AABBColliderComponent parCollider)
  {
    var offset = parCollider.Offset;
    parCollider.Offset = Vector3.Zero;

    var forceFieldObject = CreateGameObject(parScene,
      new Transform { Translation = offset, Size = parCollider.Collider.Size },
      [
        parCollider,
        new ColliderForceFieldComponent()
      ]
    );

    // var color = new Vector4(1, 0, 0, 0.5f);
    // var size = parCollider.Collider.Size;
    //
    // var frontPlaneObject = CreateGameObject(parScene,
    //   new Transform
    //   {
    //     Translation = new Vector3(0, -size.Y / 2, 0),
    //     Size = new Vector3(size.X, size.Z, 0),
    //     Rotation = Quaternion.FromAxisAngle(Vector3.UnitX, MathF.PI / 2)
    //   },
    //   [
    //     new Box2DRenderer { Color = color }
    //   ]
    // );
    //
    // var backPlaneObject = CreateGameObject(parScene,
    //   new Transform
    //   {
    //     Translation = new Vector3(0, size.Y / 2, 0),
    //     Size = new Vector3(size.X, size.Z, 0),
    //     Rotation = Quaternion.FromAxisAngle(Vector3.UnitX, -MathF.PI / 2)
    //   },
    //   [
    //     new Box2DRenderer { Color = color }
    //   ]
    // );
    //
    // var leftPlaneObject = CreateGameObject(parScene,
    //   new Transform
    //   {
    //     Translation = new Vector3(-size.X / 2, 0, 0),
    //     Size = new Vector3(size.Z, size.Y, 0),
    //     Rotation = Quaternion.FromAxisAngle(Vector3.UnitY, MathF.PI / 2)
    //   },
    //   [
    //     new Box2DRenderer { Color = color }
    //   ]
    // );
    //
    // var rightPlaneObject = CreateGameObject(parScene,
    //   new Transform
    //   {
    //     Translation = new Vector3(size.X / 2, 0, 0),
    //     Size = new Vector3(size.Z, size.Y, 0),
    //     Rotation = Quaternion.FromAxisAngle(Vector3.UnitY, -MathF.PI / 2)
    //   },
    //   [
    //     new Box2DRenderer { Color = color }
    //   ]
    // );
    //
    // var topPlaneObject = CreateGameObject(parScene,
    //   new Transform { Translation = new Vector3(0, 0, size.Z / 2), Size = new Vector3(size.X, size.Y, 0) },
    //   [
    //     new Box2DRenderer { Color = color }
    //   ]
    // );
    //
    // var bottomPlaneObject = CreateGameObject(parScene,
    //   new Transform { Translation = new Vector3(0, 0, -size.Z / 2), Size = new Vector3(size.X, size.Y, 0) },
    //   [
    //     new Box2DRenderer { Color = color }
    //   ]
    // );
    //
    // parScene.AddChild(forceFieldObject, frontPlaneObject);
    // parScene.AddChild(forceFieldObject, backPlaneObject);
    // parScene.AddChild(forceFieldObject, leftPlaneObject);
    // parScene.AddChild(forceFieldObject, rightPlaneObject);
    // parScene.AddChild(forceFieldObject, topPlaneObject);
    // parScene.AddChild(forceFieldObject, bottomPlaneObject);

    return forceFieldObject;
  }
}