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

// ChatBoostSlots represents TL type `chatBoostSlots#6859c79`.
type ChatBoostSlots struct {
	// List of boost slots
	Slots []ChatBoostSlot
}

// ChatBoostSlotsTypeID is TL type id of ChatBoostSlots.
const ChatBoostSlotsTypeID = 0x6859c79

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

func (c *ChatBoostSlots) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Slots == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatBoostSlots) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatBoostSlots",
		ID:   ChatBoostSlotsTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Slots",
			SchemaName: "slots",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatBoostSlots) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatBoostSlots#6859c79 as nil")
	}
	b.PutID(ChatBoostSlotsTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatBoostSlots) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatBoostSlots#6859c79 as nil")
	}
	b.PutInt(len(c.Slots))
	for idx, v := range c.Slots {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare chatBoostSlots#6859c79: field slots element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatBoostSlots) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatBoostSlots#6859c79 to nil")
	}
	if err := b.ConsumeID(ChatBoostSlotsTypeID); err != nil {
		return fmt.Errorf("unable to decode chatBoostSlots#6859c79: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatBoostSlots) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatBoostSlots#6859c79 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatBoostSlots#6859c79: field slots: %w", err)
		}

		if headerLen > 0 {
			c.Slots = make([]ChatBoostSlot, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ChatBoostSlot
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare chatBoostSlots#6859c79: field slots: %w", err)
			}
			c.Slots = append(c.Slots, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatBoostSlots) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatBoostSlots#6859c79 as nil")
	}
	b.ObjStart()
	b.PutID("chatBoostSlots")
	b.Comma()
	b.FieldStart("slots")
	b.ArrStart()
	for idx, v := range c.Slots {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode chatBoostSlots#6859c79: field slots 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 (c *ChatBoostSlots) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatBoostSlots#6859c79 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatBoostSlots"); err != nil {
				return fmt.Errorf("unable to decode chatBoostSlots#6859c79: %w", err)
			}
		case "slots":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value ChatBoostSlot
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode chatBoostSlots#6859c79: field slots: %w", err)
				}
				c.Slots = append(c.Slots, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatBoostSlots#6859c79: field slots: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSlots returns value of Slots field.
func (c *ChatBoostSlots) GetSlots() (value []ChatBoostSlot) {
	if c == nil {
		return
	}
	return c.Slots
}
