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

// MessagesSendPaidReactionRequest represents TL type `messages.sendPaidReaction#58bbcb50`.
// Sends one or more paid Telegram Star reactions »¹, transferring Telegram Stars »²
// to a channel's balance.
//
// Links:
//  1. https://core.telegram.org/api/reactions#paid-reactions
//  2. https://core.telegram.org/api/stars
//
// See https://core.telegram.org/method/messages.sendPaidReaction for reference.
type MessagesSendPaidReactionRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The channel
	Peer InputPeerClass
	// The message to react to
	MsgID int
	// The number of stars¹ to send (each will increment the reaction counter by one).
	//
	// Links:
	//  1) https://core.telegram.org/api/stars
	Count int
	// Unique client message ID required to prevent message resending. Note: this argument
	// must be composed of a 64-bit integer where the lower 32 bits are random, and the
	// higher 32 bits are equal to the current unixtime, i.e. uint64_t random_id = (time() <<
	// 32) | ((uint64_t)random_uint32_t()): this differs from the random_id format of all
	// other methods in the API, which just take 64 random bits.
	RandomID int64
	// Each post with star reactions has a leaderboard with the top senders, but users can
	// opt out of appearing there if they prefer more privacy. Not populating this field will
	// use the default reaction privacy, stored on the server and synced to clients using
	// updatePaidReactionPrivacy¹ (see here² for more info).
	//
	// Links:
	//  1) https://core.telegram.org/constructor/updatePaidReactionPrivacy
	//  2) https://core.telegram.org/api/reactions#paid-reaction-privacy
	//
	// Use SetPrivate and GetPrivate helpers.
	Private PaidReactionPrivacyClass
}

// MessagesSendPaidReactionRequestTypeID is TL type id of MessagesSendPaidReactionRequest.
const MessagesSendPaidReactionRequestTypeID = 0x58bbcb50

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

func (s *MessagesSendPaidReactionRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.MsgID == 0) {
		return false
	}
	if !(s.Count == 0) {
		return false
	}
	if !(s.RandomID == 0) {
		return false
	}
	if !(s.Private == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSendPaidReactionRequest from given interface.
func (s *MessagesSendPaidReactionRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetMsgID() (value int)
	GetCount() (value int)
	GetRandomID() (value int64)
	GetPrivate() (value PaidReactionPrivacyClass, ok bool)
}) {
	s.Peer = from.GetPeer()
	s.MsgID = from.GetMsgID()
	s.Count = from.GetCount()
	s.RandomID = from.GetRandomID()
	if val, ok := from.GetPrivate(); ok {
		s.Private = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *MessagesSendPaidReactionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.sendPaidReaction",
		ID:   MessagesSendPaidReactionRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "MsgID",
			SchemaName: "msg_id",
		},
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
		{
			Name:       "Private",
			SchemaName: "private",
			Null:       !s.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *MessagesSendPaidReactionRequest) SetFlags() {
	if !(s.Private == nil) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *MessagesSendPaidReactionRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.sendPaidReaction#58bbcb50 as nil")
	}
	b.PutID(MessagesSendPaidReactionRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSendPaidReactionRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.sendPaidReaction#58bbcb50 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.sendPaidReaction#58bbcb50: field flags: %w", err)
	}
	if s.Peer == nil {
		return fmt.Errorf("unable to encode messages.sendPaidReaction#58bbcb50: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.sendPaidReaction#58bbcb50: field peer: %w", err)
	}
	b.PutInt(s.MsgID)
	b.PutInt(s.Count)
	b.PutLong(s.RandomID)
	if s.Flags.Has(0) {
		if s.Private == nil {
			return fmt.Errorf("unable to encode messages.sendPaidReaction#58bbcb50: field private is nil")
		}
		if err := s.Private.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.sendPaidReaction#58bbcb50: field private: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSendPaidReactionRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.sendPaidReaction#58bbcb50 to nil")
	}
	if err := b.ConsumeID(MessagesSendPaidReactionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.sendPaidReaction#58bbcb50: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSendPaidReactionRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.sendPaidReaction#58bbcb50 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.sendPaidReaction#58bbcb50: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendPaidReaction#58bbcb50: field peer: %w", err)
		}
		s.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendPaidReaction#58bbcb50: field msg_id: %w", err)
		}
		s.MsgID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendPaidReaction#58bbcb50: field count: %w", err)
		}
		s.Count = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendPaidReaction#58bbcb50: field random_id: %w", err)
		}
		s.RandomID = value
	}
	if s.Flags.Has(0) {
		value, err := DecodePaidReactionPrivacy(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendPaidReaction#58bbcb50: field private: %w", err)
		}
		s.Private = value
	}
	return nil
}

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

// GetMsgID returns value of MsgID field.
func (s *MessagesSendPaidReactionRequest) GetMsgID() (value int) {
	if s == nil {
		return
	}
	return s.MsgID
}

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

// GetRandomID returns value of RandomID field.
func (s *MessagesSendPaidReactionRequest) GetRandomID() (value int64) {
	if s == nil {
		return
	}
	return s.RandomID
}

// SetPrivate sets value of Private conditional field.
func (s *MessagesSendPaidReactionRequest) SetPrivate(value PaidReactionPrivacyClass) {
	s.Flags.Set(0)
	s.Private = value
}

// GetPrivate returns value of Private conditional field and
// boolean which is true if field was set.
func (s *MessagesSendPaidReactionRequest) GetPrivate() (value PaidReactionPrivacyClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Private, true
}

// MessagesSendPaidReaction invokes method messages.sendPaidReaction#58bbcb50 returning error if any.
// Sends one or more paid Telegram Star reactions »¹, transferring Telegram Stars »²
// to a channel's balance.
//
// Links:
//  1. https://core.telegram.org/api/reactions#paid-reactions
//  2. https://core.telegram.org/api/stars
//
// Possible errors:
//
//	400 BALANCE_TOO_LOW: The transaction cannot be completed because the current Telegram Stars balance is too low.
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	403 CHAT_WRITE_FORBIDDEN: You can't write in this chat.
//	400 MESSAGE_ID_INVALID: The provided message id is invalid.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 RANDOM_ID_EMPTY: Random ID empty.
//	400 RANDOM_ID_EXPIRED: The specified random_id was expired (most likely it didn't follow the required uint64_t random_id = (time() << 32) | ((uint64_t)random_uint32_t()) format, or the specified time is too far in the past).
//	400 REACTIONS_COUNT_INVALID: The specified number of reactions is invalid.
//	400 SEND_AS_PEER_INVALID: You can't send messages as the specified peer.
//
// See https://core.telegram.org/method/messages.sendPaidReaction for reference.
func (c *Client) MessagesSendPaidReaction(ctx context.Context, request *MessagesSendPaidReactionRequest) (UpdatesClass, error) {
	var result UpdatesBox

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