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

// AicomposeTonesNotModified represents TL type `aicompose.tonesNotModified#c1f46103`.
//
// See https://core.telegram.org/constructor/aicompose.tonesNotModified for reference.
type AicomposeTonesNotModified struct {
}

// AicomposeTonesNotModifiedTypeID is TL type id of AicomposeTonesNotModified.
const AicomposeTonesNotModifiedTypeID = 0xc1f46103

// construct implements constructor of AicomposeTonesClass.
func (t AicomposeTonesNotModified) construct() AicomposeTonesClass { return &t }

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

	_ AicomposeTonesClass = &AicomposeTonesNotModified{}
)

func (t *AicomposeTonesNotModified) Zero() bool {
	if t == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*AicomposeTonesNotModified) TypeName() string {
	return "aicompose.tonesNotModified"
}

// TypeInfo returns info about TL type.
func (t *AicomposeTonesNotModified) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "aicompose.tonesNotModified",
		ID:   AicomposeTonesNotModifiedTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (t *AicomposeTonesNotModified) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode aicompose.tonesNotModified#c1f46103 as nil")
	}
	b.PutID(AicomposeTonesNotModifiedTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *AicomposeTonesNotModified) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode aicompose.tonesNotModified#c1f46103 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *AicomposeTonesNotModified) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode aicompose.tonesNotModified#c1f46103 to nil")
	}
	if err := b.ConsumeID(AicomposeTonesNotModifiedTypeID); err != nil {
		return fmt.Errorf("unable to decode aicompose.tonesNotModified#c1f46103: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *AicomposeTonesNotModified) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode aicompose.tonesNotModified#c1f46103 to nil")
	}
	return nil
}

// AicomposeTones represents TL type `aicompose.tones#6c9d0efe`.
//
// See https://core.telegram.org/constructor/aicompose.tones for reference.
type AicomposeTones struct {
	// Hash field of AicomposeTones.
	Hash int64
	// Tones field of AicomposeTones.
	Tones []AiComposeToneClass
	// Users field of AicomposeTones.
	Users []UserClass
}

// AicomposeTonesTypeID is TL type id of AicomposeTones.
const AicomposeTonesTypeID = 0x6c9d0efe

// construct implements constructor of AicomposeTonesClass.
func (t AicomposeTones) construct() AicomposeTonesClass { return &t }

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

	_ AicomposeTonesClass = &AicomposeTones{}
)

func (t *AicomposeTones) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Hash == 0) {
		return false
	}
	if !(t.Tones == nil) {
		return false
	}
	if !(t.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AicomposeTones from given interface.
func (t *AicomposeTones) FillFrom(from interface {
	GetHash() (value int64)
	GetTones() (value []AiComposeToneClass)
	GetUsers() (value []UserClass)
}) {
	t.Hash = from.GetHash()
	t.Tones = from.GetTones()
	t.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*AicomposeTones) TypeName() string {
	return "aicompose.tones"
}

// TypeInfo returns info about TL type.
func (t *AicomposeTones) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "aicompose.tones",
		ID:   AicomposeTonesTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
		{
			Name:       "Tones",
			SchemaName: "tones",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *AicomposeTones) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode aicompose.tones#6c9d0efe as nil")
	}
	b.PutID(AicomposeTonesTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *AicomposeTones) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode aicompose.tones#6c9d0efe as nil")
	}
	b.PutLong(t.Hash)
	b.PutVectorHeader(len(t.Tones))
	for idx, v := range t.Tones {
		if v == nil {
			return fmt.Errorf("unable to encode aicompose.tones#6c9d0efe: field tones element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode aicompose.tones#6c9d0efe: field tones element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(t.Users))
	for idx, v := range t.Users {
		if v == nil {
			return fmt.Errorf("unable to encode aicompose.tones#6c9d0efe: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode aicompose.tones#6c9d0efe: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *AicomposeTones) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode aicompose.tones#6c9d0efe to nil")
	}
	if err := b.ConsumeID(AicomposeTonesTypeID); err != nil {
		return fmt.Errorf("unable to decode aicompose.tones#6c9d0efe: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *AicomposeTones) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode aicompose.tones#6c9d0efe to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.tones#6c9d0efe: field hash: %w", err)
		}
		t.Hash = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.tones#6c9d0efe: field tones: %w", err)
		}

		if headerLen > 0 {
			t.Tones = make([]AiComposeToneClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeAiComposeTone(b)
			if err != nil {
				return fmt.Errorf("unable to decode aicompose.tones#6c9d0efe: field tones: %w", err)
			}
			t.Tones = append(t.Tones, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode aicompose.tones#6c9d0efe: field users: %w", err)
		}

		if headerLen > 0 {
			t.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 aicompose.tones#6c9d0efe: field users: %w", err)
			}
			t.Users = append(t.Users, value)
		}
	}
	return nil
}

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

// GetTones returns value of Tones field.
func (t *AicomposeTones) GetTones() (value []AiComposeToneClass) {
	if t == nil {
		return
	}
	return t.Tones
}

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

// MapTones returns field Tones wrapped in AiComposeToneClassArray helper.
func (t *AicomposeTones) MapTones() (value AiComposeToneClassArray) {
	return AiComposeToneClassArray(t.Tones)
}

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

// AicomposeTonesClassName is schema name of AicomposeTonesClass.
const AicomposeTonesClassName = "aicompose.Tones"

// AicomposeTonesClass represents aicompose.Tones generic type.
//
// See https://core.telegram.org/type/aicompose.Tones for reference.
//
// Example:
//
//	g, err := tg.DecodeAicomposeTones(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.AicomposeTonesNotModified: // aicompose.tonesNotModified#c1f46103
//	case *tg.AicomposeTones: // aicompose.tones#6c9d0efe
//	default: panic(v)
//	}
type AicomposeTonesClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() AicomposeTonesClass

	// 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 AicomposeTonesClass to AicomposeTones.
	AsModified() (*AicomposeTones, bool)
}

// AsModified tries to map AicomposeTonesNotModified to AicomposeTones.
func (t *AicomposeTonesNotModified) AsModified() (*AicomposeTones, bool) {
	return nil, false
}

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

// DecodeAicomposeTones implements binary de-serialization for AicomposeTonesClass.
func DecodeAicomposeTones(buf *bin.Buffer) (AicomposeTonesClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case AicomposeTonesNotModifiedTypeID:
		// Decoding aicompose.tonesNotModified#c1f46103.
		v := AicomposeTonesNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AicomposeTonesClass: %w", err)
		}
		return &v, nil
	case AicomposeTonesTypeID:
		// Decoding aicompose.tones#6c9d0efe.
		v := AicomposeTones{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AicomposeTonesClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode AicomposeTonesClass: %w", bin.NewUnexpectedID(id))
	}
}

// AicomposeTones boxes the AicomposeTonesClass providing a helper.
type AicomposeTonesBox struct {
	Tones AicomposeTonesClass
}

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

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