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

// MessagesGetForumTopicsByIDRequest represents TL type `messages.getForumTopicsByID#af0a4a08`.
//
// See https://core.telegram.org/method/messages.getForumTopicsByID for reference.
type MessagesGetForumTopicsByIDRequest struct {
	// Peer field of MessagesGetForumTopicsByIDRequest.
	Peer InputPeerClass
	// Topics field of MessagesGetForumTopicsByIDRequest.
	Topics []int
}

// MessagesGetForumTopicsByIDRequestTypeID is TL type id of MessagesGetForumTopicsByIDRequest.
const MessagesGetForumTopicsByIDRequestTypeID = 0xaf0a4a08

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

func (g *MessagesGetForumTopicsByIDRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Topics == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetForumTopicsByIDRequest from given interface.
func (g *MessagesGetForumTopicsByIDRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetTopics() (value []int)
}) {
	g.Peer = from.GetPeer()
	g.Topics = from.GetTopics()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetForumTopicsByIDRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getForumTopicsByID",
		ID:   MessagesGetForumTopicsByIDRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Topics",
			SchemaName: "topics",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *MessagesGetForumTopicsByIDRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getForumTopicsByID#af0a4a08 as nil")
	}
	b.PutID(MessagesGetForumTopicsByIDRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetForumTopicsByIDRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getForumTopicsByID#af0a4a08 as nil")
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode messages.getForumTopicsByID#af0a4a08: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getForumTopicsByID#af0a4a08: field peer: %w", err)
	}
	b.PutVectorHeader(len(g.Topics))
	for _, v := range g.Topics {
		b.PutInt(v)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetForumTopicsByIDRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getForumTopicsByID#af0a4a08 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopicsByID#af0a4a08: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopicsByID#af0a4a08: field topics: %w", err)
		}

		if headerLen > 0 {
			g.Topics = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode messages.getForumTopicsByID#af0a4a08: field topics: %w", err)
			}
			g.Topics = append(g.Topics, value)
		}
	}
	return nil
}

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

// GetTopics returns value of Topics field.
func (g *MessagesGetForumTopicsByIDRequest) GetTopics() (value []int) {
	if g == nil {
		return
	}
	return g.Topics
}

// MessagesGetForumTopicsByID invokes method messages.getForumTopicsByID#af0a4a08 returning error if any.
//
// See https://core.telegram.org/method/messages.getForumTopicsByID for reference.
func (c *Client) MessagesGetForumTopicsByID(ctx context.Context, request *MessagesGetForumTopicsByIDRequest) (*MessagesForumTopics, error) {
	var result MessagesForumTopics

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