// Code generated by gotdgen, DO NOT EDIT.

package tdapi

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// TestCallBytesRequest represents TL type `testCallBytes#d4215aa9`.
type TestCallBytesRequest struct {
	// Bytes to return
	X []byte
}

// TestCallBytesRequestTypeID is TL type id of TestCallBytesRequest.
const TestCallBytesRequestTypeID = 0xd4215aa9

// Ensuring interfaces in compile-time for TestCallBytesRequest.
var (
	_ bin.Encoder     = &TestCallBytesRequest{}
	_ bin.Decoder     = &TestCallBytesRequest{}
	_ bin.BareEncoder = &TestCallBytesRequest{}
	_ bin.BareDecoder = &TestCallBytesRequest{}
)

func (t *TestCallBytesRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.X == nil) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (t *TestCallBytesRequest) String() string {
	if t == nil {
		return "TestCallBytesRequest(nil)"
	}
	type Alias TestCallBytesRequest
	return fmt.Sprintf("TestCallBytesRequest%+v", Alias(*t))
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*TestCallBytesRequest) TypeID() uint32 {
	return TestCallBytesRequestTypeID
}

// TypeName returns name of type in TL schema.
func (*TestCallBytesRequest) TypeName() string {
	return "testCallBytes"
}

// TypeInfo returns info about TL type.
func (t *TestCallBytesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "testCallBytes",
		ID:   TestCallBytesRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "X",
			SchemaName: "x",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TestCallBytesRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testCallBytes#d4215aa9 as nil")
	}
	b.PutID(TestCallBytesRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TestCallBytesRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testCallBytes#d4215aa9 as nil")
	}
	b.PutBytes(t.X)
	return nil
}

// Decode implements bin.Decoder.
func (t *TestCallBytesRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode testCallBytes#d4215aa9 to nil")
	}
	if err := b.ConsumeID(TestCallBytesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode testCallBytes#d4215aa9: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *TestCallBytesRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode testCallBytes#d4215aa9 to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode testCallBytes#d4215aa9: field x: %w", err)
		}
		t.X = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TestCallBytesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode testCallBytes#d4215aa9 as nil")
	}
	b.ObjStart()
	b.PutID("testCallBytes")
	b.Comma()
	b.FieldStart("x")
	b.PutBytes(t.X)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *TestCallBytesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode testCallBytes#d4215aa9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("testCallBytes"); err != nil {
				return fmt.Errorf("unable to decode testCallBytes#d4215aa9: %w", err)
			}
		case "x":
			value, err := b.Bytes()
			if err != nil {
				return fmt.Errorf("unable to decode testCallBytes#d4215aa9: field x: %w", err)
			}
			t.X = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetX returns value of X field.
func (t *TestCallBytesRequest) GetX() (value []byte) {
	if t == nil {
		return
	}
	return t.X
}

// TestCallBytes invokes method testCallBytes#d4215aa9 returning error if any.
func (c *Client) TestCallBytes(ctx context.Context, x []byte) (*TestBytes, error) {
	var result TestBytes

	request := &TestCallBytesRequest{
		X: x,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
