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

// SearchCallMessagesRequest represents TL type `searchCallMessages#8c3bef1b`.
type SearchCallMessagesRequest struct {
	// Offset of the first entry to return as received from the previous request; use empty
	// string to get the first chunk of results
	Offset string
	// The maximum number of messages to be returned; up to 100. For optimal performance, the
	// number of returned messages is chosen by TDLib and can be smaller than the specified
	// limit
	Limit int32
	// Pass true to search only for messages with missed/declined calls
	OnlyMissed bool
}

// SearchCallMessagesRequestTypeID is TL type id of SearchCallMessagesRequest.
const SearchCallMessagesRequestTypeID = 0x8c3bef1b

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchCallMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchCallMessages",
		ID:   SearchCallMessagesRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "OnlyMissed",
			SchemaName: "only_missed",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchCallMessagesRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchCallMessages#8c3bef1b as nil")
	}
	b.PutID(SearchCallMessagesRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchCallMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchCallMessages#8c3bef1b as nil")
	}
	b.PutString(s.Offset)
	b.PutInt32(s.Limit)
	b.PutBool(s.OnlyMissed)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SearchCallMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchCallMessages#8c3bef1b to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchCallMessages#8c3bef1b: field offset: %w", err)
		}
		s.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchCallMessages#8c3bef1b: field limit: %w", err)
		}
		s.Limit = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode searchCallMessages#8c3bef1b: field only_missed: %w", err)
		}
		s.OnlyMissed = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchCallMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchCallMessages#8c3bef1b as nil")
	}
	b.ObjStart()
	b.PutID("searchCallMessages")
	b.Comma()
	b.FieldStart("offset")
	b.PutString(s.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(s.Limit)
	b.Comma()
	b.FieldStart("only_missed")
	b.PutBool(s.OnlyMissed)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

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

// GetOnlyMissed returns value of OnlyMissed field.
func (s *SearchCallMessagesRequest) GetOnlyMissed() (value bool) {
	if s == nil {
		return
	}
	return s.OnlyMissed
}

// SearchCallMessages invokes method searchCallMessages#8c3bef1b returning error if any.
func (c *Client) SearchCallMessages(ctx context.Context, request *SearchCallMessagesRequest) (*FoundMessages, error) {
	var result FoundMessages

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