// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// ChannelsGetForumTopicsByIDRequest represents TL type `channels.getForumTopicsByID#b0831eb9`.
// Get forum topics by their ID
//
// See https://core.telegram.org/method/channels.getForumTopicsByID for reference.
type ChannelsGetForumTopicsByIDRequest struct {
	// Forum
	Channel InputChannelClass
	// Topic IDs
	Topics []int
}

// ChannelsGetForumTopicsByIDRequestTypeID is TL type id of ChannelsGetForumTopicsByIDRequest.
const ChannelsGetForumTopicsByIDRequestTypeID = 0xb0831eb9

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

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

	return true
}

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

// FillFrom fills ChannelsGetForumTopicsByIDRequest from given interface.
func (g *ChannelsGetForumTopicsByIDRequest) FillFrom(from interface {
	GetChannel() (value InputChannelClass)
	GetTopics() (value []int)
}) {
	g.Channel = from.GetChannel()
	g.Topics = from.GetTopics()
}

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

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

// TypeInfo returns info about TL type.
func (g *ChannelsGetForumTopicsByIDRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.getForumTopicsByID",
		ID:   ChannelsGetForumTopicsByIDRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
		{
			Name:       "Topics",
			SchemaName: "topics",
		},
	}
	return typ
}

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

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

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

// DecodeBare implements bin.BareDecoder.
func (g *ChannelsGetForumTopicsByIDRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode channels.getForumTopicsByID#b0831eb9 to nil")
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.getForumTopicsByID#b0831eb9: field channel: %w", err)
		}
		g.Channel = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode channels.getForumTopicsByID#b0831eb9: 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 channels.getForumTopicsByID#b0831eb9: field topics: %w", err)
			}
			g.Topics = append(g.Topics, value)
		}
	}
	return nil
}

// GetChannel returns value of Channel field.
func (g *ChannelsGetForumTopicsByIDRequest) GetChannel() (value InputChannelClass) {
	if g == nil {
		return
	}
	return g.Channel
}

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

// GetChannelAsNotEmpty returns mapped value of Channel field.
func (g *ChannelsGetForumTopicsByIDRequest) GetChannelAsNotEmpty() (NotEmptyInputChannel, bool) {
	return g.Channel.AsNotEmpty()
}

// ChannelsGetForumTopicsByID invokes method channels.getForumTopicsByID#b0831eb9 returning error if any.
// Get forum topics by their ID
//
// See https://core.telegram.org/method/channels.getForumTopicsByID for reference.
// Can be used by bots.
func (c *Client) ChannelsGetForumTopicsByID(ctx context.Context, request *ChannelsGetForumTopicsByIDRequest) (*MessagesForumTopics, error) {
	var result MessagesForumTopics

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