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

// MessagesGetMessagesRequest represents TL type `messages.getMessages#63c66506`.
// Returns the list of messages by their IDs.
//
// See https://core.telegram.org/method/messages.getMessages for reference.
type MessagesGetMessagesRequest struct {
	// Message ID list
	ID []InputMessageClass
}

// MessagesGetMessagesRequestTypeID is TL type id of MessagesGetMessagesRequest.
const MessagesGetMessagesRequestTypeID = 0x63c66506

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

func (g *MessagesGetMessagesRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetMessagesRequest from given interface.
func (g *MessagesGetMessagesRequest) FillFrom(from interface {
	GetID() (value []InputMessageClass)
}) {
	g.ID = from.GetID()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getMessages",
		ID:   MessagesGetMessagesRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *MessagesGetMessagesRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getMessages#63c66506 as nil")
	}
	b.PutID(MessagesGetMessagesRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getMessages#63c66506 as nil")
	}
	b.PutVectorHeader(len(g.ID))
	for idx, v := range g.ID {
		if v == nil {
			return fmt.Errorf("unable to encode messages.getMessages#63c66506: field id element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.getMessages#63c66506: field id element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *MessagesGetMessagesRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getMessages#63c66506 to nil")
	}
	if err := b.ConsumeID(MessagesGetMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.getMessages#63c66506: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getMessages#63c66506 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getMessages#63c66506: field id: %w", err)
		}

		if headerLen > 0 {
			g.ID = make([]InputMessageClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputMessage(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.getMessages#63c66506: field id: %w", err)
			}
			g.ID = append(g.ID, value)
		}
	}
	return nil
}

// GetID returns value of ID field.
func (g *MessagesGetMessagesRequest) GetID() (value []InputMessageClass) {
	if g == nil {
		return
	}
	return g.ID
}

// MapID returns field ID wrapped in InputMessageClassArray helper.
func (g *MessagesGetMessagesRequest) MapID() (value InputMessageClassArray) {
	return InputMessageClassArray(g.ID)
}

// MessagesGetMessages invokes method messages.getMessages#63c66506 returning error if any.
// Returns the list of messages by their IDs.
//
// See https://core.telegram.org/method/messages.getMessages for reference.
func (c *Client) MessagesGetMessages(ctx context.Context, id []InputMessageClass) (MessagesMessagesClass, error) {
	var result MessagesMessagesBox

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