using Fantasy;
using Fantasy.Async;
using Fantasy.Network;
using Fantasy.Network.Interface;
using Fantasy.Platform.Net;
using NB.Map;

namespace NB.Gate;

public class C2G_EnterMapRequestHandler : MessageRPC<C2G_EnterMapRequest, G2C_EnterMapResponse>
{
    protected override async FTask Run(Session session, C2G_EnterMapRequest request, G2C_EnterMapResponse response,
        Action reply)
    {
        if (string.IsNullOrEmpty(request.RoomCode))
        {
            response.ErrorCode = ErrorCode.MapRoomIdError;
            return;
        }

        var gateUnitSessionComponent = session.GetComponent<GateUnitSessionComponent>();
        if (gateUnitSessionComponent == null)
        {
            response.ErrorCode = ErrorCode.ErrServer;
            return;
        }

        // 在缓存中检查该账号是否存在
        var gateUnitManageComponent = session.Scene.GetComponent<GateUnitManageComponent>();
        if (gateUnitManageComponent == null)
        {
            response.ErrorCode = ErrorCode.ErrServer;
            return;
        }

        var gateUnit = gateUnitManageComponent.Get(gateUnitSessionComponent.AccountID);
        if (gateUnit == null)
        {
            response.ErrorCode = ErrorCode.ErrServer;
            return;
        }


        uint serviceId = 0;
        int roomId = 0;
        if (!string.IsNullOrEmpty(request.RoomCode))
        {
            RoomHelper.ParseCode(request.RoomCode, out serviceId, out roomId);
            if (serviceId < 1 || roomId < 1)
            {
                response.ErrorCode = ErrorCode.MapRoomIdError;
                return;
            }
        }

        if (serviceId == 0)
        {
            var mapScenes = SceneConfigData.Instance.GetSceneBySceneType(SceneType.Map);
            serviceId = mapScenes.First().Id;
        }

        var sceneConfig = SceneConfigData.Instance.Get(serviceId);
        if (sceneConfig == null)
        {
            response.ErrorCode = ErrorCode.MapRoomIdError;
            return;
        }

        //先判断是否需要更换地图服务器
        var oldServerRouteId = gateUnit.GetAddress(sceneConfig.SceneType);
        if (oldServerRouteId != sceneConfig.RouteId)
        {
            //先退出旧的服务
            var ret = await gateUnit.Offline(session.RuntimeId, RouteType.MapRoute);
            if (ret != 0)
            {
                response.ErrorCode = ErrorCode.MapRoomIdError;
                return;
            }

            //再进入新的服务
            ret = await gateUnit.Online(RouteType.MapRoute, sceneConfig);
            if (ret != 0)
            {
                response.ErrorCode = ErrorCode.MapRoomIdError;
                return;
            }
        }

        //执行进入房间操作
        var roomResponse = (Map2G_EnterMapResponse)await session.Scene.NetworkMessagingComponent.CallInnerRoute(
            sceneConfig.RouteId, new G2Map_EnterMapRequest()
            {
                AccountId = gateUnit.Id,
                RoomCode = request.RoomCode,
                MapId = request.MapId
            });
        if (roomResponse.ErrorCode != 0)
        {
            response.ErrorCode = ErrorCode.MapRoomIdError;
            return;
        }

        gateUnit.RoomCode = request.RoomCode;

        response.Units = roomResponse.Units;
        response.RoomCode = request.RoomCode;
        request.MapId = request.MapId;
        response.ErrorCode = ErrorCode.Successful;
    }
}