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

// SearchChatMessagesRequest represents TL type `searchChatMessages#3fec2b64`.
type SearchChatMessagesRequest struct {
	// Identifier of the chat in which to search messages
	ChatID int64
	// Pass topic identifier to search messages only in specific topic; pass null to search
	// for messages in all topics
	TopicID MessageTopicClass
	// Query to search for
	Query string
	// Identifier of the sender of messages to search for; pass null to search for messages
	// from any sender. Not supported in secret chats
	SenderID MessageSenderClass
	// Identifier of the message starting from which history 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
	// Additional filter for messages to search; pass null to search for all messages
	Filter SearchMessagesFilterClass
}

// SearchChatMessagesRequestTypeID is TL type id of SearchChatMessagesRequest.
const SearchChatMessagesRequestTypeID = 0x3fec2b64

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

func (s *SearchChatMessagesRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.TopicID == nil) {
		return false
	}
	if !(s.Query == "") {
		return false
	}
	if !(s.SenderID == nil) {
		return false
	}
	if !(s.FromMessageID == 0) {
		return false
	}
	if !(s.Offset == 0) {
		return false
	}
	if !(s.Limit == 0) {
		return false
	}
	if !(s.Filter == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchChatMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchChatMessages",
		ID:   SearchChatMessagesRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "TopicID",
			SchemaName: "topic_id",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "SenderID",
			SchemaName: "sender_id",
		},
		{
			Name:       "FromMessageID",
			SchemaName: "from_message_id",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "Filter",
			SchemaName: "filter",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchChatMessagesRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatMessages#3fec2b64 as nil")
	}
	b.PutID(SearchChatMessagesRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchChatMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatMessages#3fec2b64 as nil")
	}
	b.PutInt53(s.ChatID)
	if s.TopicID == nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field topic_id is nil")
	}
	if err := s.TopicID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field topic_id: %w", err)
	}
	b.PutString(s.Query)
	if s.SenderID == nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field sender_id is nil")
	}
	if err := s.SenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field sender_id: %w", err)
	}
	b.PutInt53(s.FromMessageID)
	b.PutInt32(s.Offset)
	b.PutInt32(s.Limit)
	if s.Filter == nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field filter is nil")
	}
	if err := s.Filter.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field filter: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SearchChatMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchChatMessages#3fec2b64 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := DecodeMessageTopic(b)
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field topic_id: %w", err)
		}
		s.TopicID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field query: %w", err)
		}
		s.Query = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field sender_id: %w", err)
		}
		s.SenderID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field from_message_id: %w", err)
		}
		s.FromMessageID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field offset: %w", err)
		}
		s.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field limit: %w", err)
		}
		s.Limit = value
	}
	{
		value, err := DecodeSearchMessagesFilter(b)
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field filter: %w", err)
		}
		s.Filter = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchChatMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatMessages#3fec2b64 as nil")
	}
	b.ObjStart()
	b.PutID("searchChatMessages")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("topic_id")
	if s.TopicID == nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field topic_id is nil")
	}
	if err := s.TopicID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field topic_id: %w", err)
	}
	b.Comma()
	b.FieldStart("query")
	b.PutString(s.Query)
	b.Comma()
	b.FieldStart("sender_id")
	if s.SenderID == nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field sender_id is nil")
	}
	if err := s.SenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field sender_id: %w", err)
	}
	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.FieldStart("filter")
	if s.Filter == nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field filter is nil")
	}
	if err := s.Filter.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode searchChatMessages#3fec2b64: field filter: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("searchChatMessages"); err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field chat_id: %w", err)
			}
			s.ChatID = value
		case "topic_id":
			value, err := DecodeTDLibJSONMessageTopic(b)
			if err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field topic_id: %w", err)
			}
			s.TopicID = value
		case "query":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field query: %w", err)
			}
			s.Query = value
		case "sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field sender_id: %w", err)
			}
			s.SenderID = value
		case "from_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field from_message_id: %w", err)
			}
			s.FromMessageID = value
		case "offset":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field offset: %w", err)
			}
			s.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field limit: %w", err)
			}
			s.Limit = value
		case "filter":
			value, err := DecodeTDLibJSONSearchMessagesFilter(b)
			if err != nil {
				return fmt.Errorf("unable to decode searchChatMessages#3fec2b64: field filter: %w", err)
			}
			s.Filter = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetTopicID returns value of TopicID field.
func (s *SearchChatMessagesRequest) GetTopicID() (value MessageTopicClass) {
	if s == nil {
		return
	}
	return s.TopicID
}

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

// GetSenderID returns value of SenderID field.
func (s *SearchChatMessagesRequest) GetSenderID() (value MessageSenderClass) {
	if s == nil {
		return
	}
	return s.SenderID
}

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

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

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

// GetFilter returns value of Filter field.
func (s *SearchChatMessagesRequest) GetFilter() (value SearchMessagesFilterClass) {
	if s == nil {
		return
	}
	return s.Filter
}

// SearchChatMessages invokes method searchChatMessages#3fec2b64 returning error if any.
func (c *Client) SearchChatMessages(ctx context.Context, request *SearchChatMessagesRequest) (*FoundChatMessages, error) {
	var result FoundChatMessages

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