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

// SearchChatsOnServerRequest represents TL type `searchChatsOnServer#baf42f74`.
type SearchChatsOnServerRequest struct {
	// Query to search for
	Query string
	// The maximum number of chats to be returned
	Limit int32
}

// SearchChatsOnServerRequestTypeID is TL type id of SearchChatsOnServerRequest.
const SearchChatsOnServerRequestTypeID = 0xbaf42f74

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

func (s *SearchChatsOnServerRequest) 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 *SearchChatsOnServerRequest) String() string {
	if s == nil {
		return "SearchChatsOnServerRequest(nil)"
	}
	type Alias SearchChatsOnServerRequest
	return fmt.Sprintf("SearchChatsOnServerRequest%+v", Alias(*s))
}

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

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

// TypeInfo returns info about TL type.
func (s *SearchChatsOnServerRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchChatsOnServer",
		ID:   SearchChatsOnServerRequestTypeID,
	}
	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 *SearchChatsOnServerRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatsOnServer#baf42f74 as nil")
	}
	b.PutID(SearchChatsOnServerRequestTypeID)
	return s.EncodeBare(b)
}

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

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

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

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

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

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

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

// SearchChatsOnServer invokes method searchChatsOnServer#baf42f74 returning error if any.
func (c *Client) SearchChatsOnServer(ctx context.Context, request *SearchChatsOnServerRequest) (*Chats, error) {
	var result Chats

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