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

// SearchChatsRequest represents TL type `searchChats#8ff4b9cc`.
type SearchChatsRequest struct {
	// Query to search for. If the query is empty, returns up to 50 recently found chats
	Query string
	// The maximum number of chats to be returned
	Limit int32
}

// SearchChatsRequestTypeID is TL type id of SearchChatsRequest.
const SearchChatsRequestTypeID = 0x8ff4b9cc

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

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

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

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

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

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

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

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

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

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

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

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

// SearchChats invokes method searchChats#8ff4b9cc returning error if any.
func (c *Client) SearchChats(ctx context.Context, request *SearchChatsRequest) (*Chats, error) {
	var result Chats

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