package getsvc

import (
	"context"

	objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
	objectSvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object"
	objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
)

type streamObjectWriter struct {
	objectSvc.GetObjectStream
}

type streamObjectRangeWriter struct {
	objectSvc.GetObjectRangeStream
}

func (s *streamObjectWriter) WriteHeader(_ context.Context, obj *objectSDK.Object) error {
	p := new(objectV2.GetObjectPartInit)

	objV2 := obj.ToV2()
	p.SetObjectID(objV2.GetObjectID())
	p.SetHeader(objV2.GetHeader())
	p.SetSignature(objV2.GetSignature())

	return s.GetObjectStream.Send(newResponse(p))
}

func (s *streamObjectWriter) WriteChunk(_ context.Context, chunk []byte) error {
	p := new(objectV2.GetObjectPartChunk)
	p.SetChunk(chunk)

	return s.GetObjectStream.Send(newResponse(p))
}

func newResponse(p objectV2.GetObjectPart) *objectV2.GetResponse {
	r := new(objectV2.GetResponse)

	body := new(objectV2.GetResponseBody)
	r.SetBody(body)

	body.SetObjectPart(p)

	return r
}

func (s *streamObjectRangeWriter) WriteChunk(_ context.Context, chunk []byte) error {
	return s.GetObjectRangeStream.Send(newRangeResponse(chunk))
}

func newRangeResponse(p []byte) *objectV2.GetRangeResponse {
	r := new(objectV2.GetRangeResponse)

	body := new(objectV2.GetRangeResponseBody)
	r.SetBody(body)

	part := new(objectV2.GetRangePartChunk)
	part.SetChunk(p)

	body.SetRangePart(part)

	return r
}