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

// MessageEffect represents TL type `messageEffect#972a492f`.
type MessageEffect struct {
	// Unique identifier of the effect
	ID int64
	// Static icon for the effect in WEBP format; may be null if none
	StaticIcon Sticker
	// Emoji corresponding to the effect that can be used if static icon isn't available
	Emoji string
	// True, if Telegram Premium subscription is required to use the effect
	IsPremium bool
	// Type of the effect
	Type MessageEffectTypeClass
}

// MessageEffectTypeID is TL type id of MessageEffect.
const MessageEffectTypeID = 0x972a492f

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

func (m *MessageEffect) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.ID == 0) {
		return false
	}
	if !(m.StaticIcon.Zero()) {
		return false
	}
	if !(m.Emoji == "") {
		return false
	}
	if !(m.IsPremium == false) {
		return false
	}
	if !(m.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageEffect) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageEffect",
		ID:   MessageEffectTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "StaticIcon",
			SchemaName: "static_icon",
		},
		{
			Name:       "Emoji",
			SchemaName: "emoji",
		},
		{
			Name:       "IsPremium",
			SchemaName: "is_premium",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageEffect) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageEffect#972a492f as nil")
	}
	b.PutID(MessageEffectTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageEffect) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageEffect#972a492f as nil")
	}
	b.PutLong(m.ID)
	if err := m.StaticIcon.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageEffect#972a492f: field static_icon: %w", err)
	}
	b.PutString(m.Emoji)
	b.PutBool(m.IsPremium)
	if m.Type == nil {
		return fmt.Errorf("unable to encode messageEffect#972a492f: field type is nil")
	}
	if err := m.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageEffect#972a492f: field type: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageEffect) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageEffect#972a492f to nil")
	}
	if err := b.ConsumeID(MessageEffectTypeID); err != nil {
		return fmt.Errorf("unable to decode messageEffect#972a492f: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageEffect) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageEffect#972a492f to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messageEffect#972a492f: field id: %w", err)
		}
		m.ID = value
	}
	{
		if err := m.StaticIcon.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messageEffect#972a492f: field static_icon: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messageEffect#972a492f: field emoji: %w", err)
		}
		m.Emoji = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messageEffect#972a492f: field is_premium: %w", err)
		}
		m.IsPremium = value
	}
	{
		value, err := DecodeMessageEffectType(b)
		if err != nil {
			return fmt.Errorf("unable to decode messageEffect#972a492f: field type: %w", err)
		}
		m.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageEffect) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageEffect#972a492f as nil")
	}
	b.ObjStart()
	b.PutID("messageEffect")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(m.ID)
	b.Comma()
	b.FieldStart("static_icon")
	if err := m.StaticIcon.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageEffect#972a492f: field static_icon: %w", err)
	}
	b.Comma()
	b.FieldStart("emoji")
	b.PutString(m.Emoji)
	b.Comma()
	b.FieldStart("is_premium")
	b.PutBool(m.IsPremium)
	b.Comma()
	b.FieldStart("type")
	if m.Type == nil {
		return fmt.Errorf("unable to encode messageEffect#972a492f: field type is nil")
	}
	if err := m.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageEffect#972a492f: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageEffect) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageEffect#972a492f to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageEffect"); err != nil {
				return fmt.Errorf("unable to decode messageEffect#972a492f: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode messageEffect#972a492f: field id: %w", err)
			}
			m.ID = value
		case "static_icon":
			if err := m.StaticIcon.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode messageEffect#972a492f: field static_icon: %w", err)
			}
		case "emoji":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode messageEffect#972a492f: field emoji: %w", err)
			}
			m.Emoji = value
		case "is_premium":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode messageEffect#972a492f: field is_premium: %w", err)
			}
			m.IsPremium = value
		case "type":
			value, err := DecodeTDLibJSONMessageEffectType(b)
			if err != nil {
				return fmt.Errorf("unable to decode messageEffect#972a492f: field type: %w", err)
			}
			m.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (m *MessageEffect) GetID() (value int64) {
	if m == nil {
		return
	}
	return m.ID
}

// GetStaticIcon returns value of StaticIcon field.
func (m *MessageEffect) GetStaticIcon() (value Sticker) {
	if m == nil {
		return
	}
	return m.StaticIcon
}

// GetEmoji returns value of Emoji field.
func (m *MessageEffect) GetEmoji() (value string) {
	if m == nil {
		return
	}
	return m.Emoji
}

// GetIsPremium returns value of IsPremium field.
func (m *MessageEffect) GetIsPremium() (value bool) {
	if m == nil {
		return
	}
	return m.IsPremium
}

// GetType returns value of Type field.
func (m *MessageEffect) GetType() (value MessageEffectTypeClass) {
	if m == nil {
		return
	}
	return m.Type
}
