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

// AffiliateTypeCurrentUser represents TL type `affiliateTypeCurrentUser#56a701f5`.
type AffiliateTypeCurrentUser struct {
}

// AffiliateTypeCurrentUserTypeID is TL type id of AffiliateTypeCurrentUser.
const AffiliateTypeCurrentUserTypeID = 0x56a701f5

// construct implements constructor of AffiliateTypeClass.
func (a AffiliateTypeCurrentUser) construct() AffiliateTypeClass { return &a }

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

	_ AffiliateTypeClass = &AffiliateTypeCurrentUser{}
)

func (a *AffiliateTypeCurrentUser) Zero() bool {
	if a == nil {
		return true
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AffiliateTypeCurrentUser) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "affiliateTypeCurrentUser",
		ID:   AffiliateTypeCurrentUserTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (a *AffiliateTypeCurrentUser) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeCurrentUser#56a701f5 as nil")
	}
	b.PutID(AffiliateTypeCurrentUserTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AffiliateTypeCurrentUser) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeCurrentUser#56a701f5 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AffiliateTypeCurrentUser) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeCurrentUser#56a701f5 to nil")
	}
	if err := b.ConsumeID(AffiliateTypeCurrentUserTypeID); err != nil {
		return fmt.Errorf("unable to decode affiliateTypeCurrentUser#56a701f5: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AffiliateTypeCurrentUser) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeCurrentUser#56a701f5 to nil")
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AffiliateTypeCurrentUser) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeCurrentUser#56a701f5 as nil")
	}
	b.ObjStart()
	b.PutID("affiliateTypeCurrentUser")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AffiliateTypeCurrentUser) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeCurrentUser#56a701f5 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("affiliateTypeCurrentUser"); err != nil {
				return fmt.Errorf("unable to decode affiliateTypeCurrentUser#56a701f5: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// AffiliateTypeBot represents TL type `affiliateTypeBot#c273f840`.
type AffiliateTypeBot struct {
	// User identifier of the bot
	UserID int64
}

// AffiliateTypeBotTypeID is TL type id of AffiliateTypeBot.
const AffiliateTypeBotTypeID = 0xc273f840

// construct implements constructor of AffiliateTypeClass.
func (a AffiliateTypeBot) construct() AffiliateTypeClass { return &a }

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

	_ AffiliateTypeClass = &AffiliateTypeBot{}
)

func (a *AffiliateTypeBot) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.UserID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AffiliateTypeBot) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "affiliateTypeBot",
		ID:   AffiliateTypeBotTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AffiliateTypeBot) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeBot#c273f840 as nil")
	}
	b.PutID(AffiliateTypeBotTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AffiliateTypeBot) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeBot#c273f840 as nil")
	}
	b.PutInt53(a.UserID)
	return nil
}

// Decode implements bin.Decoder.
func (a *AffiliateTypeBot) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeBot#c273f840 to nil")
	}
	if err := b.ConsumeID(AffiliateTypeBotTypeID); err != nil {
		return fmt.Errorf("unable to decode affiliateTypeBot#c273f840: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AffiliateTypeBot) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeBot#c273f840 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode affiliateTypeBot#c273f840: field user_id: %w", err)
		}
		a.UserID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AffiliateTypeBot) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeBot#c273f840 as nil")
	}
	b.ObjStart()
	b.PutID("affiliateTypeBot")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(a.UserID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AffiliateTypeBot) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeBot#c273f840 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("affiliateTypeBot"); err != nil {
				return fmt.Errorf("unable to decode affiliateTypeBot#c273f840: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode affiliateTypeBot#c273f840: field user_id: %w", err)
			}
			a.UserID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (a *AffiliateTypeBot) GetUserID() (value int64) {
	if a == nil {
		return
	}
	return a.UserID
}

// AffiliateTypeChannel represents TL type `affiliateTypeChannel#d73be869`.
type AffiliateTypeChannel struct {
	// Identifier of the channel chat
	ChatID int64
}

// AffiliateTypeChannelTypeID is TL type id of AffiliateTypeChannel.
const AffiliateTypeChannelTypeID = 0xd73be869

// construct implements constructor of AffiliateTypeClass.
func (a AffiliateTypeChannel) construct() AffiliateTypeClass { return &a }

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

	_ AffiliateTypeClass = &AffiliateTypeChannel{}
)

func (a *AffiliateTypeChannel) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.ChatID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AffiliateTypeChannel) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "affiliateTypeChannel",
		ID:   AffiliateTypeChannelTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AffiliateTypeChannel) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeChannel#d73be869 as nil")
	}
	b.PutID(AffiliateTypeChannelTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AffiliateTypeChannel) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeChannel#d73be869 as nil")
	}
	b.PutInt53(a.ChatID)
	return nil
}

// Decode implements bin.Decoder.
func (a *AffiliateTypeChannel) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeChannel#d73be869 to nil")
	}
	if err := b.ConsumeID(AffiliateTypeChannelTypeID); err != nil {
		return fmt.Errorf("unable to decode affiliateTypeChannel#d73be869: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AffiliateTypeChannel) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeChannel#d73be869 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode affiliateTypeChannel#d73be869: field chat_id: %w", err)
		}
		a.ChatID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AffiliateTypeChannel) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateTypeChannel#d73be869 as nil")
	}
	b.ObjStart()
	b.PutID("affiliateTypeChannel")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(a.ChatID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AffiliateTypeChannel) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateTypeChannel#d73be869 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("affiliateTypeChannel"); err != nil {
				return fmt.Errorf("unable to decode affiliateTypeChannel#d73be869: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode affiliateTypeChannel#d73be869: field chat_id: %w", err)
			}
			a.ChatID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (a *AffiliateTypeChannel) GetChatID() (value int64) {
	if a == nil {
		return
	}
	return a.ChatID
}

// AffiliateTypeClassName is schema name of AffiliateTypeClass.
const AffiliateTypeClassName = "AffiliateType"

// AffiliateTypeClass represents AffiliateType generic type.
//
// Example:
//
//	g, err := tdapi.DecodeAffiliateType(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.AffiliateTypeCurrentUser: // affiliateTypeCurrentUser#56a701f5
//	case *tdapi.AffiliateTypeBot: // affiliateTypeBot#c273f840
//	case *tdapi.AffiliateTypeChannel: // affiliateTypeChannel#d73be869
//	default: panic(v)
//	}
type AffiliateTypeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() AffiliateTypeClass

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

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeAffiliateType implements binary de-serialization for AffiliateTypeClass.
func DecodeAffiliateType(buf *bin.Buffer) (AffiliateTypeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case AffiliateTypeCurrentUserTypeID:
		// Decoding affiliateTypeCurrentUser#56a701f5.
		v := AffiliateTypeCurrentUser{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateTypeClass: %w", err)
		}
		return &v, nil
	case AffiliateTypeBotTypeID:
		// Decoding affiliateTypeBot#c273f840.
		v := AffiliateTypeBot{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateTypeClass: %w", err)
		}
		return &v, nil
	case AffiliateTypeChannelTypeID:
		// Decoding affiliateTypeChannel#d73be869.
		v := AffiliateTypeChannel{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode AffiliateTypeClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONAffiliateType implements binary de-serialization for AffiliateTypeClass.
func DecodeTDLibJSONAffiliateType(buf tdjson.Decoder) (AffiliateTypeClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "affiliateTypeCurrentUser":
		// Decoding affiliateTypeCurrentUser#56a701f5.
		v := AffiliateTypeCurrentUser{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateTypeClass: %w", err)
		}
		return &v, nil
	case "affiliateTypeBot":
		// Decoding affiliateTypeBot#c273f840.
		v := AffiliateTypeBot{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateTypeClass: %w", err)
		}
		return &v, nil
	case "affiliateTypeChannel":
		// Decoding affiliateTypeChannel#d73be869.
		v := AffiliateTypeChannel{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode AffiliateTypeClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// AffiliateType boxes the AffiliateTypeClass providing a helper.
type AffiliateTypeBox struct {
	AffiliateType AffiliateTypeClass
}

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

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

// DecodeTDLibJSON implements bin.Decoder for AffiliateTypeBox.
func (b *AffiliateTypeBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode AffiliateTypeBox to nil")
	}
	v, err := DecodeTDLibJSONAffiliateType(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.AffiliateType = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for AffiliateTypeBox.
func (b *AffiliateTypeBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.AffiliateType == nil {
		return fmt.Errorf("unable to encode AffiliateTypeClass as nil")
	}
	return b.AffiliateType.EncodeTDLibJSON(buf)
}
