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

// GetOwnedStickerSetsRequest represents TL type `getOwnedStickerSets#58fe8120`.
type GetOwnedStickerSetsRequest struct {
	// Identifier of the sticker set from which to return owned sticker sets; use 0 to get
	// results from the beginning
	OffsetStickerSetID int64
	// The maximum number of sticker sets to be returned; must be positive and can't be
	// greater than 100. For optimal performance, the number of returned objects is chosen by
	// TDLib and can be smaller than the specified limit
	Limit int32
}

// GetOwnedStickerSetsRequestTypeID is TL type id of GetOwnedStickerSetsRequest.
const GetOwnedStickerSetsRequestTypeID = 0x58fe8120

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

func (g *GetOwnedStickerSetsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.OffsetStickerSetID == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetOwnedStickerSetsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getOwnedStickerSets",
		ID:   GetOwnedStickerSetsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OffsetStickerSetID",
			SchemaName: "offset_sticker_set_id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetOwnedStickerSetsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getOwnedStickerSets#58fe8120 as nil")
	}
	b.PutID(GetOwnedStickerSetsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetOwnedStickerSetsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getOwnedStickerSets#58fe8120 as nil")
	}
	b.PutLong(g.OffsetStickerSetID)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetOwnedStickerSetsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getOwnedStickerSets#58fe8120 to nil")
	}
	if err := b.ConsumeID(GetOwnedStickerSetsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getOwnedStickerSets#58fe8120: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetOwnedStickerSetsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getOwnedStickerSets#58fe8120 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode getOwnedStickerSets#58fe8120: field offset_sticker_set_id: %w", err)
		}
		g.OffsetStickerSetID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getOwnedStickerSets#58fe8120: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetOwnedStickerSetsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getOwnedStickerSets#58fe8120 as nil")
	}
	b.ObjStart()
	b.PutID("getOwnedStickerSets")
	b.Comma()
	b.FieldStart("offset_sticker_set_id")
	b.PutLong(g.OffsetStickerSetID)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetOwnedStickerSetsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getOwnedStickerSets#58fe8120 to nil")
	}

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

// GetOffsetStickerSetID returns value of OffsetStickerSetID field.
func (g *GetOwnedStickerSetsRequest) GetOffsetStickerSetID() (value int64) {
	if g == nil {
		return
	}
	return g.OffsetStickerSetID
}

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

// GetOwnedStickerSets invokes method getOwnedStickerSets#58fe8120 returning error if any.
func (c *Client) GetOwnedStickerSets(ctx context.Context, request *GetOwnedStickerSetsRequest) (*StickerSets, error) {
	var result StickerSets

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