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

// ResendMessagesRequest represents TL type `resendMessages#11f0d054`.
type ResendMessagesRequest struct {
	// Identifier of the chat to send messages
	ChatID int64
	// Identifiers of the messages to resend. Message identifiers must be in a strictly
	// increasing order
	MessageIDs []int64
	// New manually chosen quote from the message to be replied; pass null if none. Ignored
	// if more than one message is re-sent, or if messageSendingStateFailed
	// need_another_reply_quote == false
	Quote InputTextQuote
	// The number of Telegram Stars the user agreed to pay to send the messages. Ignored if
	// messageSendingStateFailed.required_paid_message_star_count == 0
	PaidMessageStarCount int64
}

// ResendMessagesRequestTypeID is TL type id of ResendMessagesRequest.
const ResendMessagesRequestTypeID = 0x11f0d054

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

func (r *ResendMessagesRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.ChatID == 0) {
		return false
	}
	if !(r.MessageIDs == nil) {
		return false
	}
	if !(r.Quote.Zero()) {
		return false
	}
	if !(r.PaidMessageStarCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *ResendMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "resendMessages",
		ID:   ResendMessagesRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageIDs",
			SchemaName: "message_ids",
		},
		{
			Name:       "Quote",
			SchemaName: "quote",
		},
		{
			Name:       "PaidMessageStarCount",
			SchemaName: "paid_message_star_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ResendMessagesRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode resendMessages#11f0d054 as nil")
	}
	b.PutID(ResendMessagesRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ResendMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode resendMessages#11f0d054 as nil")
	}
	b.PutInt53(r.ChatID)
	b.PutInt(len(r.MessageIDs))
	for _, v := range r.MessageIDs {
		b.PutInt53(v)
	}
	if err := r.Quote.Encode(b); err != nil {
		return fmt.Errorf("unable to encode resendMessages#11f0d054: field quote: %w", err)
	}
	b.PutInt53(r.PaidMessageStarCount)
	return nil
}

// Decode implements bin.Decoder.
func (r *ResendMessagesRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode resendMessages#11f0d054 to nil")
	}
	if err := b.ConsumeID(ResendMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode resendMessages#11f0d054: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ResendMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode resendMessages#11f0d054 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode resendMessages#11f0d054: field chat_id: %w", err)
		}
		r.ChatID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode resendMessages#11f0d054: field message_ids: %w", err)
		}

		if headerLen > 0 {
			r.MessageIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode resendMessages#11f0d054: field message_ids: %w", err)
			}
			r.MessageIDs = append(r.MessageIDs, value)
		}
	}
	{
		if err := r.Quote.Decode(b); err != nil {
			return fmt.Errorf("unable to decode resendMessages#11f0d054: field quote: %w", err)
		}
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode resendMessages#11f0d054: field paid_message_star_count: %w", err)
		}
		r.PaidMessageStarCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *ResendMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode resendMessages#11f0d054 as nil")
	}
	b.ObjStart()
	b.PutID("resendMessages")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(r.ChatID)
	b.Comma()
	b.FieldStart("message_ids")
	b.ArrStart()
	for _, v := range r.MessageIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("quote")
	if err := r.Quote.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode resendMessages#11f0d054: field quote: %w", err)
	}
	b.Comma()
	b.FieldStart("paid_message_star_count")
	b.PutInt53(r.PaidMessageStarCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *ResendMessagesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode resendMessages#11f0d054 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("resendMessages"); err != nil {
				return fmt.Errorf("unable to decode resendMessages#11f0d054: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode resendMessages#11f0d054: field chat_id: %w", err)
			}
			r.ChatID = value
		case "message_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode resendMessages#11f0d054: field message_ids: %w", err)
				}
				r.MessageIDs = append(r.MessageIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode resendMessages#11f0d054: field message_ids: %w", err)
			}
		case "quote":
			if err := r.Quote.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode resendMessages#11f0d054: field quote: %w", err)
			}
		case "paid_message_star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode resendMessages#11f0d054: field paid_message_star_count: %w", err)
			}
			r.PaidMessageStarCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetMessageIDs returns value of MessageIDs field.
func (r *ResendMessagesRequest) GetMessageIDs() (value []int64) {
	if r == nil {
		return
	}
	return r.MessageIDs
}

// GetQuote returns value of Quote field.
func (r *ResendMessagesRequest) GetQuote() (value InputTextQuote) {
	if r == nil {
		return
	}
	return r.Quote
}

// GetPaidMessageStarCount returns value of PaidMessageStarCount field.
func (r *ResendMessagesRequest) GetPaidMessageStarCount() (value int64) {
	if r == nil {
		return
	}
	return r.PaidMessageStarCount
}

// ResendMessages invokes method resendMessages#11f0d054 returning error if any.
func (c *Client) ResendMessages(ctx context.Context, request *ResendMessagesRequest) (*Messages, error) {
	var result Messages

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