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

// SetChatPinnedStoriesRequest represents TL type `setChatPinnedStories#f5cb679f`.
type SetChatPinnedStoriesRequest struct {
	// Identifier of the chat that posted the stories
	ChatID int64
	// New list of pinned stories. All stories must be posted to the chat page first. There
	// can be up to getOption("pinned_story_count_max") pinned stories on a chat page
	StoryIDs []int32
}

// SetChatPinnedStoriesRequestTypeID is TL type id of SetChatPinnedStoriesRequest.
const SetChatPinnedStoriesRequestTypeID = 0xf5cb679f

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

func (s *SetChatPinnedStoriesRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.StoryIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetChatPinnedStoriesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setChatPinnedStories",
		ID:   SetChatPinnedStoriesRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "StoryIDs",
			SchemaName: "story_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetChatPinnedStoriesRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatPinnedStories#f5cb679f as nil")
	}
	b.PutID(SetChatPinnedStoriesRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetChatPinnedStoriesRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatPinnedStories#f5cb679f as nil")
	}
	b.PutInt53(s.ChatID)
	b.PutInt(len(s.StoryIDs))
	for _, v := range s.StoryIDs {
		b.PutInt32(v)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetChatPinnedStoriesRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatPinnedStories#f5cb679f to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setChatPinnedStories#f5cb679f: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode setChatPinnedStories#f5cb679f: field story_ids: %w", err)
		}

		if headerLen > 0 {
			s.StoryIDs = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode setChatPinnedStories#f5cb679f: field story_ids: %w", err)
			}
			s.StoryIDs = append(s.StoryIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetChatPinnedStoriesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatPinnedStories#f5cb679f as nil")
	}
	b.ObjStart()
	b.PutID("setChatPinnedStories")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("story_ids")
	b.ArrStart()
	for _, v := range s.StoryIDs {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setChatPinnedStories"); err != nil {
				return fmt.Errorf("unable to decode setChatPinnedStories#f5cb679f: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setChatPinnedStories#f5cb679f: field chat_id: %w", err)
			}
			s.ChatID = value
		case "story_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode setChatPinnedStories#f5cb679f: field story_ids: %w", err)
				}
				s.StoryIDs = append(s.StoryIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode setChatPinnedStories#f5cb679f: field story_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (s *SetChatPinnedStoriesRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

// GetStoryIDs returns value of StoryIDs field.
func (s *SetChatPinnedStoriesRequest) GetStoryIDs() (value []int32) {
	if s == nil {
		return
	}
	return s.StoryIDs
}

// SetChatPinnedStories invokes method setChatPinnedStories#f5cb679f returning error if any.
func (c *Client) SetChatPinnedStories(ctx context.Context, request *SetChatPinnedStoriesRequest) error {
	var ok Ok

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