using System;
using Godot;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using HttpClient = System.Net.Http.HttpClient;

namespace BITKit;
/// <summary>
/// 为Godot提供的BITApp加载服务
/// </summary>
public partial class BITAppForGodot : Node
{
	/// <summary>
	/// 依赖服务集合
	/// </summary>
	public static ServiceCollection ServiceCollection { get; private set; } = new();
	/// <summary>
	/// 依赖服务提供接口
	/// </summary>
	public static ServiceProvider ServiceProvider { get; private set; }
	/// <summary>
	/// 在构造函数中注册Logger
	/// </summary>
	public BITAppForGodot()
	{
		BIT4Log.OnLog += GD.Print;
		BIT4Log.OnWarning += GD.PushWarning;
		BIT4Log.OnNextLine += () => GD.Print();
		BIT4Log.OnException += x=>GD.PrintErr(x.ToString());
		
		//启动BITApp
		BITApp.Start();
		BIT4Log.Log<BITAppForGodot>("已创建BITApp");
	}

	public override async void _Ready()
	{
		BIT4Log.Log<BITAppForGodot>("正在创建BITWebApp");
		
		//添加测试用HttpClient
		ServiceCollection.AddSingleton<HttpClient>();
		
		//构造依赖服务提供接口
		ServiceProvider = ServiceCollection.BuildServiceProvider();

	}
	protected override void Dispose(bool disposing)
	{
#pragma warning disable CS4014
		//停止BITApp
		BITApp.Stop();
#pragma warning restore CS4014
		BIT4Log.Log<BITAppForGodot>("已安全退出App");
	}
}
