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

// MessagesFoundStickersNotModified represents TL type `messages.foundStickersNotModified#6010c534`.
// No new stickers were found for the specified query
//
// See https://core.telegram.org/constructor/messages.foundStickersNotModified for reference.
type MessagesFoundStickersNotModified struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Offset for pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset int
}

// MessagesFoundStickersNotModifiedTypeID is TL type id of MessagesFoundStickersNotModified.
const MessagesFoundStickersNotModifiedTypeID = 0x6010c534

// construct implements constructor of MessagesFoundStickersClass.
func (f MessagesFoundStickersNotModified) construct() MessagesFoundStickersClass { return &f }

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

	_ MessagesFoundStickersClass = &MessagesFoundStickersNotModified{}
)

func (f *MessagesFoundStickersNotModified) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Flags.Zero()) {
		return false
	}
	if !(f.NextOffset == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesFoundStickersNotModified from given interface.
func (f *MessagesFoundStickersNotModified) FillFrom(from interface {
	GetNextOffset() (value int, ok bool)
}) {
	if val, ok := from.GetNextOffset(); ok {
		f.NextOffset = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (f *MessagesFoundStickersNotModified) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.foundStickersNotModified",
		ID:   MessagesFoundStickersNotModifiedTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !f.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (f *MessagesFoundStickersNotModified) SetFlags() {
	if !(f.NextOffset == 0) {
		f.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (f *MessagesFoundStickersNotModified) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode messages.foundStickersNotModified#6010c534 as nil")
	}
	b.PutID(MessagesFoundStickersNotModifiedTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *MessagesFoundStickersNotModified) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode messages.foundStickersNotModified#6010c534 as nil")
	}
	f.SetFlags()
	if err := f.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.foundStickersNotModified#6010c534: field flags: %w", err)
	}
	if f.Flags.Has(0) {
		b.PutInt(f.NextOffset)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (f *MessagesFoundStickersNotModified) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode messages.foundStickersNotModified#6010c534 to nil")
	}
	{
		if err := f.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.foundStickersNotModified#6010c534: field flags: %w", err)
		}
	}
	if f.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.foundStickersNotModified#6010c534: field next_offset: %w", err)
		}
		f.NextOffset = value
	}
	return nil
}

// SetNextOffset sets value of NextOffset conditional field.
func (f *MessagesFoundStickersNotModified) SetNextOffset(value int) {
	f.Flags.Set(0)
	f.NextOffset = value
}

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

// MessagesFoundStickers represents TL type `messages.foundStickers#82c9e290`.
// Found stickers
//
// See https://core.telegram.org/constructor/messages.foundStickers for reference.
type MessagesFoundStickers struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Offset for pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset int
	// Hash used for caching, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int64
	// Found stickers
	Stickers []DocumentClass
}

// MessagesFoundStickersTypeID is TL type id of MessagesFoundStickers.
const MessagesFoundStickersTypeID = 0x82c9e290

// construct implements constructor of MessagesFoundStickersClass.
func (f MessagesFoundStickers) construct() MessagesFoundStickersClass { return &f }

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

	_ MessagesFoundStickersClass = &MessagesFoundStickers{}
)

func (f *MessagesFoundStickers) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Flags.Zero()) {
		return false
	}
	if !(f.NextOffset == 0) {
		return false
	}
	if !(f.Hash == 0) {
		return false
	}
	if !(f.Stickers == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesFoundStickers from given interface.
func (f *MessagesFoundStickers) FillFrom(from interface {
	GetNextOffset() (value int, ok bool)
	GetHash() (value int64)
	GetStickers() (value []DocumentClass)
}) {
	if val, ok := from.GetNextOffset(); ok {
		f.NextOffset = val
	}

	f.Hash = from.GetHash()
	f.Stickers = from.GetStickers()
}

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

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

// TypeInfo returns info about TL type.
func (f *MessagesFoundStickers) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.foundStickers",
		ID:   MessagesFoundStickersTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !f.Flags.Has(0),
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
		{
			Name:       "Stickers",
			SchemaName: "stickers",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (f *MessagesFoundStickers) SetFlags() {
	if !(f.NextOffset == 0) {
		f.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (f *MessagesFoundStickers) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode messages.foundStickers#82c9e290 as nil")
	}
	b.PutID(MessagesFoundStickersTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *MessagesFoundStickers) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode messages.foundStickers#82c9e290 as nil")
	}
	f.SetFlags()
	if err := f.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.foundStickers#82c9e290: field flags: %w", err)
	}
	if f.Flags.Has(0) {
		b.PutInt(f.NextOffset)
	}
	b.PutLong(f.Hash)
	b.PutVectorHeader(len(f.Stickers))
	for idx, v := range f.Stickers {
		if v == nil {
			return fmt.Errorf("unable to encode messages.foundStickers#82c9e290: field stickers element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.foundStickers#82c9e290: field stickers element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (f *MessagesFoundStickers) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode messages.foundStickers#82c9e290 to nil")
	}
	{
		if err := f.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.foundStickers#82c9e290: field flags: %w", err)
		}
	}
	if f.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.foundStickers#82c9e290: field next_offset: %w", err)
		}
		f.NextOffset = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.foundStickers#82c9e290: field hash: %w", err)
		}
		f.Hash = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.foundStickers#82c9e290: field stickers: %w", err)
		}

		if headerLen > 0 {
			f.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.foundStickers#82c9e290: field stickers: %w", err)
			}
			f.Stickers = append(f.Stickers, value)
		}
	}
	return nil
}

// SetNextOffset sets value of NextOffset conditional field.
func (f *MessagesFoundStickers) SetNextOffset(value int) {
	f.Flags.Set(0)
	f.NextOffset = value
}

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

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

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

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

// MessagesFoundStickersClassName is schema name of MessagesFoundStickersClass.
const MessagesFoundStickersClassName = "messages.FoundStickers"

// MessagesFoundStickersClass represents messages.FoundStickers generic type.
//
// See https://core.telegram.org/type/messages.FoundStickers for reference.
//
// Example:
//
//	g, err := tg.DecodeMessagesFoundStickers(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.MessagesFoundStickersNotModified: // messages.foundStickersNotModified#6010c534
//	case *tg.MessagesFoundStickers: // messages.foundStickers#82c9e290
//	default: panic(v)
//	}
type MessagesFoundStickersClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessagesFoundStickersClass

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

	// Offset for pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	GetNextOffset() (value int, ok bool)

	// AsModified tries to map MessagesFoundStickersClass to MessagesFoundStickers.
	AsModified() (*MessagesFoundStickers, bool)
}

// AsModified tries to map MessagesFoundStickersNotModified to MessagesFoundStickers.
func (f *MessagesFoundStickersNotModified) AsModified() (*MessagesFoundStickers, bool) {
	return nil, false
}

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

// DecodeMessagesFoundStickers implements binary de-serialization for MessagesFoundStickersClass.
func DecodeMessagesFoundStickers(buf *bin.Buffer) (MessagesFoundStickersClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case MessagesFoundStickersNotModifiedTypeID:
		// Decoding messages.foundStickersNotModified#6010c534.
		v := MessagesFoundStickersNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesFoundStickersClass: %w", err)
		}
		return &v, nil
	case MessagesFoundStickersTypeID:
		// Decoding messages.foundStickers#82c9e290.
		v := MessagesFoundStickers{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesFoundStickersClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessagesFoundStickersClass: %w", bin.NewUnexpectedID(id))
	}
}

// MessagesFoundStickers boxes the MessagesFoundStickersClass providing a helper.
type MessagesFoundStickersBox struct {
	FoundStickers MessagesFoundStickersClass
}

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

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