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

// SearchSavedMessagesRequest represents TL type `searchSavedMessages#8a9b9f96`.
type SearchSavedMessagesRequest struct {
	// If not 0, only messages in the specified Saved Messages topic will be considered; pass
	// 0 to consider all messages
	SavedMessagesTopicID int64
	// Tag to search for; pass null to return all suitable messages
	Tag ReactionTypeClass
	// Query to search for
	Query string
	// Identifier of the message starting from which messages must be fetched; use 0 to get
	// results from the last message
	FromMessageID int64
	// Specify 0 to get results from exactly the message from_message_id or a negative offset
	// to get the specified message and some newer messages
	Offset int32
	// The maximum number of messages to be returned; must be positive and can't be greater
	// than 100. If the offset is negative, the limit must be greater than -offset.
	Limit int32
}

// SearchSavedMessagesRequestTypeID is TL type id of SearchSavedMessagesRequest.
const SearchSavedMessagesRequestTypeID = 0x8a9b9f96

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchSavedMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchSavedMessages",
		ID:   SearchSavedMessagesRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SavedMessagesTopicID",
			SchemaName: "saved_messages_topic_id",
		},
		{
			Name:       "Tag",
			SchemaName: "tag",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "FromMessageID",
			SchemaName: "from_message_id",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchSavedMessagesRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchSavedMessages#8a9b9f96 as nil")
	}
	b.PutID(SearchSavedMessagesRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchSavedMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchSavedMessages#8a9b9f96 as nil")
	}
	b.PutInt53(s.SavedMessagesTopicID)
	if s.Tag == nil {
		return fmt.Errorf("unable to encode searchSavedMessages#8a9b9f96: field tag is nil")
	}
	if err := s.Tag.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchSavedMessages#8a9b9f96: field tag: %w", err)
	}
	b.PutString(s.Query)
	b.PutInt53(s.FromMessageID)
	b.PutInt32(s.Offset)
	b.PutInt32(s.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SearchSavedMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchSavedMessages#8a9b9f96 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field saved_messages_topic_id: %w", err)
		}
		s.SavedMessagesTopicID = value
	}
	{
		value, err := DecodeReactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field tag: %w", err)
		}
		s.Tag = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field query: %w", err)
		}
		s.Query = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field from_message_id: %w", err)
		}
		s.FromMessageID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field offset: %w", err)
		}
		s.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchSavedMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchSavedMessages#8a9b9f96 as nil")
	}
	b.ObjStart()
	b.PutID("searchSavedMessages")
	b.Comma()
	b.FieldStart("saved_messages_topic_id")
	b.PutInt53(s.SavedMessagesTopicID)
	b.Comma()
	b.FieldStart("tag")
	if s.Tag == nil {
		return fmt.Errorf("unable to encode searchSavedMessages#8a9b9f96: field tag is nil")
	}
	if err := s.Tag.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode searchSavedMessages#8a9b9f96: field tag: %w", err)
	}
	b.Comma()
	b.FieldStart("query")
	b.PutString(s.Query)
	b.Comma()
	b.FieldStart("from_message_id")
	b.PutInt53(s.FromMessageID)
	b.Comma()
	b.FieldStart("offset")
	b.PutInt32(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 *SearchSavedMessagesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchSavedMessages#8a9b9f96 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("searchSavedMessages"); err != nil {
				return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: %w", err)
			}
		case "saved_messages_topic_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field saved_messages_topic_id: %w", err)
			}
			s.SavedMessagesTopicID = value
		case "tag":
			value, err := DecodeTDLibJSONReactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field tag: %w", err)
			}
			s.Tag = value
		case "query":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field query: %w", err)
			}
			s.Query = value
		case "from_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field from_message_id: %w", err)
			}
			s.FromMessageID = value
		case "offset":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field offset: %w", err)
			}
			s.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode searchSavedMessages#8a9b9f96: field limit: %w", err)
			}
			s.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSavedMessagesTopicID returns value of SavedMessagesTopicID field.
func (s *SearchSavedMessagesRequest) GetSavedMessagesTopicID() (value int64) {
	if s == nil {
		return
	}
	return s.SavedMessagesTopicID
}

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

// GetQuery returns value of Query field.
func (s *SearchSavedMessagesRequest) GetQuery() (value string) {
	if s == nil {
		return
	}
	return s.Query
}

// GetFromMessageID returns value of FromMessageID field.
func (s *SearchSavedMessagesRequest) GetFromMessageID() (value int64) {
	if s == nil {
		return
	}
	return s.FromMessageID
}

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

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

// SearchSavedMessages invokes method searchSavedMessages#8a9b9f96 returning error if any.
func (c *Client) SearchSavedMessages(ctx context.Context, request *SearchSavedMessagesRequest) (*FoundChatMessages, error) {
	var result FoundChatMessages

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