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

// MessageForwardInfo represents TL type `messageForwardInfo#cb877b7d`.
type MessageForwardInfo struct {
	// Origin of the forwarded message
	Origin MessageOriginClass
	// Point in time (Unix timestamp) when the message was originally sent
	Date int32
	// For messages forwarded to the chat with the current user (Saved Messages), to the
	// Replies bot chat, or to the channel's discussion group, information about the source
	// message from which the message was forwarded last time; may be null for other forwards
	// or if unknown
	Source ForwardSource
	// The type of public service announcement for the forwarded message
	PublicServiceAnnouncementType string
}

// MessageForwardInfoTypeID is TL type id of MessageForwardInfo.
const MessageForwardInfoTypeID = 0xcb877b7d

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

func (m *MessageForwardInfo) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.Origin == nil) {
		return false
	}
	if !(m.Date == 0) {
		return false
	}
	if !(m.Source.Zero()) {
		return false
	}
	if !(m.PublicServiceAnnouncementType == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageForwardInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageForwardInfo",
		ID:   MessageForwardInfoTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Origin",
			SchemaName: "origin",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Source",
			SchemaName: "source",
		},
		{
			Name:       "PublicServiceAnnouncementType",
			SchemaName: "public_service_announcement_type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageForwardInfo) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageForwardInfo#cb877b7d as nil")
	}
	b.PutID(MessageForwardInfoTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageForwardInfo) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageForwardInfo#cb877b7d as nil")
	}
	if m.Origin == nil {
		return fmt.Errorf("unable to encode messageForwardInfo#cb877b7d: field origin is nil")
	}
	if err := m.Origin.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageForwardInfo#cb877b7d: field origin: %w", err)
	}
	b.PutInt32(m.Date)
	if err := m.Source.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageForwardInfo#cb877b7d: field source: %w", err)
	}
	b.PutString(m.PublicServiceAnnouncementType)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (m *MessageForwardInfo) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageForwardInfo#cb877b7d to nil")
	}
	{
		value, err := DecodeMessageOrigin(b)
		if err != nil {
			return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: field origin: %w", err)
		}
		m.Origin = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: field date: %w", err)
		}
		m.Date = value
	}
	{
		if err := m.Source.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: field source: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: field public_service_announcement_type: %w", err)
		}
		m.PublicServiceAnnouncementType = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageForwardInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageForwardInfo#cb877b7d as nil")
	}
	b.ObjStart()
	b.PutID("messageForwardInfo")
	b.Comma()
	b.FieldStart("origin")
	if m.Origin == nil {
		return fmt.Errorf("unable to encode messageForwardInfo#cb877b7d: field origin is nil")
	}
	if err := m.Origin.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageForwardInfo#cb877b7d: field origin: %w", err)
	}
	b.Comma()
	b.FieldStart("date")
	b.PutInt32(m.Date)
	b.Comma()
	b.FieldStart("source")
	if err := m.Source.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageForwardInfo#cb877b7d: field source: %w", err)
	}
	b.Comma()
	b.FieldStart("public_service_announcement_type")
	b.PutString(m.PublicServiceAnnouncementType)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageForwardInfo"); err != nil {
				return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: %w", err)
			}
		case "origin":
			value, err := DecodeTDLibJSONMessageOrigin(b)
			if err != nil {
				return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: field origin: %w", err)
			}
			m.Origin = value
		case "date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: field date: %w", err)
			}
			m.Date = value
		case "source":
			if err := m.Source.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: field source: %w", err)
			}
		case "public_service_announcement_type":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode messageForwardInfo#cb877b7d: field public_service_announcement_type: %w", err)
			}
			m.PublicServiceAnnouncementType = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetOrigin returns value of Origin field.
func (m *MessageForwardInfo) GetOrigin() (value MessageOriginClass) {
	if m == nil {
		return
	}
	return m.Origin
}

// GetDate returns value of Date field.
func (m *MessageForwardInfo) GetDate() (value int32) {
	if m == nil {
		return
	}
	return m.Date
}

// GetSource returns value of Source field.
func (m *MessageForwardInfo) GetSource() (value ForwardSource) {
	if m == nil {
		return
	}
	return m.Source
}

// GetPublicServiceAnnouncementType returns value of PublicServiceAnnouncementType field.
func (m *MessageForwardInfo) GetPublicServiceAnnouncementType() (value string) {
	if m == nil {
		return
	}
	return m.PublicServiceAnnouncementType
}
