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

// SponsoredMessages represents TL type `sponsoredMessages#e402a0f`.
type SponsoredMessages struct {
	// List of sponsored messages
	Messages []SponsoredMessage
	// The minimum number of messages between shown sponsored messages, or 0 if only one
	// sponsored message must be shown after all ordinary messages
	MessagesBetween int32
}

// SponsoredMessagesTypeID is TL type id of SponsoredMessages.
const SponsoredMessagesTypeID = 0xe402a0f

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

func (s *SponsoredMessages) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Messages == nil) {
		return false
	}
	if !(s.MessagesBetween == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SponsoredMessages) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sponsoredMessages",
		ID:   SponsoredMessagesTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
		{
			Name:       "MessagesBetween",
			SchemaName: "messages_between",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SponsoredMessages) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sponsoredMessages#e402a0f as nil")
	}
	b.PutID(SponsoredMessagesTypeID)
	return s.EncodeBare(b)
}

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

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

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

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

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

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

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

// GetMessages returns value of Messages field.
func (s *SponsoredMessages) GetMessages() (value []SponsoredMessage) {
	if s == nil {
		return
	}
	return s.Messages
}

// GetMessagesBetween returns value of MessagesBetween field.
func (s *SponsoredMessages) GetMessagesBetween() (value int32) {
	if s == nil {
		return
	}
	return s.MessagesBetween
}
