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

// SearchPublicMessagesByTagRequest represents TL type `searchPublicMessagesByTag#25976caa`.
type SearchPublicMessagesByTagRequest struct {
	// 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 messages to be returned; up to 100. For optimal performance, the
	// number of returned messages is chosen by TDLib and can be smaller than the specified
	// limit
	Limit int32
}

// SearchPublicMessagesByTagRequestTypeID is TL type id of SearchPublicMessagesByTagRequest.
const SearchPublicMessagesByTagRequestTypeID = 0x25976caa

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (s *SearchPublicMessagesByTagRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicMessagesByTag#25976caa as nil")
	}
	b.PutID(SearchPublicMessagesByTagRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchPublicMessagesByTagRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicMessagesByTag#25976caa as nil")
	}
	b.PutString(s.Tag)
	b.PutString(s.Offset)
	b.PutInt32(s.Limit)
	return nil
}

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchPublicMessagesByTagRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicMessagesByTag#25976caa as nil")
	}
	b.ObjStart()
	b.PutID("searchPublicMessagesByTag")
	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 *SearchPublicMessagesByTagRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchPublicMessagesByTag#25976caa to nil")
	}

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

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

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

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

// SearchPublicMessagesByTag invokes method searchPublicMessagesByTag#25976caa returning error if any.
func (c *Client) SearchPublicMessagesByTag(ctx context.Context, request *SearchPublicMessagesByTagRequest) (*FoundMessages, error) {
	var result FoundMessages

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