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

// GetGrossingWebAppBotsRequest represents TL type `getGrossingWebAppBots#6522ce1a`.
type GetGrossingWebAppBotsRequest struct {
	// Offset of the first entry to return as received from the previous request; use empty
	// string to get the first chunk of results
	Offset string
	// The maximum number of bots to be returned; up to 100
	Limit int32
}

// GetGrossingWebAppBotsRequestTypeID is TL type id of GetGrossingWebAppBotsRequest.
const GetGrossingWebAppBotsRequestTypeID = 0x6522ce1a

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (g *GetGrossingWebAppBotsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getGrossingWebAppBots#6522ce1a as nil")
	}
	b.PutID(GetGrossingWebAppBotsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetGrossingWebAppBotsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getGrossingWebAppBots#6522ce1a as nil")
	}
	b.PutString(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetGrossingWebAppBotsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getGrossingWebAppBots#6522ce1a to nil")
	}
	if err := b.ConsumeID(GetGrossingWebAppBotsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getGrossingWebAppBots#6522ce1a: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetGrossingWebAppBotsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getGrossingWebAppBots#6522ce1a to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getGrossingWebAppBots#6522ce1a: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getGrossingWebAppBots#6522ce1a: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetGrossingWebAppBotsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getGrossingWebAppBots#6522ce1a as nil")
	}
	b.ObjStart()
	b.PutID("getGrossingWebAppBots")
	b.Comma()
	b.FieldStart("offset")
	b.PutString(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 *GetGrossingWebAppBotsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getGrossingWebAppBots#6522ce1a to nil")
	}

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

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

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

// GetGrossingWebAppBots invokes method getGrossingWebAppBots#6522ce1a returning error if any.
func (c *Client) GetGrossingWebAppBots(ctx context.Context, request *GetGrossingWebAppBotsRequest) (*FoundUsers, error) {
	var result FoundUsers

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