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

// QuickReplyMessages represents TL type `quickReplyMessages#8426549b`.
type QuickReplyMessages struct {
	// List of quick reply messages; messages may be null
	Messages []QuickReplyMessage
}

// QuickReplyMessagesTypeID is TL type id of QuickReplyMessages.
const QuickReplyMessagesTypeID = 0x8426549b

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

func (q *QuickReplyMessages) Zero() bool {
	if q == nil {
		return true
	}
	if !(q.Messages == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (q *QuickReplyMessages) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "quickReplyMessages",
		ID:   QuickReplyMessagesTypeID,
	}
	if q == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (q *QuickReplyMessages) Encode(b *bin.Buffer) error {
	if q == nil {
		return fmt.Errorf("can't encode quickReplyMessages#8426549b as nil")
	}
	b.PutID(QuickReplyMessagesTypeID)
	return q.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (q *QuickReplyMessages) EncodeBare(b *bin.Buffer) error {
	if q == nil {
		return fmt.Errorf("can't encode quickReplyMessages#8426549b as nil")
	}
	b.PutInt(len(q.Messages))
	for idx, v := range q.Messages {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare quickReplyMessages#8426549b: field messages element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (q *QuickReplyMessages) Decode(b *bin.Buffer) error {
	if q == nil {
		return fmt.Errorf("can't decode quickReplyMessages#8426549b to nil")
	}
	if err := b.ConsumeID(QuickReplyMessagesTypeID); err != nil {
		return fmt.Errorf("unable to decode quickReplyMessages#8426549b: %w", err)
	}
	return q.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (q *QuickReplyMessages) DecodeBare(b *bin.Buffer) error {
	if q == nil {
		return fmt.Errorf("can't decode quickReplyMessages#8426549b to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode quickReplyMessages#8426549b: field messages: %w", err)
		}

		if headerLen > 0 {
			q.Messages = make([]QuickReplyMessage, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value QuickReplyMessage
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare quickReplyMessages#8426549b: field messages: %w", err)
			}
			q.Messages = append(q.Messages, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (q *QuickReplyMessages) EncodeTDLibJSON(b tdjson.Encoder) error {
	if q == nil {
		return fmt.Errorf("can't encode quickReplyMessages#8426549b as nil")
	}
	b.ObjStart()
	b.PutID("quickReplyMessages")
	b.Comma()
	b.FieldStart("messages")
	b.ArrStart()
	for idx, v := range q.Messages {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode quickReplyMessages#8426549b: field messages element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (q *QuickReplyMessages) DecodeTDLibJSON(b tdjson.Decoder) error {
	if q == nil {
		return fmt.Errorf("can't decode quickReplyMessages#8426549b to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("quickReplyMessages"); err != nil {
				return fmt.Errorf("unable to decode quickReplyMessages#8426549b: %w", err)
			}
		case "messages":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value QuickReplyMessage
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode quickReplyMessages#8426549b: field messages: %w", err)
				}
				q.Messages = append(q.Messages, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode quickReplyMessages#8426549b: field messages: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMessages returns value of Messages field.
func (q *QuickReplyMessages) GetMessages() (value []QuickReplyMessage) {
	if q == nil {
		return
	}
	return q.Messages
}
