// 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{}
)

// GetBlockedMessageSendersRequest represents TL type `getBlockedMessageSenders#8ce52f16`.
type GetBlockedMessageSendersRequest struct {
	// Block list from which to return users
	BlockList BlockListClass
	// Number of users and chats to skip in the result; must be non-negative
	Offset int32
	// The maximum number of users and chats to return; up to 100
	Limit int32
}

// GetBlockedMessageSendersRequestTypeID is TL type id of GetBlockedMessageSendersRequest.
const GetBlockedMessageSendersRequestTypeID = 0x8ce52f16

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

func (g *GetBlockedMessageSendersRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.BlockList == nil) {
		return false
	}
	if !(g.Offset == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetBlockedMessageSendersRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getBlockedMessageSenders",
		ID:   GetBlockedMessageSendersRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BlockList",
			SchemaName: "block_list",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetBlockedMessageSendersRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBlockedMessageSenders#8ce52f16 as nil")
	}
	b.PutID(GetBlockedMessageSendersRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetBlockedMessageSendersRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBlockedMessageSenders#8ce52f16 as nil")
	}
	if g.BlockList == nil {
		return fmt.Errorf("unable to encode getBlockedMessageSenders#8ce52f16: field block_list is nil")
	}
	if err := g.BlockList.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getBlockedMessageSenders#8ce52f16: field block_list: %w", err)
	}
	b.PutInt32(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetBlockedMessageSendersRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBlockedMessageSenders#8ce52f16 to nil")
	}
	if err := b.ConsumeID(GetBlockedMessageSendersRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getBlockedMessageSenders#8ce52f16: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetBlockedMessageSendersRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBlockedMessageSenders#8ce52f16 to nil")
	}
	{
		value, err := DecodeBlockList(b)
		if err != nil {
			return fmt.Errorf("unable to decode getBlockedMessageSenders#8ce52f16: field block_list: %w", err)
		}
		g.BlockList = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getBlockedMessageSenders#8ce52f16: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getBlockedMessageSenders#8ce52f16: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetBlockedMessageSendersRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getBlockedMessageSenders#8ce52f16 as nil")
	}
	b.ObjStart()
	b.PutID("getBlockedMessageSenders")
	b.Comma()
	b.FieldStart("block_list")
	if g.BlockList == nil {
		return fmt.Errorf("unable to encode getBlockedMessageSenders#8ce52f16: field block_list is nil")
	}
	if err := g.BlockList.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getBlockedMessageSenders#8ce52f16: field block_list: %w", err)
	}
	b.Comma()
	b.FieldStart("offset")
	b.PutInt32(g.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetBlockedMessageSendersRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getBlockedMessageSenders#8ce52f16 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getBlockedMessageSenders"); err != nil {
				return fmt.Errorf("unable to decode getBlockedMessageSenders#8ce52f16: %w", err)
			}
		case "block_list":
			value, err := DecodeTDLibJSONBlockList(b)
			if err != nil {
				return fmt.Errorf("unable to decode getBlockedMessageSenders#8ce52f16: field block_list: %w", err)
			}
			g.BlockList = value
		case "offset":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getBlockedMessageSenders#8ce52f16: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getBlockedMessageSenders#8ce52f16: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBlockList returns value of BlockList field.
func (g *GetBlockedMessageSendersRequest) GetBlockList() (value BlockListClass) {
	if g == nil {
		return
	}
	return g.BlockList
}

// GetOffset returns value of Offset field.
func (g *GetBlockedMessageSendersRequest) GetOffset() (value int32) {
	if g == nil {
		return
	}
	return g.Offset
}

// GetLimit returns value of Limit field.
func (g *GetBlockedMessageSendersRequest) GetLimit() (value int32) {
	if g == nil {
		return
	}
	return g.Limit
}

// GetBlockedMessageSenders invokes method getBlockedMessageSenders#8ce52f16 returning error if any.
func (c *Client) GetBlockedMessageSenders(ctx context.Context, request *GetBlockedMessageSendersRequest) (*MessageSenders, error) {
	var result MessageSenders

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