// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessageReactor represents TL type `messageReactor#4ba3a95a`.
// Info about a user in the paid Star reactions leaderboard¹ for a message.
//
// Links:
//  1. https://core.telegram.org/api/reactions#paid-reactions
//
// See https://core.telegram.org/constructor/messageReactor for reference.
type MessageReactor struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, the reactor is one of the most active reactors; may be unset if the reactor is
	// the current user.
	Top bool
	// If set, this reactor is the current user.
	My bool
	// If set, the reactor is anonymous.
	Anonymous bool
	// Identifier of the peer that reacted: may be unset for anonymous reactors different
	// from the current user (i.e. if the current user sent an anonymous reaction anonymous
	// will be set but this field will also be set).
	//
	// Use SetPeerID and GetPeerID helpers.
	PeerID PeerClass
	// The number of sent Telegram Stars.
	Count int
}

// MessageReactorTypeID is TL type id of MessageReactor.
const MessageReactorTypeID = 0x4ba3a95a

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

func (m *MessageReactor) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.Flags.Zero()) {
		return false
	}
	if !(m.Top == false) {
		return false
	}
	if !(m.My == false) {
		return false
	}
	if !(m.Anonymous == false) {
		return false
	}
	if !(m.PeerID == nil) {
		return false
	}
	if !(m.Count == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessageReactor from given interface.
func (m *MessageReactor) FillFrom(from interface {
	GetTop() (value bool)
	GetMy() (value bool)
	GetAnonymous() (value bool)
	GetPeerID() (value PeerClass, ok bool)
	GetCount() (value int)
}) {
	m.Top = from.GetTop()
	m.My = from.GetMy()
	m.Anonymous = from.GetAnonymous()
	if val, ok := from.GetPeerID(); ok {
		m.PeerID = val
	}

	m.Count = from.GetCount()
}

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

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

// TypeInfo returns info about TL type.
func (m *MessageReactor) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageReactor",
		ID:   MessageReactorTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Top",
			SchemaName: "top",
			Null:       !m.Flags.Has(0),
		},
		{
			Name:       "My",
			SchemaName: "my",
			Null:       !m.Flags.Has(1),
		},
		{
			Name:       "Anonymous",
			SchemaName: "anonymous",
			Null:       !m.Flags.Has(2),
		},
		{
			Name:       "PeerID",
			SchemaName: "peer_id",
			Null:       !m.Flags.Has(3),
		},
		{
			Name:       "Count",
			SchemaName: "count",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (m *MessageReactor) SetFlags() {
	if !(m.Top == false) {
		m.Flags.Set(0)
	}
	if !(m.My == false) {
		m.Flags.Set(1)
	}
	if !(m.Anonymous == false) {
		m.Flags.Set(2)
	}
	if !(m.PeerID == nil) {
		m.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (m *MessageReactor) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageReactor#4ba3a95a as nil")
	}
	b.PutID(MessageReactorTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageReactor) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageReactor#4ba3a95a as nil")
	}
	m.SetFlags()
	if err := m.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageReactor#4ba3a95a: field flags: %w", err)
	}
	if m.Flags.Has(3) {
		if m.PeerID == nil {
			return fmt.Errorf("unable to encode messageReactor#4ba3a95a: field peer_id is nil")
		}
		if err := m.PeerID.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messageReactor#4ba3a95a: field peer_id: %w", err)
		}
	}
	b.PutInt(m.Count)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (m *MessageReactor) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageReactor#4ba3a95a to nil")
	}
	{
		if err := m.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messageReactor#4ba3a95a: field flags: %w", err)
		}
	}
	m.Top = m.Flags.Has(0)
	m.My = m.Flags.Has(1)
	m.Anonymous = m.Flags.Has(2)
	if m.Flags.Has(3) {
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messageReactor#4ba3a95a: field peer_id: %w", err)
		}
		m.PeerID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messageReactor#4ba3a95a: field count: %w", err)
		}
		m.Count = value
	}
	return nil
}

// SetTop sets value of Top conditional field.
func (m *MessageReactor) SetTop(value bool) {
	if value {
		m.Flags.Set(0)
		m.Top = true
	} else {
		m.Flags.Unset(0)
		m.Top = false
	}
}

// GetTop returns value of Top conditional field.
func (m *MessageReactor) GetTop() (value bool) {
	if m == nil {
		return
	}
	return m.Flags.Has(0)
}

// SetMy sets value of My conditional field.
func (m *MessageReactor) SetMy(value bool) {
	if value {
		m.Flags.Set(1)
		m.My = true
	} else {
		m.Flags.Unset(1)
		m.My = false
	}
}

// GetMy returns value of My conditional field.
func (m *MessageReactor) GetMy() (value bool) {
	if m == nil {
		return
	}
	return m.Flags.Has(1)
}

// SetAnonymous sets value of Anonymous conditional field.
func (m *MessageReactor) SetAnonymous(value bool) {
	if value {
		m.Flags.Set(2)
		m.Anonymous = true
	} else {
		m.Flags.Unset(2)
		m.Anonymous = false
	}
}

// GetAnonymous returns value of Anonymous conditional field.
func (m *MessageReactor) GetAnonymous() (value bool) {
	if m == nil {
		return
	}
	return m.Flags.Has(2)
}

// SetPeerID sets value of PeerID conditional field.
func (m *MessageReactor) SetPeerID(value PeerClass) {
	m.Flags.Set(3)
	m.PeerID = value
}

// GetPeerID returns value of PeerID conditional field and
// boolean which is true if field was set.
func (m *MessageReactor) GetPeerID() (value PeerClass, ok bool) {
	if m == nil {
		return
	}
	if !m.Flags.Has(3) {
		return value, false
	}
	return m.PeerID, true
}

// GetCount returns value of Count field.
func (m *MessageReactor) GetCount() (value int) {
	if m == nil {
		return
	}
	return m.Count
}
