using CMSMicroservice.Protobuf.Protos.NetworkMembership;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.NetworkMembershipCQ.Commands.JoinNetwork;
using CMSMicroservice.Application.NetworkMembershipCQ.Commands.MoveInNetwork;
using CMSMicroservice.Application.NetworkMembershipCQ.Commands.RemoveFromNetwork;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetUserNetworkPosition;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkTree;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkMembershipHistory;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkStatistics;

namespace CMSMicroservice.WebApi.Services;

public class NetworkMembershipService : NetworkMembershipContract.NetworkMembershipContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

    public NetworkMembershipService(IDispatchRequestToCQRS dispatchRequestToCQRS)
    {
        _dispatchRequestToCQRS = dispatchRequestToCQRS;
    }

    public override async Task<Empty> JoinNetwork(JoinNetworkRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<JoinNetworkRequest, JoinNetworkCommand>(request, context);
    }

    public override async Task<Empty> ChangeNetworkParent(ChangeNetworkParentRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<ChangeNetworkParentRequest, MoveInNetworkCommand>(request, context);
    }

    public override async Task<Empty> RemoveFromNetwork(RemoveFromNetworkRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<RemoveFromNetworkRequest, RemoveFromNetworkCommand>(request, context);
    }

    public override async Task<GetUserNetworkResponse> GetUserNetwork(GetUserNetworkRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetUserNetworkRequest, GetUserNetworkPositionQuery, GetUserNetworkResponse>(request, context);
    }

    public override async Task<GetNetworkTreeResponse> GetNetworkTree(GetNetworkTreeRequest request, ServerCallContext context)
    {

        return await _dispatchRequestToCQRS.Handle<GetNetworkTreeRequest, GetNetworkTreeQuery, GetNetworkTreeResponse>(request, context);
    }

    public override async Task<GetNetworkMembershipHistoryResponse> GetNetworkMembershipHistory(GetNetworkMembershipHistoryRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetNetworkMembershipHistoryRequest, GetNetworkMembershipHistoryQuery, GetNetworkMembershipHistoryResponse>(request, context);
    }

    public override async Task<GetNetworkStatisticsResponse> GetNetworkStatistics(GetNetworkStatisticsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetNetworkStatisticsRequest, GetNetworkStatisticsQuery, GetNetworkStatisticsResponse>(request, context);
    }
}
