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

// SearchOutgoingDocumentMessagesRequest represents TL type `searchOutgoingDocumentMessages#c023c47e`.
type SearchOutgoingDocumentMessagesRequest struct {
	// Query to search for in document file name and message caption
	Query string
	// The maximum number of messages to be returned; up to 100
	Limit int32
}

// SearchOutgoingDocumentMessagesRequestTypeID is TL type id of SearchOutgoingDocumentMessagesRequest.
const SearchOutgoingDocumentMessagesRequestTypeID = 0xc023c47e

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchOutgoingDocumentMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchOutgoingDocumentMessages",
		ID:   SearchOutgoingDocumentMessagesRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchOutgoingDocumentMessagesRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchOutgoingDocumentMessages#c023c47e as nil")
	}
	b.PutID(SearchOutgoingDocumentMessagesRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchOutgoingDocumentMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchOutgoingDocumentMessages#c023c47e as nil")
	}
	b.PutString(s.Query)
	b.PutInt32(s.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SearchOutgoingDocumentMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchOutgoingDocumentMessages#c023c47e to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchOutgoingDocumentMessages#c023c47e: field query: %w", err)
		}
		s.Query = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchOutgoingDocumentMessages#c023c47e: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchOutgoingDocumentMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchOutgoingDocumentMessages#c023c47e as nil")
	}
	b.ObjStart()
	b.PutID("searchOutgoingDocumentMessages")
	b.Comma()
	b.FieldStart("query")
	b.PutString(s.Query)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(s.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

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

// SearchOutgoingDocumentMessages invokes method searchOutgoingDocumentMessages#c023c47e returning error if any.
func (c *Client) SearchOutgoingDocumentMessages(ctx context.Context, request *SearchOutgoingDocumentMessagesRequest) (*FoundMessages, error) {
	var result FoundMessages

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