using System.Diagnostics;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas;
using Fantasy.Event;
using Fantasy.Helper;
using Fantasy.Serialize;
using NB.Game;
using NB.Map;
using NBF;
using ProtoBuf;

namespace NB;

public sealed class SubSceneTestComponent : Entity
{
    public override void Dispose()
    {
        Log.Debug("销毁SubScene下的SubSceneTestComponent");
        base.Dispose();
    }
}

public sealed class OnCreateSceneEvent : AsyncEventSystem<OnCreateScene>
{
    private static long _addressableSceneRunTimeId;

    /// <summary>
    /// Handles the OnCreateScene event.
    /// </summary> 
    /// <param name="self">The OnCreateScene object.</param>
    /// <returns>A task representing the asynchronous operation.</returns>
    protected override async FTask Handler(OnCreateScene self)
    {
        // var epoch1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000;
        //
        // {
        //     var now = TimeHelper.Transition(new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc));
        //     var epochThisYear = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000 - epoch1970;
        //     var time = (uint)((now - epochThisYear) / 1000);
        //     Log.Debug($"time = {time} now = {now} epochThisYear = {epochThisYear}");
        // }
        //
        // {
        //     var now = TimeHelper.Transition(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc));
        //     var epochThisYear = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000 - epoch1970;
        //     var time = (uint)((now - epochThisYear) / 1000);
        //     Log.Debug($"time = {time} now = {now} epochThisYear = {epochThisYear}");
        // }

        var scene = self.Scene;

        switch (scene.SceneType)
        {
            case 6666:
            {
                var subSceneTestComponent = scene.AddComponent<SubSceneTestComponent>();
                Log.Debug("增加了SubSceneTestComponent");
                scene.EntityComponent.CustomSystem(subSceneTestComponent, CustomSystemType.RunSystem);
                break;
            }
            case SceneType.Addressable:
            {
                // scene.AddComponent<AddressableManageComponent>(); 
                _addressableSceneRunTimeId = scene.RuntimeId;
                break;
            }
            case SceneType.Map:
            {
                Log.Debug($"Map Scene  SceneRuntimeId:{scene.RuntimeId}");
                
                var roomCode = RoomHelper.GenerateCode(scene.SceneConfigId, 1);
                Log.Info($"测试 roomCode：{roomCode}");
                // uint serverId = 25255;
                // for (int i = 1; i < 65535; i++)
                // {
                //     var roomId = RoomHelper.GenerateCode(serverId, i);
                //     RoomHelper.ParseCode(roomId, out var pId, out var rId);
                //     Log.Info($"生成id测试，房间id={roomId} 原始服务id：{serverId} 原始房间id={i} 解析={pId} {rId}");
                // }

                break;
            }
            case SceneType.Social:
            {
                break;
            }
            case SceneType.Game:
            {
                var rod = RodConfig.Get(30001);
                Log.Info("rod config id="+rod.Id);
                // // Begins transaction
                // using (var session = mongoClient.StartSession())
                // {
                //     session.StartTransaction();
                //     try
                //     {
                //         // Creates sample data
                //         var book = new Book
                //         {
                //             Title = "Beloved",
                //             Author = "Toni Morrison",
                //             InStock = true
                //         };
                //         var film = new Film
                //         {
                //             Title = "Star Wars",
                //             Director = "George Lucas",
                //             InStock = true
                //         };
                //         // Inserts sample data
                //         books.InsertOne(session, book);
                //         films.InsertOne(session, film);
                //         // Commits our transaction
                //         session.CommitTransaction();
                //     } 
                //     catch (Exception e)
                //     {
                //         Console.WriteLine("Error writing to MongoDB: " + e.Message);
                //         return;
                //     }
                //     // Prints a success message if no error thrown
                //     Console.WriteLine("Successfully committed transaction!");
                // }

                // var client = self.Scene.World.DataBase;

                // List<FTask> tasks = new List<FTask>();
                // Stopwatch stopwatch = new Stopwatch();
                // stopwatch.Start();
                // for (int i = 0; i < 100; i++)
                // {
                //     var accountId = scene.EntityIdFactory.Create;
                //     var account = PlayerFactory.Create(scene, accountId);
                //
                //     account.Level = 99;
                //     account.NickName = $"测试号{i + 1}";
                //     account.Country = "cn";
                //     account.Exp = 999;
                //     account.Head = "xxx.png";
                //     tasks.Add(account.Save());
                // }
                // await FTask.WaitAll(tasks);
                // // self.Scene.World.DataBase.InsertBatch()
                //
                // stopwatch.Stop();
                // Log.Info($"创建100个号入库耗时={stopwatch.ElapsedMilliseconds}ms");


                break;
            }
            case SceneType.Gate:
            {
                // var tasks = new List<FTask>(2000);
                // var session = scene.GetSession(_addressableSceneRunTimeId);
                // var sceneNetworkMessagingComponent = scene.NetworkMessagingComponent;
                // var g2ATestRequest = new G2A_TestRequest();
                //
                // async FTask Call()
                // {
                //     await sceneNetworkMessagingComponent.CallInnerRouteBySession(session,_addressableSceneRunTimeId,g2ATestRequest);
                // }
                //
                // for (int i = 0; i < 100000000000; i++)
                // {
                //     tasks.Clear();
                //     for (int j = 0; j < tasks.Capacity; ++j)
                //     {
                //         tasks.Add(Call());
                //     }
                //     await FTask.WaitAll(tasks);
                // }

                // 执行自定义系统
                var testCustomSystemComponent = scene.AddComponent<TestCustomSystemComponent>();
                scene.EntityComponent.CustomSystem(testCustomSystemComponent, CustomSystemType.RunSystem);
                // // 测试配置表
                // var instanceList = UnitConfigData.Instance.List;
                // var unitConfig = instanceList[0];
                // Log.Debug(instanceList[0].Dic[1]);
                break;
            }
        }

        await FTask.CompletedTask;
    }
}