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

// MessagesReadReactionsRequest represents TL type `messages.readReactions#9ec44f93`.
// Mark message reactions »¹ as read
//
// Links:
//  1. https://core.telegram.org/api/reactions
//
// See https://core.telegram.org/method/messages.readReactions for reference.
type MessagesReadReactionsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Peer
	Peer InputPeerClass
	// Mark as read only reactions to messages within the specified forum topic¹
	//
	// Links:
	//  1) https://core.telegram.org/api/forum#forum-topics
	//
	// Use SetTopMsgID and GetTopMsgID helpers.
	TopMsgID int
	// If set, must be equal to the ID of a monoforum topic¹: will affect that topic in the
	// monoforum passed in peer.
	//
	// Links:
	//  1) https://core.telegram.org/api/monoforum
	//
	// Use SetSavedPeerID and GetSavedPeerID helpers.
	SavedPeerID InputPeerClass
}

// MessagesReadReactionsRequestTypeID is TL type id of MessagesReadReactionsRequest.
const MessagesReadReactionsRequestTypeID = 0x9ec44f93

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

func (r *MessagesReadReactionsRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.TopMsgID == 0) {
		return false
	}
	if !(r.SavedPeerID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesReadReactionsRequest from given interface.
func (r *MessagesReadReactionsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetTopMsgID() (value int, ok bool)
	GetSavedPeerID() (value InputPeerClass, ok bool)
}) {
	r.Peer = from.GetPeer()
	if val, ok := from.GetTopMsgID(); ok {
		r.TopMsgID = val
	}

	if val, ok := from.GetSavedPeerID(); ok {
		r.SavedPeerID = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*MessagesReadReactionsRequest) TypeName() string {
	return "messages.readReactions"
}

// TypeInfo returns info about TL type.
func (r *MessagesReadReactionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.readReactions",
		ID:   MessagesReadReactionsRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "TopMsgID",
			SchemaName: "top_msg_id",
			Null:       !r.Flags.Has(0),
		},
		{
			Name:       "SavedPeerID",
			SchemaName: "saved_peer_id",
			Null:       !r.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *MessagesReadReactionsRequest) SetFlags() {
	if !(r.TopMsgID == 0) {
		r.Flags.Set(0)
	}
	if !(r.SavedPeerID == nil) {
		r.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (r *MessagesReadReactionsRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.readReactions#9ec44f93 as nil")
	}
	b.PutID(MessagesReadReactionsRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesReadReactionsRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.readReactions#9ec44f93 as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.readReactions#9ec44f93: field flags: %w", err)
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode messages.readReactions#9ec44f93: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.readReactions#9ec44f93: field peer: %w", err)
	}
	if r.Flags.Has(0) {
		b.PutInt(r.TopMsgID)
	}
	if r.Flags.Has(1) {
		if r.SavedPeerID == nil {
			return fmt.Errorf("unable to encode messages.readReactions#9ec44f93: field saved_peer_id is nil")
		}
		if err := r.SavedPeerID.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.readReactions#9ec44f93: field saved_peer_id: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *MessagesReadReactionsRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.readReactions#9ec44f93 to nil")
	}
	if err := b.ConsumeID(MessagesReadReactionsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.readReactions#9ec44f93: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *MessagesReadReactionsRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.readReactions#9ec44f93 to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.readReactions#9ec44f93: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.readReactions#9ec44f93: field peer: %w", err)
		}
		r.Peer = value
	}
	if r.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.readReactions#9ec44f93: field top_msg_id: %w", err)
		}
		r.TopMsgID = value
	}
	if r.Flags.Has(1) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.readReactions#9ec44f93: field saved_peer_id: %w", err)
		}
		r.SavedPeerID = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (r *MessagesReadReactionsRequest) GetPeer() (value InputPeerClass) {
	if r == nil {
		return
	}
	return r.Peer
}

// SetTopMsgID sets value of TopMsgID conditional field.
func (r *MessagesReadReactionsRequest) SetTopMsgID(value int) {
	r.Flags.Set(0)
	r.TopMsgID = value
}

// GetTopMsgID returns value of TopMsgID conditional field and
// boolean which is true if field was set.
func (r *MessagesReadReactionsRequest) GetTopMsgID() (value int, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(0) {
		return value, false
	}
	return r.TopMsgID, true
}

// SetSavedPeerID sets value of SavedPeerID conditional field.
func (r *MessagesReadReactionsRequest) SetSavedPeerID(value InputPeerClass) {
	r.Flags.Set(1)
	r.SavedPeerID = value
}

// GetSavedPeerID returns value of SavedPeerID conditional field and
// boolean which is true if field was set.
func (r *MessagesReadReactionsRequest) GetSavedPeerID() (value InputPeerClass, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(1) {
		return value, false
	}
	return r.SavedPeerID, true
}

// MessagesReadReactions invokes method messages.readReactions#9ec44f93 returning error if any.
// Mark message reactions »¹ as read
//
// Links:
//  1. https://core.telegram.org/api/reactions
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/messages.readReactions for reference.
func (c *Client) MessagesReadReactions(ctx context.Context, request *MessagesReadReactionsRequest) (*MessagesAffectedHistory, error) {
	var result MessagesAffectedHistory

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
