package object

import (
	"context"
	"fmt"

	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/util"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/util/response"
)

type ResponseService struct {
	respSvc *response.Service

	svc ServiceServer
}

type searchStreamResponser struct {
	util.ServerStream

	respWriter util.ResponseMessageWriter
}

type getStreamResponser struct {
	util.ServerStream

	respWriter util.ResponseMessageWriter
}

type getRangeStreamResponser struct {
	util.ServerStream

	respWriter util.ResponseMessageWriter
}

type putStreamResponser struct {
	stream *response.ClientMessageStreamer
}

// NewResponseService returns object service instance that passes internal service
// call to response service.
func NewResponseService(objSvc ServiceServer, respSvc *response.Service) *ResponseService {
	return &ResponseService{
		respSvc: respSvc,
		svc:     objSvc,
	}
}

func (s *getStreamResponser) Send(resp *object.GetResponse) error {
	return s.respWriter(resp)
}

func (s *ResponseService) Get(req *object.GetRequest, stream GetObjectStream) error {
	return s.svc.Get(req, &getStreamResponser{
		ServerStream: stream,
		respWriter: s.respSvc.HandleServerStreamRequest(func(resp util.ResponseMessage) error {
			return stream.Send(resp.(*object.GetResponse))
		}),
	})
}

func (s *putStreamResponser) Send(ctx context.Context, req *object.PutRequest) error {
	return s.stream.Send(ctx, req)
}

func (s *putStreamResponser) CloseAndRecv(ctx context.Context) (*object.PutResponse, error) {
	r, err := s.stream.CloseAndRecv(ctx)
	if err != nil {
		return nil, fmt.Errorf("(%T) could not receive response: %w", s, err)
	}

	return r.(*object.PutResponse), nil
}

func (s *ResponseService) Put() (PutObjectStream, error) {
	stream, err := s.svc.Put()
	if err != nil {
		return nil, fmt.Errorf("could not create Put object streamer: %w", err)
	}

	return &putStreamResponser{
		stream: s.respSvc.CreateRequestStreamer(
			func(ctx context.Context, req any) error {
				return stream.Send(ctx, req.(*object.PutRequest))
			},
			func(ctx context.Context) (util.ResponseMessage, error) {
				return stream.CloseAndRecv(ctx)
			},
		),
	}, nil
}

func (s *ResponseService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
	resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
		func(ctx context.Context, req any) (util.ResponseMessage, error) {
			return s.svc.Head(ctx, req.(*object.HeadRequest))
		},
	)
	if err != nil {
		return nil, err
	}

	return resp.(*object.HeadResponse), nil
}

func (s *searchStreamResponser) Send(resp *object.SearchResponse) error {
	return s.respWriter(resp)
}

func (s *ResponseService) Search(req *object.SearchRequest, stream SearchStream) error {
	return s.svc.Search(req, &searchStreamResponser{
		ServerStream: stream,
		respWriter: s.respSvc.HandleServerStreamRequest(func(resp util.ResponseMessage) error {
			return stream.Send(resp.(*object.SearchResponse))
		}),
	})
}

func (s *ResponseService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
	resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
		func(ctx context.Context, req any) (util.ResponseMessage, error) {
			return s.svc.Delete(ctx, req.(*object.DeleteRequest))
		},
	)
	if err != nil {
		return nil, err
	}

	return resp.(*object.DeleteResponse), nil
}

func (s *getRangeStreamResponser) Send(resp *object.GetRangeResponse) error {
	return s.respWriter(resp)
}

func (s *ResponseService) GetRange(req *object.GetRangeRequest, stream GetObjectRangeStream) error {
	return s.svc.GetRange(req, &getRangeStreamResponser{
		ServerStream: stream,
		respWriter: s.respSvc.HandleServerStreamRequest(func(resp util.ResponseMessage) error {
			return stream.Send(resp.(*object.GetRangeResponse))
		}),
	})
}

func (s *ResponseService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
	resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
		func(ctx context.Context, req any) (util.ResponseMessage, error) {
			return s.svc.GetRangeHash(ctx, req.(*object.GetRangeHashRequest))
		},
	)
	if err != nil {
		return nil, err
	}

	return resp.(*object.GetRangeHashResponse), nil
}