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

// AddPendingPaidMessageReactionRequest represents TL type `addPendingPaidMessageReaction#eb9bcdd3`.
type AddPendingPaidMessageReactionRequest struct {
	// Identifier of the chat to which the message belongs
	ChatID int64
	// Identifier of the message
	MessageID int64
	// Number of Telegram Stars to be used for the reaction. The total number of pending paid
	// reactions must not exceed getOption("paid_reaction_star_count_max")
	StarCount int64
	// Type of the paid reaction; pass null if the user didn't choose reaction type
	// explicitly, for example, the reaction is set from the message bubble
	Type PaidReactionTypeClass
}

// AddPendingPaidMessageReactionRequestTypeID is TL type id of AddPendingPaidMessageReactionRequest.
const AddPendingPaidMessageReactionRequestTypeID = 0xeb9bcdd3

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

func (a *AddPendingPaidMessageReactionRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.ChatID == 0) {
		return false
	}
	if !(a.MessageID == 0) {
		return false
	}
	if !(a.StarCount == 0) {
		return false
	}
	if !(a.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AddPendingPaidMessageReactionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "addPendingPaidMessageReaction",
		ID:   AddPendingPaidMessageReactionRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "StarCount",
			SchemaName: "star_count",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AddPendingPaidMessageReactionRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addPendingPaidMessageReaction#eb9bcdd3 as nil")
	}
	b.PutID(AddPendingPaidMessageReactionRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AddPendingPaidMessageReactionRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addPendingPaidMessageReaction#eb9bcdd3 as nil")
	}
	b.PutInt53(a.ChatID)
	b.PutInt53(a.MessageID)
	b.PutInt53(a.StarCount)
	if a.Type == nil {
		return fmt.Errorf("unable to encode addPendingPaidMessageReaction#eb9bcdd3: field type is nil")
	}
	if err := a.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode addPendingPaidMessageReaction#eb9bcdd3: field type: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AddPendingPaidMessageReactionRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addPendingPaidMessageReaction#eb9bcdd3 to nil")
	}
	if err := b.ConsumeID(AddPendingPaidMessageReactionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AddPendingPaidMessageReactionRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addPendingPaidMessageReaction#eb9bcdd3 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: field chat_id: %w", err)
		}
		a.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: field message_id: %w", err)
		}
		a.MessageID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: field star_count: %w", err)
		}
		a.StarCount = value
	}
	{
		value, err := DecodePaidReactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: field type: %w", err)
		}
		a.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AddPendingPaidMessageReactionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode addPendingPaidMessageReaction#eb9bcdd3 as nil")
	}
	b.ObjStart()
	b.PutID("addPendingPaidMessageReaction")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(a.ChatID)
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(a.MessageID)
	b.Comma()
	b.FieldStart("star_count")
	b.PutInt53(a.StarCount)
	b.Comma()
	b.FieldStart("type")
	if a.Type == nil {
		return fmt.Errorf("unable to encode addPendingPaidMessageReaction#eb9bcdd3: field type is nil")
	}
	if err := a.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode addPendingPaidMessageReaction#eb9bcdd3: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AddPendingPaidMessageReactionRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode addPendingPaidMessageReaction#eb9bcdd3 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("addPendingPaidMessageReaction"); err != nil {
				return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: field chat_id: %w", err)
			}
			a.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: field message_id: %w", err)
			}
			a.MessageID = value
		case "star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: field star_count: %w", err)
			}
			a.StarCount = value
		case "type":
			value, err := DecodeTDLibJSONPaidReactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode addPendingPaidMessageReaction#eb9bcdd3: field type: %w", err)
			}
			a.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (a *AddPendingPaidMessageReactionRequest) GetChatID() (value int64) {
	if a == nil {
		return
	}
	return a.ChatID
}

// GetMessageID returns value of MessageID field.
func (a *AddPendingPaidMessageReactionRequest) GetMessageID() (value int64) {
	if a == nil {
		return
	}
	return a.MessageID
}

// GetStarCount returns value of StarCount field.
func (a *AddPendingPaidMessageReactionRequest) GetStarCount() (value int64) {
	if a == nil {
		return
	}
	return a.StarCount
}

// GetType returns value of Type field.
func (a *AddPendingPaidMessageReactionRequest) GetType() (value PaidReactionTypeClass) {
	if a == nil {
		return
	}
	return a.Type
}

// AddPendingPaidMessageReaction invokes method addPendingPaidMessageReaction#eb9bcdd3 returning error if any.
func (c *Client) AddPendingPaidMessageReaction(ctx context.Context, request *AddPendingPaidMessageReactionRequest) error {
	var ok Ok

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