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

// MessageSelfDestructTypeTimer represents TL type `messageSelfDestructTypeTimer#508d57cd`.
type MessageSelfDestructTypeTimer struct {
	// The message's self-destruct time, in seconds; must be between 0 and 60 in private
	// chats
	SelfDestructTime int32
}

// MessageSelfDestructTypeTimerTypeID is TL type id of MessageSelfDestructTypeTimer.
const MessageSelfDestructTypeTimerTypeID = 0x508d57cd

// construct implements constructor of MessageSelfDestructTypeClass.
func (m MessageSelfDestructTypeTimer) construct() MessageSelfDestructTypeClass { return &m }

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

	_ MessageSelfDestructTypeClass = &MessageSelfDestructTypeTimer{}
)

func (m *MessageSelfDestructTypeTimer) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.SelfDestructTime == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageSelfDestructTypeTimer) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageSelfDestructTypeTimer",
		ID:   MessageSelfDestructTypeTimerTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SelfDestructTime",
			SchemaName: "self_destruct_time",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageSelfDestructTypeTimer) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageSelfDestructTypeTimer#508d57cd as nil")
	}
	b.PutID(MessageSelfDestructTypeTimerTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageSelfDestructTypeTimer) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageSelfDestructTypeTimer#508d57cd as nil")
	}
	b.PutInt32(m.SelfDestructTime)
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageSelfDestructTypeTimer) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageSelfDestructTypeTimer#508d57cd to nil")
	}
	if err := b.ConsumeID(MessageSelfDestructTypeTimerTypeID); err != nil {
		return fmt.Errorf("unable to decode messageSelfDestructTypeTimer#508d57cd: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageSelfDestructTypeTimer) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageSelfDestructTypeTimer#508d57cd to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode messageSelfDestructTypeTimer#508d57cd: field self_destruct_time: %w", err)
		}
		m.SelfDestructTime = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageSelfDestructTypeTimer) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageSelfDestructTypeTimer#508d57cd as nil")
	}
	b.ObjStart()
	b.PutID("messageSelfDestructTypeTimer")
	b.Comma()
	b.FieldStart("self_destruct_time")
	b.PutInt32(m.SelfDestructTime)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageSelfDestructTypeTimer) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageSelfDestructTypeTimer#508d57cd to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageSelfDestructTypeTimer"); err != nil {
				return fmt.Errorf("unable to decode messageSelfDestructTypeTimer#508d57cd: %w", err)
			}
		case "self_destruct_time":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode messageSelfDestructTypeTimer#508d57cd: field self_destruct_time: %w", err)
			}
			m.SelfDestructTime = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSelfDestructTime returns value of SelfDestructTime field.
func (m *MessageSelfDestructTypeTimer) GetSelfDestructTime() (value int32) {
	if m == nil {
		return
	}
	return m.SelfDestructTime
}

// MessageSelfDestructTypeImmediately represents TL type `messageSelfDestructTypeImmediately#c23c9005`.
type MessageSelfDestructTypeImmediately struct {
}

// MessageSelfDestructTypeImmediatelyTypeID is TL type id of MessageSelfDestructTypeImmediately.
const MessageSelfDestructTypeImmediatelyTypeID = 0xc23c9005

// construct implements constructor of MessageSelfDestructTypeClass.
func (m MessageSelfDestructTypeImmediately) construct() MessageSelfDestructTypeClass { return &m }

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

	_ MessageSelfDestructTypeClass = &MessageSelfDestructTypeImmediately{}
)

func (m *MessageSelfDestructTypeImmediately) Zero() bool {
	if m == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (m *MessageSelfDestructTypeImmediately) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageSelfDestructTypeImmediately#c23c9005 as nil")
	}
	b.PutID(MessageSelfDestructTypeImmediatelyTypeID)
	return m.EncodeBare(b)
}

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

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

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

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

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

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

// MessageSelfDestructTypeClassName is schema name of MessageSelfDestructTypeClass.
const MessageSelfDestructTypeClassName = "MessageSelfDestructType"

// MessageSelfDestructTypeClass represents MessageSelfDestructType generic type.
//
// Example:
//
//	g, err := tdapi.DecodeMessageSelfDestructType(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.MessageSelfDestructTypeTimer: // messageSelfDestructTypeTimer#508d57cd
//	case *tdapi.MessageSelfDestructTypeImmediately: // messageSelfDestructTypeImmediately#c23c9005
//	default: panic(v)
//	}
type MessageSelfDestructTypeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessageSelfDestructTypeClass

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

// DecodeMessageSelfDestructType implements binary de-serialization for MessageSelfDestructTypeClass.
func DecodeMessageSelfDestructType(buf *bin.Buffer) (MessageSelfDestructTypeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case MessageSelfDestructTypeTimerTypeID:
		// Decoding messageSelfDestructTypeTimer#508d57cd.
		v := MessageSelfDestructTypeTimer{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageSelfDestructTypeClass: %w", err)
		}
		return &v, nil
	case MessageSelfDestructTypeImmediatelyTypeID:
		// Decoding messageSelfDestructTypeImmediately#c23c9005.
		v := MessageSelfDestructTypeImmediately{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageSelfDestructTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessageSelfDestructTypeClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONMessageSelfDestructType implements binary de-serialization for MessageSelfDestructTypeClass.
func DecodeTDLibJSONMessageSelfDestructType(buf tdjson.Decoder) (MessageSelfDestructTypeClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "messageSelfDestructTypeTimer":
		// Decoding messageSelfDestructTypeTimer#508d57cd.
		v := MessageSelfDestructTypeTimer{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageSelfDestructTypeClass: %w", err)
		}
		return &v, nil
	case "messageSelfDestructTypeImmediately":
		// Decoding messageSelfDestructTypeImmediately#c23c9005.
		v := MessageSelfDestructTypeImmediately{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessageSelfDestructTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessageSelfDestructTypeClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// MessageSelfDestructType boxes the MessageSelfDestructTypeClass providing a helper.
type MessageSelfDestructTypeBox struct {
	MessageSelfDestructType MessageSelfDestructTypeClass
}

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

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

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

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