using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas.Interface;
using Unity.Mathematics;

namespace NB.Map;

public class MapUnitSystemDestroySystem : DestroySystem<MapUnit>
{
    protected override void Destroy(MapUnit self)
    {
        self.MapId = 0;
        self.Position = float3.zero;
        self.Rotation = float3.zero;
        self.GateRouteId = 0L;
    }
}

public static class MapUnitSystem
{
    public static async FTask<uint> EnterMap(this MapUnit self, int mapId)
    {
        var mapManageComponent = self.Scene.GetComponent<MapManageComponent>();

        var map = mapManageComponent.Get(mapId);
        if (map == null)
        {
            return ErrorCode.MapIdError;
        }

        if (map.Units.TryAdd(self.Id, self))
        {
            // 同步客户端
            // self.Scene.NetworkMessagingComponent.SendInnerRoute(self.GateRouteId, new Map2C_ChangeMap()
            // {
            //     Node = 1,
            //     MapId = mapId
            // });
        }

        self.MapId = mapId;


        await FTask.CompletedTask;


        return ErrorCode.Successful;
    }


    public static MapUnitInfo ToMapUnitInfo(this MapUnit self)
    {
        var ret = new MapUnitInfo()
        {
            Id = self.Id,
            Position = Vector3Info.Create(self.Scene),
            Rotation = Vector3Info.Create(self.Scene),
        };

        ret.Position.x = self.Position.x;
        ret.Position.y = self.Position.y;
        ret.Position.z = self.Position.z;

        ret.Rotation.x = self.Rotation.x;
        ret.Rotation.y = self.Rotation.y;
        ret.Rotation.z = self.Rotation.z;

        return ret;
    }
}