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

// LoadSavedMessagesTopicsRequest represents TL type `loadSavedMessagesTopics#1146d6b8`.
type LoadSavedMessagesTopicsRequest struct {
	// The maximum number of topics to be loaded. For optimal performance, the number of
	// loaded topics is chosen by TDLib and can be smaller than the specified limit, even if
	// the end of the list is not reached
	Limit int32
}

// LoadSavedMessagesTopicsRequestTypeID is TL type id of LoadSavedMessagesTopicsRequest.
const LoadSavedMessagesTopicsRequestTypeID = 0x1146d6b8

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

func (l *LoadSavedMessagesTopicsRequest) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LoadSavedMessagesTopicsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "loadSavedMessagesTopics",
		ID:   LoadSavedMessagesTopicsRequestTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LoadSavedMessagesTopicsRequest) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode loadSavedMessagesTopics#1146d6b8 as nil")
	}
	b.PutID(LoadSavedMessagesTopicsRequestTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LoadSavedMessagesTopicsRequest) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode loadSavedMessagesTopics#1146d6b8 as nil")
	}
	b.PutInt32(l.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (l *LoadSavedMessagesTopicsRequest) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode loadSavedMessagesTopics#1146d6b8 to nil")
	}
	if err := b.ConsumeID(LoadSavedMessagesTopicsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode loadSavedMessagesTopics#1146d6b8: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LoadSavedMessagesTopicsRequest) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode loadSavedMessagesTopics#1146d6b8 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode loadSavedMessagesTopics#1146d6b8: field limit: %w", err)
		}
		l.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LoadSavedMessagesTopicsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode loadSavedMessagesTopics#1146d6b8 as nil")
	}
	b.ObjStart()
	b.PutID("loadSavedMessagesTopics")
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(l.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LoadSavedMessagesTopicsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode loadSavedMessagesTopics#1146d6b8 to nil")
	}

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

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

// LoadSavedMessagesTopics invokes method loadSavedMessagesTopics#1146d6b8 returning error if any.
func (c *Client) LoadSavedMessagesTopics(ctx context.Context, limit int32) error {
	var ok Ok

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