// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesStickersNotModified represents TL type `messages.stickersNotModified#f1749a22`.
// No new stickers were found for the given query
//
// See https://core.telegram.org/constructor/messages.stickersNotModified for reference.
type MessagesStickersNotModified struct {
}

// MessagesStickersNotModifiedTypeID is TL type id of MessagesStickersNotModified.
const MessagesStickersNotModifiedTypeID = 0xf1749a22

// construct implements constructor of MessagesStickersClass.
func (s MessagesStickersNotModified) construct() MessagesStickersClass { return &s }

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

	_ MessagesStickersClass = &MessagesStickersNotModified{}
)

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

	return true
}

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

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

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

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

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

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

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

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

// MessagesStickers represents TL type `messages.stickers#30a6ec7e`.
// Found stickers
//
// See https://core.telegram.org/constructor/messages.stickers for reference.
type MessagesStickers struct {
	// Hash used for caching, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int64
	// Stickers
	Stickers []DocumentClass
}

// MessagesStickersTypeID is TL type id of MessagesStickers.
const MessagesStickersTypeID = 0x30a6ec7e

// construct implements constructor of MessagesStickersClass.
func (s MessagesStickers) construct() MessagesStickersClass { return &s }

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

	_ MessagesStickersClass = &MessagesStickers{}
)

func (s *MessagesStickers) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Hash == 0) {
		return false
	}
	if !(s.Stickers == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesStickers from given interface.
func (s *MessagesStickers) FillFrom(from interface {
	GetHash() (value int64)
	GetStickers() (value []DocumentClass)
}) {
	s.Hash = from.GetHash()
	s.Stickers = from.GetStickers()
}

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

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

// TypeInfo returns info about TL type.
func (s *MessagesStickers) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.stickers",
		ID:   MessagesStickersTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
		{
			Name:       "Stickers",
			SchemaName: "stickers",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *MessagesStickers) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.stickers#30a6ec7e as nil")
	}
	b.PutID(MessagesStickersTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesStickers) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.stickers#30a6ec7e as nil")
	}
	b.PutLong(s.Hash)
	b.PutVectorHeader(len(s.Stickers))
	for idx, v := range s.Stickers {
		if v == nil {
			return fmt.Errorf("unable to encode messages.stickers#30a6ec7e: field stickers element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.stickers#30a6ec7e: field stickers element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *MessagesStickers) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.stickers#30a6ec7e to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.stickers#30a6ec7e: field hash: %w", err)
		}
		s.Hash = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.stickers#30a6ec7e: field stickers: %w", err)
		}

		if headerLen > 0 {
			s.Stickers = make([]DocumentClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeDocument(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.stickers#30a6ec7e: field stickers: %w", err)
			}
			s.Stickers = append(s.Stickers, value)
		}
	}
	return nil
}

// GetHash returns value of Hash field.
func (s *MessagesStickers) GetHash() (value int64) {
	if s == nil {
		return
	}
	return s.Hash
}

// GetStickers returns value of Stickers field.
func (s *MessagesStickers) GetStickers() (value []DocumentClass) {
	if s == nil {
		return
	}
	return s.Stickers
}

// MapStickers returns field Stickers wrapped in DocumentClassArray helper.
func (s *MessagesStickers) MapStickers() (value DocumentClassArray) {
	return DocumentClassArray(s.Stickers)
}

// MessagesStickersClassName is schema name of MessagesStickersClass.
const MessagesStickersClassName = "messages.Stickers"

// MessagesStickersClass represents messages.Stickers generic type.
//
// See https://core.telegram.org/type/messages.Stickers for reference.
//
// Example:
//
//	g, err := tg.DecodeMessagesStickers(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.MessagesStickersNotModified: // messages.stickersNotModified#f1749a22
//	case *tg.MessagesStickers: // messages.stickers#30a6ec7e
//	default: panic(v)
//	}
type MessagesStickersClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessagesStickersClass

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

	// AsModified tries to map MessagesStickersClass to MessagesStickers.
	AsModified() (*MessagesStickers, bool)
}

// AsModified tries to map MessagesStickersNotModified to MessagesStickers.
func (s *MessagesStickersNotModified) AsModified() (*MessagesStickers, bool) {
	return nil, false
}

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

// DecodeMessagesStickers implements binary de-serialization for MessagesStickersClass.
func DecodeMessagesStickers(buf *bin.Buffer) (MessagesStickersClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case MessagesStickersNotModifiedTypeID:
		// Decoding messages.stickersNotModified#f1749a22.
		v := MessagesStickersNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesStickersClass: %w", err)
		}
		return &v, nil
	case MessagesStickersTypeID:
		// Decoding messages.stickers#30a6ec7e.
		v := MessagesStickers{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesStickersClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessagesStickersClass: %w", bin.NewUnexpectedID(id))
	}
}

// MessagesStickers boxes the MessagesStickersClass providing a helper.
type MessagesStickersBox struct {
	Stickers MessagesStickersClass
}

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

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