using DoomDeathmatch.Component.MVC.Controller;
using DoomDeathmatch.Component.MVC.Model.Enemy;
using DoomDeathmatch.Component.MVC.View;
using DoomDeathmatch.Component.UI;
using DoomDeathmatch.Component.Util;
using DoomDeathmatch.Component.Util.Collision;
using Engine.Graphics.Texture;
using Engine.Scene;
using Engine.Scene.Component.BuiltIn;
using Engine.Scene.Component.BuiltIn.Renderer;
using OpenTK.Mathematics;

namespace DoomDeathmatch.Scene.Play;

public static class EnemyObject
{
  public static GameObject CreateEnemy(Engine.Engine parEngine, Engine.Scene.Scene parScene, EnemyData parEnemyData)
  {
    var enemyHealthTextRenderer = new TextRenderer { Font = UiUtil.GetDoomFont(parEngine), Text = "Здоровье: 000" };
    var displayBox2DRenderer = new Box2DRenderer();

    var demonObject = GameObjectUtil.CreateGameObject(parScene,
    [
      new EnemyController(parEnemyData),
      new EnemyView(displayBox2DRenderer),

      new HealthController(parEnemyData.BaseHealth),
      new EnemyHealthView(enemyHealthTextRenderer),

      new MovementController(),
      new RigidbodyComponent(),
      new DragComponent { Drag = 5f, Coefficient = new Vector3(1, 1, 0) },

      new AABBColliderComponent
      {
        Collider = new AABBCollider { Size = new Vector3(1, 1, 2) },
        Offset = new Vector3(0, 0f, 1f),
        ColliderGroups = { "enemy" },
        ExcludeColliderCollideGroups = { "enemy" }
      },
    ]);
    var demonHealthContainer = GameObjectUtil.CreateGameObject(parScene,
      new Transform { Translation = new Vector3(0, 1.25f, 0f), Scale = new Vector3(0.2f) }
    );

    var demonHealthObject = GameObjectUtil.CreateGameObject(parScene, [
        enemyHealthTextRenderer,
        new TextAlignComponent { Alignment = TextAlignComponent.Align.Center }
      ]
    );
    var demonVisualObject = GameObjectUtil.CreateGameObject(parScene,
      new Transform { Translation = new Vector3(0, 0f, 1f), Size = new Vector3(1, 2, 1) }, [
        displayBox2DRenderer,
        new BillboardComponent(),
      ]
    );

    parScene.AddChild(demonObject, demonVisualObject);
    parScene.AddChild(demonVisualObject, demonHealthContainer);
    parScene.AddChild(demonHealthContainer, demonHealthObject);

    return demonObject;
  }

  public static GameObject CreateImpFireball(Engine.Scene.Scene parScene, Vector3 parPosition,
    Vector3 parVelocity, float parDamage, Transform? parBillboardTarget = null)
  {
    var rigidbodyComponent = new RigidbodyComponent();
    rigidbodyComponent.AddVelocity(parVelocity);

    var fireballObject = GameObjectUtil.CreateGameObject(parScene,
      new Transform { Translation = parPosition, Size = new Vector3(0.5f) },
      [
        rigidbodyComponent,
        new Box2DRenderer
        {
          Texture = Engine.Engine.Instance.AssetResourceManager.Load<Texture>("texture/fireball.png")
        },
        new BillboardComponent { Target = parBillboardTarget },
        new FireballComponent { Damage = parDamage },
        new AABBColliderComponent
        {
          Collider = new AABBCollider { Size = new Vector3(0.25f) }, ExcludeColliderCollideGroups = { "enemy" }
        }
      ]
    );

    return fireballObject;
  }
}