// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// NotifyPeer represents TL type `notifyPeer#9fd40bd8`.
// Notifications generated by a certain user or group.
//
// See https://core.telegram.org/constructor/notifyPeer for reference.
type NotifyPeer struct {
	// user or group
	Peer PeerClass
}

// NotifyPeerTypeID is TL type id of NotifyPeer.
const NotifyPeerTypeID = 0x9fd40bd8

// construct implements constructor of NotifyPeerClass.
func (n NotifyPeer) construct() NotifyPeerClass { return &n }

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

	_ NotifyPeerClass = &NotifyPeer{}
)

func (n *NotifyPeer) Zero() bool {
	if n == nil {
		return true
	}
	if !(n.Peer == nil) {
		return false
	}

	return true
}

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

// FillFrom fills NotifyPeer from given interface.
func (n *NotifyPeer) FillFrom(from interface {
	GetPeer() (value PeerClass)
}) {
	n.Peer = from.GetPeer()
}

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

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

// TypeInfo returns info about TL type.
func (n *NotifyPeer) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "notifyPeer",
		ID:   NotifyPeerTypeID,
	}
	if n == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (n *NotifyPeer) Encode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notifyPeer#9fd40bd8 as nil")
	}
	b.PutID(NotifyPeerTypeID)
	return n.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (n *NotifyPeer) EncodeBare(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notifyPeer#9fd40bd8 as nil")
	}
	if n.Peer == nil {
		return fmt.Errorf("unable to encode notifyPeer#9fd40bd8: field peer is nil")
	}
	if err := n.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode notifyPeer#9fd40bd8: field peer: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (n *NotifyPeer) Decode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't decode notifyPeer#9fd40bd8 to nil")
	}
	if err := b.ConsumeID(NotifyPeerTypeID); err != nil {
		return fmt.Errorf("unable to decode notifyPeer#9fd40bd8: %w", err)
	}
	return n.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (n *NotifyPeer) DecodeBare(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't decode notifyPeer#9fd40bd8 to nil")
	}
	{
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode notifyPeer#9fd40bd8: field peer: %w", err)
		}
		n.Peer = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (n *NotifyPeer) GetPeer() (value PeerClass) {
	if n == nil {
		return
	}
	return n.Peer
}

// NotifyUsers represents TL type `notifyUsers#b4c83b4c`.
// Notifications generated by all users.
//
// See https://core.telegram.org/constructor/notifyUsers for reference.
type NotifyUsers struct {
}

// NotifyUsersTypeID is TL type id of NotifyUsers.
const NotifyUsersTypeID = 0xb4c83b4c

// construct implements constructor of NotifyPeerClass.
func (n NotifyUsers) construct() NotifyPeerClass { return &n }

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

	_ NotifyPeerClass = &NotifyUsers{}
)

func (n *NotifyUsers) Zero() bool {
	if n == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (n *NotifyUsers) Encode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notifyUsers#b4c83b4c as nil")
	}
	b.PutID(NotifyUsersTypeID)
	return n.EncodeBare(b)
}

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

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

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

// NotifyChats represents TL type `notifyChats#c007cec3`.
// Notifications generated by all groups.
//
// See https://core.telegram.org/constructor/notifyChats for reference.
type NotifyChats struct {
}

// NotifyChatsTypeID is TL type id of NotifyChats.
const NotifyChatsTypeID = 0xc007cec3

// construct implements constructor of NotifyPeerClass.
func (n NotifyChats) construct() NotifyPeerClass { return &n }

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

	_ NotifyPeerClass = &NotifyChats{}
)

func (n *NotifyChats) Zero() bool {
	if n == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (n *NotifyChats) Encode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notifyChats#c007cec3 as nil")
	}
	b.PutID(NotifyChatsTypeID)
	return n.EncodeBare(b)
}

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

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

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

// NotifyBroadcasts represents TL type `notifyBroadcasts#d612e8ef`.
// Channel notification settings
//
// See https://core.telegram.org/constructor/notifyBroadcasts for reference.
type NotifyBroadcasts struct {
}

// NotifyBroadcastsTypeID is TL type id of NotifyBroadcasts.
const NotifyBroadcastsTypeID = 0xd612e8ef

// construct implements constructor of NotifyPeerClass.
func (n NotifyBroadcasts) construct() NotifyPeerClass { return &n }

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

	_ NotifyPeerClass = &NotifyBroadcasts{}
)

func (n *NotifyBroadcasts) Zero() bool {
	if n == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (n *NotifyBroadcasts) Encode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notifyBroadcasts#d612e8ef as nil")
	}
	b.PutID(NotifyBroadcastsTypeID)
	return n.EncodeBare(b)
}

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

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

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

// NotifyForumTopic represents TL type `notifyForumTopic#226e6308`.
// Notifications generated by a topic¹ in a forum².
//
// Links:
//  1. https://core.telegram.org/api/forum#forum-topics
//  2. https://core.telegram.org/api/forum
//
// See https://core.telegram.org/constructor/notifyForumTopic for reference.
type NotifyForumTopic struct {
	// Forum ID
	Peer PeerClass
	// Topic ID¹
	//
	// Links:
	//  1) https://core.telegram.org/api/forum#forum-topics
	TopMsgID int
}

// NotifyForumTopicTypeID is TL type id of NotifyForumTopic.
const NotifyForumTopicTypeID = 0x226e6308

// construct implements constructor of NotifyPeerClass.
func (n NotifyForumTopic) construct() NotifyPeerClass { return &n }

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

	_ NotifyPeerClass = &NotifyForumTopic{}
)

func (n *NotifyForumTopic) Zero() bool {
	if n == nil {
		return true
	}
	if !(n.Peer == nil) {
		return false
	}
	if !(n.TopMsgID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills NotifyForumTopic from given interface.
func (n *NotifyForumTopic) FillFrom(from interface {
	GetPeer() (value PeerClass)
	GetTopMsgID() (value int)
}) {
	n.Peer = from.GetPeer()
	n.TopMsgID = from.GetTopMsgID()
}

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

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

// TypeInfo returns info about TL type.
func (n *NotifyForumTopic) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "notifyForumTopic",
		ID:   NotifyForumTopicTypeID,
	}
	if n == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "TopMsgID",
			SchemaName: "top_msg_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (n *NotifyForumTopic) Encode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notifyForumTopic#226e6308 as nil")
	}
	b.PutID(NotifyForumTopicTypeID)
	return n.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (n *NotifyForumTopic) EncodeBare(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notifyForumTopic#226e6308 as nil")
	}
	if n.Peer == nil {
		return fmt.Errorf("unable to encode notifyForumTopic#226e6308: field peer is nil")
	}
	if err := n.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode notifyForumTopic#226e6308: field peer: %w", err)
	}
	b.PutInt(n.TopMsgID)
	return nil
}

// Decode implements bin.Decoder.
func (n *NotifyForumTopic) Decode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't decode notifyForumTopic#226e6308 to nil")
	}
	if err := b.ConsumeID(NotifyForumTopicTypeID); err != nil {
		return fmt.Errorf("unable to decode notifyForumTopic#226e6308: %w", err)
	}
	return n.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (n *NotifyForumTopic) DecodeBare(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't decode notifyForumTopic#226e6308 to nil")
	}
	{
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode notifyForumTopic#226e6308: field peer: %w", err)
		}
		n.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode notifyForumTopic#226e6308: field top_msg_id: %w", err)
		}
		n.TopMsgID = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (n *NotifyForumTopic) GetPeer() (value PeerClass) {
	if n == nil {
		return
	}
	return n.Peer
}

// GetTopMsgID returns value of TopMsgID field.
func (n *NotifyForumTopic) GetTopMsgID() (value int) {
	if n == nil {
		return
	}
	return n.TopMsgID
}

// NotifyPeerClassName is schema name of NotifyPeerClass.
const NotifyPeerClassName = "NotifyPeer"

// NotifyPeerClass represents NotifyPeer generic type.
//
// See https://core.telegram.org/type/NotifyPeer for reference.
//
// Example:
//
//	g, err := tg.DecodeNotifyPeer(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.NotifyPeer: // notifyPeer#9fd40bd8
//	case *tg.NotifyUsers: // notifyUsers#b4c83b4c
//	case *tg.NotifyChats: // notifyChats#c007cec3
//	case *tg.NotifyBroadcasts: // notifyBroadcasts#d612e8ef
//	case *tg.NotifyForumTopic: // notifyForumTopic#226e6308
//	default: panic(v)
//	}
type NotifyPeerClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() NotifyPeerClass

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

// DecodeNotifyPeer implements binary de-serialization for NotifyPeerClass.
func DecodeNotifyPeer(buf *bin.Buffer) (NotifyPeerClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case NotifyPeerTypeID:
		// Decoding notifyPeer#9fd40bd8.
		v := NotifyPeer{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode NotifyPeerClass: %w", err)
		}
		return &v, nil
	case NotifyUsersTypeID:
		// Decoding notifyUsers#b4c83b4c.
		v := NotifyUsers{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode NotifyPeerClass: %w", err)
		}
		return &v, nil
	case NotifyChatsTypeID:
		// Decoding notifyChats#c007cec3.
		v := NotifyChats{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode NotifyPeerClass: %w", err)
		}
		return &v, nil
	case NotifyBroadcastsTypeID:
		// Decoding notifyBroadcasts#d612e8ef.
		v := NotifyBroadcasts{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode NotifyPeerClass: %w", err)
		}
		return &v, nil
	case NotifyForumTopicTypeID:
		// Decoding notifyForumTopic#226e6308.
		v := NotifyForumTopic{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode NotifyPeerClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode NotifyPeerClass: %w", bin.NewUnexpectedID(id))
	}
}

// NotifyPeer boxes the NotifyPeerClass providing a helper.
type NotifyPeerBox struct {
	NotifyPeer NotifyPeerClass
}

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

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