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

// PageListOrderedItemText represents TL type `pageListOrderedItemText#5e068047`.
// Ordered list of text items
//
// See https://core.telegram.org/constructor/pageListOrderedItemText for reference.
type PageListOrderedItemText struct {
	// Number of element within ordered list
	Num string
	// Text
	Text RichTextClass
}

// PageListOrderedItemTextTypeID is TL type id of PageListOrderedItemText.
const PageListOrderedItemTextTypeID = 0x5e068047

// construct implements constructor of PageListOrderedItemClass.
func (p PageListOrderedItemText) construct() PageListOrderedItemClass { return &p }

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

	_ PageListOrderedItemClass = &PageListOrderedItemText{}
)

func (p *PageListOrderedItemText) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Num == "") {
		return false
	}
	if !(p.Text == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PageListOrderedItemText from given interface.
func (p *PageListOrderedItemText) FillFrom(from interface {
	GetNum() (value string)
	GetText() (value RichTextClass)
}) {
	p.Num = from.GetNum()
	p.Text = from.GetText()
}

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

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

// TypeInfo returns info about TL type.
func (p *PageListOrderedItemText) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "pageListOrderedItemText",
		ID:   PageListOrderedItemTextTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Num",
			SchemaName: "num",
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PageListOrderedItemText) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pageListOrderedItemText#5e068047 as nil")
	}
	b.PutID(PageListOrderedItemTextTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PageListOrderedItemText) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pageListOrderedItemText#5e068047 as nil")
	}
	b.PutString(p.Num)
	if p.Text == nil {
		return fmt.Errorf("unable to encode pageListOrderedItemText#5e068047: field text is nil")
	}
	if err := p.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode pageListOrderedItemText#5e068047: field text: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PageListOrderedItemText) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pageListOrderedItemText#5e068047 to nil")
	}
	if err := b.ConsumeID(PageListOrderedItemTextTypeID); err != nil {
		return fmt.Errorf("unable to decode pageListOrderedItemText#5e068047: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PageListOrderedItemText) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pageListOrderedItemText#5e068047 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode pageListOrderedItemText#5e068047: field num: %w", err)
		}
		p.Num = value
	}
	{
		value, err := DecodeRichText(b)
		if err != nil {
			return fmt.Errorf("unable to decode pageListOrderedItemText#5e068047: field text: %w", err)
		}
		p.Text = value
	}
	return nil
}

// GetNum returns value of Num field.
func (p *PageListOrderedItemText) GetNum() (value string) {
	if p == nil {
		return
	}
	return p.Num
}

// GetText returns value of Text field.
func (p *PageListOrderedItemText) GetText() (value RichTextClass) {
	if p == nil {
		return
	}
	return p.Text
}

// PageListOrderedItemBlocks represents TL type `pageListOrderedItemBlocks#98dd8936`.
// Ordered list of IV¹ blocks
//
// Links:
//  1. https://instantview.telegram.org
//
// See https://core.telegram.org/constructor/pageListOrderedItemBlocks for reference.
type PageListOrderedItemBlocks struct {
	// Number of element within ordered list
	Num string
	// Item contents
	Blocks []PageBlockClass
}

// PageListOrderedItemBlocksTypeID is TL type id of PageListOrderedItemBlocks.
const PageListOrderedItemBlocksTypeID = 0x98dd8936

// construct implements constructor of PageListOrderedItemClass.
func (p PageListOrderedItemBlocks) construct() PageListOrderedItemClass { return &p }

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

	_ PageListOrderedItemClass = &PageListOrderedItemBlocks{}
)

func (p *PageListOrderedItemBlocks) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Num == "") {
		return false
	}
	if !(p.Blocks == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PageListOrderedItemBlocks from given interface.
func (p *PageListOrderedItemBlocks) FillFrom(from interface {
	GetNum() (value string)
	GetBlocks() (value []PageBlockClass)
}) {
	p.Num = from.GetNum()
	p.Blocks = from.GetBlocks()
}

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

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

// TypeInfo returns info about TL type.
func (p *PageListOrderedItemBlocks) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "pageListOrderedItemBlocks",
		ID:   PageListOrderedItemBlocksTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Num",
			SchemaName: "num",
		},
		{
			Name:       "Blocks",
			SchemaName: "blocks",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PageListOrderedItemBlocks) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pageListOrderedItemBlocks#98dd8936 as nil")
	}
	b.PutID(PageListOrderedItemBlocksTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PageListOrderedItemBlocks) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pageListOrderedItemBlocks#98dd8936 as nil")
	}
	b.PutString(p.Num)
	b.PutVectorHeader(len(p.Blocks))
	for idx, v := range p.Blocks {
		if v == nil {
			return fmt.Errorf("unable to encode pageListOrderedItemBlocks#98dd8936: field blocks element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode pageListOrderedItemBlocks#98dd8936: field blocks element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PageListOrderedItemBlocks) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pageListOrderedItemBlocks#98dd8936 to nil")
	}
	if err := b.ConsumeID(PageListOrderedItemBlocksTypeID); err != nil {
		return fmt.Errorf("unable to decode pageListOrderedItemBlocks#98dd8936: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PageListOrderedItemBlocks) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pageListOrderedItemBlocks#98dd8936 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode pageListOrderedItemBlocks#98dd8936: field num: %w", err)
		}
		p.Num = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode pageListOrderedItemBlocks#98dd8936: field blocks: %w", err)
		}

		if headerLen > 0 {
			p.Blocks = make([]PageBlockClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodePageBlock(b)
			if err != nil {
				return fmt.Errorf("unable to decode pageListOrderedItemBlocks#98dd8936: field blocks: %w", err)
			}
			p.Blocks = append(p.Blocks, value)
		}
	}
	return nil
}

// GetNum returns value of Num field.
func (p *PageListOrderedItemBlocks) GetNum() (value string) {
	if p == nil {
		return
	}
	return p.Num
}

// GetBlocks returns value of Blocks field.
func (p *PageListOrderedItemBlocks) GetBlocks() (value []PageBlockClass) {
	if p == nil {
		return
	}
	return p.Blocks
}

// MapBlocks returns field Blocks wrapped in PageBlockClassArray helper.
func (p *PageListOrderedItemBlocks) MapBlocks() (value PageBlockClassArray) {
	return PageBlockClassArray(p.Blocks)
}

// PageListOrderedItemClassName is schema name of PageListOrderedItemClass.
const PageListOrderedItemClassName = "PageListOrderedItem"

// PageListOrderedItemClass represents PageListOrderedItem generic type.
//
// See https://core.telegram.org/type/PageListOrderedItem for reference.
//
// Example:
//
//	g, err := tg.DecodePageListOrderedItem(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.PageListOrderedItemText: // pageListOrderedItemText#5e068047
//	case *tg.PageListOrderedItemBlocks: // pageListOrderedItemBlocks#98dd8936
//	default: panic(v)
//	}
type PageListOrderedItemClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() PageListOrderedItemClass

	// 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

	// Number of element within ordered list
	GetNum() (value string)
}

// DecodePageListOrderedItem implements binary de-serialization for PageListOrderedItemClass.
func DecodePageListOrderedItem(buf *bin.Buffer) (PageListOrderedItemClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case PageListOrderedItemTextTypeID:
		// Decoding pageListOrderedItemText#5e068047.
		v := PageListOrderedItemText{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PageListOrderedItemClass: %w", err)
		}
		return &v, nil
	case PageListOrderedItemBlocksTypeID:
		// Decoding pageListOrderedItemBlocks#98dd8936.
		v := PageListOrderedItemBlocks{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PageListOrderedItemClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode PageListOrderedItemClass: %w", bin.NewUnexpectedID(id))
	}
}

// PageListOrderedItem boxes the PageListOrderedItemClass providing a helper.
type PageListOrderedItemBox struct {
	PageListOrderedItem PageListOrderedItemClass
}

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

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