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

// TargetChatCurrent represents TL type `targetChatCurrent#e729d110`.
type TargetChatCurrent struct {
}

// TargetChatCurrentTypeID is TL type id of TargetChatCurrent.
const TargetChatCurrentTypeID = 0xe729d110

// construct implements constructor of TargetChatClass.
func (t TargetChatCurrent) construct() TargetChatClass { return &t }

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

	_ TargetChatClass = &TargetChatCurrent{}
)

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (t *TargetChatCurrent) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatCurrent#e729d110 as nil")
	}
	b.PutID(TargetChatCurrentTypeID)
	return t.EncodeBare(b)
}

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

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

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

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

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

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

// TargetChatChosen represents TL type `targetChatChosen#acf8d5a6`.
type TargetChatChosen struct {
	// Allowed types for the chat
	Types TargetChatTypes
}

// TargetChatChosenTypeID is TL type id of TargetChatChosen.
const TargetChatChosenTypeID = 0xacf8d5a6

// construct implements constructor of TargetChatClass.
func (t TargetChatChosen) construct() TargetChatClass { return &t }

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

	_ TargetChatClass = &TargetChatChosen{}
)

func (t *TargetChatChosen) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Types.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TargetChatChosen) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "targetChatChosen",
		ID:   TargetChatChosenTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Types",
			SchemaName: "types",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TargetChatChosen) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatChosen#acf8d5a6 as nil")
	}
	b.PutID(TargetChatChosenTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TargetChatChosen) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatChosen#acf8d5a6 as nil")
	}
	if err := t.Types.Encode(b); err != nil {
		return fmt.Errorf("unable to encode targetChatChosen#acf8d5a6: field types: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (t *TargetChatChosen) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode targetChatChosen#acf8d5a6 to nil")
	}
	{
		if err := t.Types.Decode(b); err != nil {
			return fmt.Errorf("unable to decode targetChatChosen#acf8d5a6: field types: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TargetChatChosen) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatChosen#acf8d5a6 as nil")
	}
	b.ObjStart()
	b.PutID("targetChatChosen")
	b.Comma()
	b.FieldStart("types")
	if err := t.Types.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode targetChatChosen#acf8d5a6: field types: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetTypes returns value of Types field.
func (t *TargetChatChosen) GetTypes() (value TargetChatTypes) {
	if t == nil {
		return
	}
	return t.Types
}

// TargetChatInternalLink represents TL type `targetChatInternalLink#dd788fe0`.
type TargetChatInternalLink struct {
	// An internal link pointing to the chat
	Link InternalLinkTypeClass
}

// TargetChatInternalLinkTypeID is TL type id of TargetChatInternalLink.
const TargetChatInternalLinkTypeID = 0xdd788fe0

// construct implements constructor of TargetChatClass.
func (t TargetChatInternalLink) construct() TargetChatClass { return &t }

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

	_ TargetChatClass = &TargetChatInternalLink{}
)

func (t *TargetChatInternalLink) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Link == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TargetChatInternalLink) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "targetChatInternalLink",
		ID:   TargetChatInternalLinkTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Link",
			SchemaName: "link",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TargetChatInternalLink) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatInternalLink#dd788fe0 as nil")
	}
	b.PutID(TargetChatInternalLinkTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TargetChatInternalLink) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatInternalLink#dd788fe0 as nil")
	}
	if t.Link == nil {
		return fmt.Errorf("unable to encode targetChatInternalLink#dd788fe0: field link is nil")
	}
	if err := t.Link.Encode(b); err != nil {
		return fmt.Errorf("unable to encode targetChatInternalLink#dd788fe0: field link: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (t *TargetChatInternalLink) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode targetChatInternalLink#dd788fe0 to nil")
	}
	{
		value, err := DecodeInternalLinkType(b)
		if err != nil {
			return fmt.Errorf("unable to decode targetChatInternalLink#dd788fe0: field link: %w", err)
		}
		t.Link = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TargetChatInternalLink) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode targetChatInternalLink#dd788fe0 as nil")
	}
	b.ObjStart()
	b.PutID("targetChatInternalLink")
	b.Comma()
	b.FieldStart("link")
	if t.Link == nil {
		return fmt.Errorf("unable to encode targetChatInternalLink#dd788fe0: field link is nil")
	}
	if err := t.Link.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode targetChatInternalLink#dd788fe0: field link: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("targetChatInternalLink"); err != nil {
				return fmt.Errorf("unable to decode targetChatInternalLink#dd788fe0: %w", err)
			}
		case "link":
			value, err := DecodeTDLibJSONInternalLinkType(b)
			if err != nil {
				return fmt.Errorf("unable to decode targetChatInternalLink#dd788fe0: field link: %w", err)
			}
			t.Link = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetLink returns value of Link field.
func (t *TargetChatInternalLink) GetLink() (value InternalLinkTypeClass) {
	if t == nil {
		return
	}
	return t.Link
}

// TargetChatClassName is schema name of TargetChatClass.
const TargetChatClassName = "TargetChat"

// TargetChatClass represents TargetChat generic type.
//
// Example:
//
//	g, err := tdapi.DecodeTargetChat(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.TargetChatCurrent: // targetChatCurrent#e729d110
//	case *tdapi.TargetChatChosen: // targetChatChosen#acf8d5a6
//	case *tdapi.TargetChatInternalLink: // targetChatInternalLink#dd788fe0
//	default: panic(v)
//	}
type TargetChatClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() TargetChatClass

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

// DecodeTargetChat implements binary de-serialization for TargetChatClass.
func DecodeTargetChat(buf *bin.Buffer) (TargetChatClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case TargetChatCurrentTypeID:
		// Decoding targetChatCurrent#e729d110.
		v := TargetChatCurrent{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode TargetChatClass: %w", err)
		}
		return &v, nil
	case TargetChatChosenTypeID:
		// Decoding targetChatChosen#acf8d5a6.
		v := TargetChatChosen{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode TargetChatClass: %w", err)
		}
		return &v, nil
	case TargetChatInternalLinkTypeID:
		// Decoding targetChatInternalLink#dd788fe0.
		v := TargetChatInternalLink{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode TargetChatClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode TargetChatClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONTargetChat implements binary de-serialization for TargetChatClass.
func DecodeTDLibJSONTargetChat(buf tdjson.Decoder) (TargetChatClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "targetChatCurrent":
		// Decoding targetChatCurrent#e729d110.
		v := TargetChatCurrent{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode TargetChatClass: %w", err)
		}
		return &v, nil
	case "targetChatChosen":
		// Decoding targetChatChosen#acf8d5a6.
		v := TargetChatChosen{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode TargetChatClass: %w", err)
		}
		return &v, nil
	case "targetChatInternalLink":
		// Decoding targetChatInternalLink#dd788fe0.
		v := TargetChatInternalLink{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode TargetChatClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode TargetChatClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// TargetChat boxes the TargetChatClass providing a helper.
type TargetChatBox struct {
	TargetChat TargetChatClass
}

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

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

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

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