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

// MessageViewer represents TL type `messageViewer#56f111cd`.
type MessageViewer struct {
	// User identifier of the viewer
	UserID int64
	// Approximate point in time (Unix timestamp) when the message was viewed
	ViewDate int32
}

// MessageViewerTypeID is TL type id of MessageViewer.
const MessageViewerTypeID = 0x56f111cd

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

func (m *MessageViewer) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.UserID == 0) {
		return false
	}
	if !(m.ViewDate == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageViewer) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageViewer",
		ID:   MessageViewerTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "ViewDate",
			SchemaName: "view_date",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageViewer) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageViewer#56f111cd as nil")
	}
	b.PutID(MessageViewerTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageViewer) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageViewer#56f111cd as nil")
	}
	b.PutInt53(m.UserID)
	b.PutInt32(m.ViewDate)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (m *MessageViewer) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageViewer#56f111cd to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode messageViewer#56f111cd: field user_id: %w", err)
		}
		m.UserID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode messageViewer#56f111cd: field view_date: %w", err)
		}
		m.ViewDate = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageViewer) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageViewer#56f111cd as nil")
	}
	b.ObjStart()
	b.PutID("messageViewer")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(m.UserID)
	b.Comma()
	b.FieldStart("view_date")
	b.PutInt32(m.ViewDate)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageViewer"); err != nil {
				return fmt.Errorf("unable to decode messageViewer#56f111cd: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode messageViewer#56f111cd: field user_id: %w", err)
			}
			m.UserID = value
		case "view_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode messageViewer#56f111cd: field view_date: %w", err)
			}
			m.ViewDate = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (m *MessageViewer) GetUserID() (value int64) {
	if m == nil {
		return
	}
	return m.UserID
}

// GetViewDate returns value of ViewDate field.
func (m *MessageViewer) GetViewDate() (value int32) {
	if m == nil {
		return
	}
	return m.ViewDate
}
