// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// MessagesSponsoredMessages represents TL type `messages.sponsoredMessages#c9ee1d87`.
// A set of sponsored messages associated to a channel
//
// See https://core.telegram.org/constructor/messages.sponsoredMessages for reference.
type MessagesSponsoredMessages struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, specifies the minimum number of messages between shown sponsored messages;
	// otherwise, only one sponsored message must be shown after all ordinary messages.
	//
	// Use SetPostsBetween and GetPostsBetween helpers.
	PostsBetween int
	// Sponsored messages
	Messages []SponsoredMessage
	// Chats mentioned in the sponsored messages
	Chats []ChatClass
	// Users mentioned in the sponsored messages
	Users []UserClass
}

// MessagesSponsoredMessagesTypeID is TL type id of MessagesSponsoredMessages.
const MessagesSponsoredMessagesTypeID = 0xc9ee1d87

// construct implements constructor of MessagesSponsoredMessagesClass.
func (s MessagesSponsoredMessages) construct() MessagesSponsoredMessagesClass { return &s }

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

	_ MessagesSponsoredMessagesClass = &MessagesSponsoredMessages{}
)

func (s *MessagesSponsoredMessages) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.PostsBetween == 0) {
		return false
	}
	if !(s.Messages == nil) {
		return false
	}
	if !(s.Chats == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSponsoredMessages from given interface.
func (s *MessagesSponsoredMessages) FillFrom(from interface {
	GetPostsBetween() (value int, ok bool)
	GetMessages() (value []SponsoredMessage)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	if val, ok := from.GetPostsBetween(); ok {
		s.PostsBetween = val
	}

	s.Messages = from.GetMessages()
	s.Chats = from.GetChats()
	s.Users = from.GetUsers()
}

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

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

// TypeInfo returns info about TL type.
func (s *MessagesSponsoredMessages) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.sponsoredMessages",
		ID:   MessagesSponsoredMessagesTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PostsBetween",
			SchemaName: "posts_between",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *MessagesSponsoredMessages) SetFlags() {
	if !(s.PostsBetween == 0) {
		s.Flags.Set(0)
	}
}

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

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSponsoredMessages) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.sponsoredMessages#c9ee1d87 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field flags: %w", err)
	}
	if s.Flags.Has(0) {
		b.PutInt(s.PostsBetween)
	}
	b.PutVectorHeader(len(s.Messages))
	for idx, v := range s.Messages {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field messages element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Chats))
	for idx, v := range s.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Users))
	for idx, v := range s.Users {
		if v == nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSponsoredMessages) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.sponsoredMessages#c9ee1d87 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field flags: %w", err)
		}
	}
	if s.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field posts_between: %w", err)
		}
		s.PostsBetween = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: 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.Decode(b); err != nil {
				return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field messages: %w", err)
			}
			s.Messages = append(s.Messages, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field chats: %w", err)
		}

		if headerLen > 0 {
			s.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field chats: %w", err)
			}
			s.Chats = append(s.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field users: %w", err)
		}

		if headerLen > 0 {
			s.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	return nil
}

// SetPostsBetween sets value of PostsBetween conditional field.
func (s *MessagesSponsoredMessages) SetPostsBetween(value int) {
	s.Flags.Set(0)
	s.PostsBetween = value
}

// GetPostsBetween returns value of PostsBetween conditional field and
// boolean which is true if field was set.
func (s *MessagesSponsoredMessages) GetPostsBetween() (value int, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.PostsBetween, true
}

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

// GetChats returns value of Chats field.
func (s *MessagesSponsoredMessages) GetChats() (value []ChatClass) {
	if s == nil {
		return
	}
	return s.Chats
}

// GetUsers returns value of Users field.
func (s *MessagesSponsoredMessages) GetUsers() (value []UserClass) {
	if s == nil {
		return
	}
	return s.Users
}

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (s *MessagesSponsoredMessages) MapChats() (value ChatClassArray) {
	return ChatClassArray(s.Chats)
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (s *MessagesSponsoredMessages) MapUsers() (value UserClassArray) {
	return UserClassArray(s.Users)
}

// MessagesSponsoredMessagesEmpty represents TL type `messages.sponsoredMessagesEmpty#1839490f`.
// No sponsored messages are available.
//
// See https://core.telegram.org/constructor/messages.sponsoredMessagesEmpty for reference.
type MessagesSponsoredMessagesEmpty struct {
}

// MessagesSponsoredMessagesEmptyTypeID is TL type id of MessagesSponsoredMessagesEmpty.
const MessagesSponsoredMessagesEmptyTypeID = 0x1839490f

// construct implements constructor of MessagesSponsoredMessagesClass.
func (s MessagesSponsoredMessagesEmpty) construct() MessagesSponsoredMessagesClass { return &s }

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

	_ MessagesSponsoredMessagesClass = &MessagesSponsoredMessagesEmpty{}
)

func (s *MessagesSponsoredMessagesEmpty) Zero() bool {
	if s == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*MessagesSponsoredMessagesEmpty) TypeName() string {
	return "messages.sponsoredMessagesEmpty"
}

// TypeInfo returns info about TL type.
func (s *MessagesSponsoredMessagesEmpty) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.sponsoredMessagesEmpty",
		ID:   MessagesSponsoredMessagesEmptyTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (s *MessagesSponsoredMessagesEmpty) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.sponsoredMessagesEmpty#1839490f as nil")
	}
	b.PutID(MessagesSponsoredMessagesEmptyTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSponsoredMessagesEmpty) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.sponsoredMessagesEmpty#1839490f as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSponsoredMessagesEmpty) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.sponsoredMessagesEmpty#1839490f to nil")
	}
	if err := b.ConsumeID(MessagesSponsoredMessagesEmptyTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.sponsoredMessagesEmpty#1839490f: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSponsoredMessagesEmpty) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.sponsoredMessagesEmpty#1839490f to nil")
	}
	return nil
}

// MessagesSponsoredMessagesClassName is schema name of MessagesSponsoredMessagesClass.
const MessagesSponsoredMessagesClassName = "messages.SponsoredMessages"

// MessagesSponsoredMessagesClass represents messages.SponsoredMessages generic type.
//
// See https://core.telegram.org/type/messages.SponsoredMessages for reference.
//
// Example:
//
//	g, err := tg.DecodeMessagesSponsoredMessages(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.MessagesSponsoredMessages: // messages.sponsoredMessages#c9ee1d87
//	case *tg.MessagesSponsoredMessagesEmpty: // messages.sponsoredMessagesEmpty#1839490f
//	default: panic(v)
//	}
type MessagesSponsoredMessagesClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessagesSponsoredMessagesClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// AsNotEmpty tries to map MessagesSponsoredMessagesClass to MessagesSponsoredMessages.
	AsNotEmpty() (*MessagesSponsoredMessages, bool)
}

// AsNotEmpty tries to map MessagesSponsoredMessages to MessagesSponsoredMessages.
func (s *MessagesSponsoredMessages) AsNotEmpty() (*MessagesSponsoredMessages, bool) {
	return s, true
}

// AsNotEmpty tries to map MessagesSponsoredMessagesEmpty to MessagesSponsoredMessages.
func (s *MessagesSponsoredMessagesEmpty) AsNotEmpty() (*MessagesSponsoredMessages, bool) {
	return nil, false
}

// DecodeMessagesSponsoredMessages implements binary de-serialization for MessagesSponsoredMessagesClass.
func DecodeMessagesSponsoredMessages(buf *bin.Buffer) (MessagesSponsoredMessagesClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case MessagesSponsoredMessagesTypeID:
		// Decoding messages.sponsoredMessages#c9ee1d87.
		v := MessagesSponsoredMessages{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesSponsoredMessagesClass: %w", err)
		}
		return &v, nil
	case MessagesSponsoredMessagesEmptyTypeID:
		// Decoding messages.sponsoredMessagesEmpty#1839490f.
		v := MessagesSponsoredMessagesEmpty{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesSponsoredMessagesClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessagesSponsoredMessagesClass: %w", bin.NewUnexpectedID(id))
	}
}

// MessagesSponsoredMessages boxes the MessagesSponsoredMessagesClass providing a helper.
type MessagesSponsoredMessagesBox struct {
	SponsoredMessages MessagesSponsoredMessagesClass
}

// Decode implements bin.Decoder for MessagesSponsoredMessagesBox.
func (b *MessagesSponsoredMessagesBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode MessagesSponsoredMessagesBox to nil")
	}
	v, err := DecodeMessagesSponsoredMessages(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.SponsoredMessages = v
	return nil
}

// Encode implements bin.Encode for MessagesSponsoredMessagesBox.
func (b *MessagesSponsoredMessagesBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.SponsoredMessages == nil {
		return fmt.Errorf("unable to encode MessagesSponsoredMessagesClass as nil")
	}
	return b.SponsoredMessages.Encode(buf)
}
