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

// FoundChatMessages represents TL type `foundChatMessages#4a25270c`.
type FoundChatMessages struct {
	// Approximate total number of messages found; -1 if unknown
	TotalCount int32
	// List of messages
	Messages []Message
	// The offset for the next request. If 0, there are no more results
	NextFromMessageID int64
}

// FoundChatMessagesTypeID is TL type id of FoundChatMessages.
const FoundChatMessagesTypeID = 0x4a25270c

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

func (f *FoundChatMessages) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.TotalCount == 0) {
		return false
	}
	if !(f.Messages == nil) {
		return false
	}
	if !(f.NextFromMessageID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FoundChatMessages) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "foundChatMessages",
		ID:   FoundChatMessagesTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
		{
			Name:       "NextFromMessageID",
			SchemaName: "next_from_message_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FoundChatMessages) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundChatMessages#4a25270c as nil")
	}
	b.PutID(FoundChatMessagesTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FoundChatMessages) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundChatMessages#4a25270c as nil")
	}
	b.PutInt32(f.TotalCount)
	b.PutInt(len(f.Messages))
	for idx, v := range f.Messages {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare foundChatMessages#4a25270c: field messages element with index %d: %w", idx, err)
		}
	}
	b.PutInt53(f.NextFromMessageID)
	return nil
}

// Decode implements bin.Decoder.
func (f *FoundChatMessages) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundChatMessages#4a25270c to nil")
	}
	if err := b.ConsumeID(FoundChatMessagesTypeID); err != nil {
		return fmt.Errorf("unable to decode foundChatMessages#4a25270c: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FoundChatMessages) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundChatMessages#4a25270c to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode foundChatMessages#4a25270c: field total_count: %w", err)
		}
		f.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode foundChatMessages#4a25270c: field messages: %w", err)
		}

		if headerLen > 0 {
			f.Messages = make([]Message, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Message
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare foundChatMessages#4a25270c: field messages: %w", err)
			}
			f.Messages = append(f.Messages, value)
		}
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode foundChatMessages#4a25270c: field next_from_message_id: %w", err)
		}
		f.NextFromMessageID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FoundChatMessages) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode foundChatMessages#4a25270c as nil")
	}
	b.ObjStart()
	b.PutID("foundChatMessages")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(f.TotalCount)
	b.Comma()
	b.FieldStart("messages")
	b.ArrStart()
	for idx, v := range f.Messages {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode foundChatMessages#4a25270c: field messages element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_from_message_id")
	b.PutInt53(f.NextFromMessageID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *FoundChatMessages) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode foundChatMessages#4a25270c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("foundChatMessages"); err != nil {
				return fmt.Errorf("unable to decode foundChatMessages#4a25270c: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode foundChatMessages#4a25270c: field total_count: %w", err)
			}
			f.TotalCount = value
		case "messages":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value Message
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode foundChatMessages#4a25270c: field messages: %w", err)
				}
				f.Messages = append(f.Messages, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode foundChatMessages#4a25270c: field messages: %w", err)
			}
		case "next_from_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode foundChatMessages#4a25270c: field next_from_message_id: %w", err)
			}
			f.NextFromMessageID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (f *FoundChatMessages) GetTotalCount() (value int32) {
	if f == nil {
		return
	}
	return f.TotalCount
}

// GetMessages returns value of Messages field.
func (f *FoundChatMessages) GetMessages() (value []Message) {
	if f == nil {
		return
	}
	return f.Messages
}

// GetNextFromMessageID returns value of NextFromMessageID field.
func (f *FoundChatMessages) GetNextFromMessageID() (value int64) {
	if f == nil {
		return
	}
	return f.NextFromMessageID
}
