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

// SetPinnedSavedMessagesTopicsRequest represents TL type `setPinnedSavedMessagesTopics#475a1110`.
type SetPinnedSavedMessagesTopicsRequest struct {
	// Identifiers of the new pinned Saved Messages topics
	SavedMessagesTopicIDs []int64
}

// SetPinnedSavedMessagesTopicsRequestTypeID is TL type id of SetPinnedSavedMessagesTopicsRequest.
const SetPinnedSavedMessagesTopicsRequestTypeID = 0x475a1110

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

func (s *SetPinnedSavedMessagesTopicsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.SavedMessagesTopicIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetPinnedSavedMessagesTopicsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setPinnedSavedMessagesTopics",
		ID:   SetPinnedSavedMessagesTopicsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SavedMessagesTopicIDs",
			SchemaName: "saved_messages_topic_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetPinnedSavedMessagesTopicsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPinnedSavedMessagesTopics#475a1110 as nil")
	}
	b.PutID(SetPinnedSavedMessagesTopicsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetPinnedSavedMessagesTopicsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPinnedSavedMessagesTopics#475a1110 as nil")
	}
	b.PutInt(len(s.SavedMessagesTopicIDs))
	for _, v := range s.SavedMessagesTopicIDs {
		b.PutInt53(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetPinnedSavedMessagesTopicsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPinnedSavedMessagesTopics#475a1110 to nil")
	}
	if err := b.ConsumeID(SetPinnedSavedMessagesTopicsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setPinnedSavedMessagesTopics#475a1110: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetPinnedSavedMessagesTopicsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPinnedSavedMessagesTopics#475a1110 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode setPinnedSavedMessagesTopics#475a1110: field saved_messages_topic_ids: %w", err)
		}

		if headerLen > 0 {
			s.SavedMessagesTopicIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setPinnedSavedMessagesTopics#475a1110: field saved_messages_topic_ids: %w", err)
			}
			s.SavedMessagesTopicIDs = append(s.SavedMessagesTopicIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetPinnedSavedMessagesTopicsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setPinnedSavedMessagesTopics#475a1110 as nil")
	}
	b.ObjStart()
	b.PutID("setPinnedSavedMessagesTopics")
	b.Comma()
	b.FieldStart("saved_messages_topic_ids")
	b.ArrStart()
	for _, v := range s.SavedMessagesTopicIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetPinnedSavedMessagesTopicsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setPinnedSavedMessagesTopics#475a1110 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setPinnedSavedMessagesTopics"); err != nil {
				return fmt.Errorf("unable to decode setPinnedSavedMessagesTopics#475a1110: %w", err)
			}
		case "saved_messages_topic_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode setPinnedSavedMessagesTopics#475a1110: field saved_messages_topic_ids: %w", err)
				}
				s.SavedMessagesTopicIDs = append(s.SavedMessagesTopicIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode setPinnedSavedMessagesTopics#475a1110: field saved_messages_topic_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSavedMessagesTopicIDs returns value of SavedMessagesTopicIDs field.
func (s *SetPinnedSavedMessagesTopicsRequest) GetSavedMessagesTopicIDs() (value []int64) {
	if s == nil {
		return
	}
	return s.SavedMessagesTopicIDs
}

// SetPinnedSavedMessagesTopics invokes method setPinnedSavedMessagesTopics#475a1110 returning error if any.
func (c *Client) SetPinnedSavedMessagesTopics(ctx context.Context, savedmessagestopicids []int64) error {
	var ok Ok

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