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

// SearchPublicStoriesByTagRequest represents TL type `searchPublicStoriesByTag#69fbb14a`.
type SearchPublicStoriesByTagRequest struct {
	// Identifier of the chat that posted the stories to search for; pass 0 to search stories
	// in all chats
	StoryPosterChatID int64
	// Hashtag or cashtag to search for
	Tag string
	// 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 stories to be returned; up to 100. For optimal performance, the
	// number of returned stories is chosen by TDLib and can be smaller than the specified
	// limit
	Limit int32
}

// SearchPublicStoriesByTagRequestTypeID is TL type id of SearchPublicStoriesByTagRequest.
const SearchPublicStoriesByTagRequestTypeID = 0x69fbb14a

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

func (s *SearchPublicStoriesByTagRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.StoryPosterChatID == 0) {
		return false
	}
	if !(s.Tag == "") {
		return false
	}
	if !(s.Offset == "") {
		return false
	}
	if !(s.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchPublicStoriesByTagRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchPublicStoriesByTag",
		ID:   SearchPublicStoriesByTagRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryPosterChatID",
			SchemaName: "story_poster_chat_id",
		},
		{
			Name:       "Tag",
			SchemaName: "tag",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchPublicStoriesByTagRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicStoriesByTag#69fbb14a as nil")
	}
	b.PutID(SearchPublicStoriesByTagRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchPublicStoriesByTagRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicStoriesByTag#69fbb14a as nil")
	}
	b.PutInt53(s.StoryPosterChatID)
	b.PutString(s.Tag)
	b.PutString(s.Offset)
	b.PutInt32(s.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SearchPublicStoriesByTagRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchPublicStoriesByTag#69fbb14a to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: field story_poster_chat_id: %w", err)
		}
		s.StoryPosterChatID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: field tag: %w", err)
		}
		s.Tag = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: field offset: %w", err)
		}
		s.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchPublicStoriesByTagRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicStoriesByTag#69fbb14a as nil")
	}
	b.ObjStart()
	b.PutID("searchPublicStoriesByTag")
	b.Comma()
	b.FieldStart("story_poster_chat_id")
	b.PutInt53(s.StoryPosterChatID)
	b.Comma()
	b.FieldStart("tag")
	b.PutString(s.Tag)
	b.Comma()
	b.FieldStart("offset")
	b.PutString(s.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(s.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("searchPublicStoriesByTag"); err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: %w", err)
			}
		case "story_poster_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: field story_poster_chat_id: %w", err)
			}
			s.StoryPosterChatID = value
		case "tag":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: field tag: %w", err)
			}
			s.Tag = value
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: field offset: %w", err)
			}
			s.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByTag#69fbb14a: field limit: %w", err)
			}
			s.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStoryPosterChatID returns value of StoryPosterChatID field.
func (s *SearchPublicStoriesByTagRequest) GetStoryPosterChatID() (value int64) {
	if s == nil {
		return
	}
	return s.StoryPosterChatID
}

// GetTag returns value of Tag field.
func (s *SearchPublicStoriesByTagRequest) GetTag() (value string) {
	if s == nil {
		return
	}
	return s.Tag
}

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

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

// SearchPublicStoriesByTag invokes method searchPublicStoriesByTag#69fbb14a returning error if any.
func (c *Client) SearchPublicStoriesByTag(ctx context.Context, request *SearchPublicStoriesByTagRequest) (*FoundStories, error) {
	var result FoundStories

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