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

// MessageStatistics represents TL type `messageStatistics#a2ce4f07`.
type MessageStatistics struct {
	// A graph containing number of message views and shares
	MessageInteractionGraph StatisticalGraphClass
	// A graph containing number of message reactions
	MessageReactionGraph StatisticalGraphClass
}

// MessageStatisticsTypeID is TL type id of MessageStatistics.
const MessageStatisticsTypeID = 0xa2ce4f07

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

func (m *MessageStatistics) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.MessageInteractionGraph == nil) {
		return false
	}
	if !(m.MessageReactionGraph == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageStatistics) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageStatistics",
		ID:   MessageStatisticsTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MessageInteractionGraph",
			SchemaName: "message_interaction_graph",
		},
		{
			Name:       "MessageReactionGraph",
			SchemaName: "message_reaction_graph",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageStatistics) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageStatistics#a2ce4f07 as nil")
	}
	b.PutID(MessageStatisticsTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageStatistics) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageStatistics#a2ce4f07 as nil")
	}
	if m.MessageInteractionGraph == nil {
		return fmt.Errorf("unable to encode messageStatistics#a2ce4f07: field message_interaction_graph is nil")
	}
	if err := m.MessageInteractionGraph.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageStatistics#a2ce4f07: field message_interaction_graph: %w", err)
	}
	if m.MessageReactionGraph == nil {
		return fmt.Errorf("unable to encode messageStatistics#a2ce4f07: field message_reaction_graph is nil")
	}
	if err := m.MessageReactionGraph.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageStatistics#a2ce4f07: field message_reaction_graph: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (m *MessageStatistics) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageStatistics#a2ce4f07 to nil")
	}
	{
		value, err := DecodeStatisticalGraph(b)
		if err != nil {
			return fmt.Errorf("unable to decode messageStatistics#a2ce4f07: field message_interaction_graph: %w", err)
		}
		m.MessageInteractionGraph = value
	}
	{
		value, err := DecodeStatisticalGraph(b)
		if err != nil {
			return fmt.Errorf("unable to decode messageStatistics#a2ce4f07: field message_reaction_graph: %w", err)
		}
		m.MessageReactionGraph = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageStatistics) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageStatistics#a2ce4f07 as nil")
	}
	b.ObjStart()
	b.PutID("messageStatistics")
	b.Comma()
	b.FieldStart("message_interaction_graph")
	if m.MessageInteractionGraph == nil {
		return fmt.Errorf("unable to encode messageStatistics#a2ce4f07: field message_interaction_graph is nil")
	}
	if err := m.MessageInteractionGraph.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageStatistics#a2ce4f07: field message_interaction_graph: %w", err)
	}
	b.Comma()
	b.FieldStart("message_reaction_graph")
	if m.MessageReactionGraph == nil {
		return fmt.Errorf("unable to encode messageStatistics#a2ce4f07: field message_reaction_graph is nil")
	}
	if err := m.MessageReactionGraph.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageStatistics#a2ce4f07: field message_reaction_graph: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageStatistics"); err != nil {
				return fmt.Errorf("unable to decode messageStatistics#a2ce4f07: %w", err)
			}
		case "message_interaction_graph":
			value, err := DecodeTDLibJSONStatisticalGraph(b)
			if err != nil {
				return fmt.Errorf("unable to decode messageStatistics#a2ce4f07: field message_interaction_graph: %w", err)
			}
			m.MessageInteractionGraph = value
		case "message_reaction_graph":
			value, err := DecodeTDLibJSONStatisticalGraph(b)
			if err != nil {
				return fmt.Errorf("unable to decode messageStatistics#a2ce4f07: field message_reaction_graph: %w", err)
			}
			m.MessageReactionGraph = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMessageInteractionGraph returns value of MessageInteractionGraph field.
func (m *MessageStatistics) GetMessageInteractionGraph() (value StatisticalGraphClass) {
	if m == nil {
		return
	}
	return m.MessageInteractionGraph
}

// GetMessageReactionGraph returns value of MessageReactionGraph field.
func (m *MessageStatistics) GetMessageReactionGraph() (value StatisticalGraphClass) {
	if m == nil {
		return
	}
	return m.MessageReactionGraph
}
