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

// SearchChatMembersRequest represents TL type `searchChatMembers#e56d46c5`.
type SearchChatMembersRequest struct {
	// Chat identifier
	ChatID int64
	// Query to search for
	Query string
	// The maximum number of users to be returned; up to 200
	Limit int32
	// The type of users to search for; pass null to search among all chat members
	Filter ChatMembersFilterClass
}

// SearchChatMembersRequestTypeID is TL type id of SearchChatMembersRequest.
const SearchChatMembersRequestTypeID = 0xe56d46c5

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchChatMembersRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchChatMembers",
		ID:   SearchChatMembersRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "Filter",
			SchemaName: "filter",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchChatMembersRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatMembers#e56d46c5 as nil")
	}
	b.PutID(SearchChatMembersRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchChatMembersRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatMembers#e56d46c5 as nil")
	}
	b.PutInt53(s.ChatID)
	b.PutString(s.Query)
	b.PutInt32(s.Limit)
	if s.Filter == nil {
		return fmt.Errorf("unable to encode searchChatMembers#e56d46c5: field filter is nil")
	}
	if err := s.Filter.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchChatMembers#e56d46c5: field filter: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SearchChatMembersRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchChatMembers#e56d46c5 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMembers#e56d46c5: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMembers#e56d46c5: field query: %w", err)
		}
		s.Query = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMembers#e56d46c5: field limit: %w", err)
		}
		s.Limit = value
	}
	{
		value, err := DecodeChatMembersFilter(b)
		if err != nil {
			return fmt.Errorf("unable to decode searchChatMembers#e56d46c5: field filter: %w", err)
		}
		s.Filter = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchChatMembersRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatMembers#e56d46c5 as nil")
	}
	b.ObjStart()
	b.PutID("searchChatMembers")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("query")
	b.PutString(s.Query)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(s.Limit)
	b.Comma()
	b.FieldStart("filter")
	if s.Filter == nil {
		return fmt.Errorf("unable to encode searchChatMembers#e56d46c5: field filter is nil")
	}
	if err := s.Filter.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode searchChatMembers#e56d46c5: field filter: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

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

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

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

// SearchChatMembers invokes method searchChatMembers#e56d46c5 returning error if any.
func (c *Client) SearchChatMembers(ctx context.Context, request *SearchChatMembersRequest) (*ChatMembers, error) {
	var result ChatMembers

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