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

// MessagesGetForumTopicsRequest represents TL type `messages.getForumTopics#3ba47bff`.
//
// See https://core.telegram.org/method/messages.getForumTopics for reference.
type MessagesGetForumTopicsRequest struct {
	// Flags field of MessagesGetForumTopicsRequest.
	Flags bin.Fields
	// Peer field of MessagesGetForumTopicsRequest.
	Peer InputPeerClass
	// Q field of MessagesGetForumTopicsRequest.
	//
	// Use SetQ and GetQ helpers.
	Q string
	// OffsetDate field of MessagesGetForumTopicsRequest.
	OffsetDate int
	// OffsetID field of MessagesGetForumTopicsRequest.
	OffsetID int
	// OffsetTopic field of MessagesGetForumTopicsRequest.
	OffsetTopic int
	// Limit field of MessagesGetForumTopicsRequest.
	Limit int
}

// MessagesGetForumTopicsRequestTypeID is TL type id of MessagesGetForumTopicsRequest.
const MessagesGetForumTopicsRequestTypeID = 0x3ba47bff

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

func (g *MessagesGetForumTopicsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Q == "") {
		return false
	}
	if !(g.OffsetDate == 0) {
		return false
	}
	if !(g.OffsetID == 0) {
		return false
	}
	if !(g.OffsetTopic == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetForumTopicsRequest from given interface.
func (g *MessagesGetForumTopicsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetQ() (value string, ok bool)
	GetOffsetDate() (value int)
	GetOffsetID() (value int)
	GetOffsetTopic() (value int)
	GetLimit() (value int)
}) {
	g.Peer = from.GetPeer()
	if val, ok := from.GetQ(); ok {
		g.Q = val
	}

	g.OffsetDate = from.GetOffsetDate()
	g.OffsetID = from.GetOffsetID()
	g.OffsetTopic = from.GetOffsetTopic()
	g.Limit = from.GetLimit()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetForumTopicsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getForumTopics",
		ID:   MessagesGetForumTopicsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Q",
			SchemaName: "q",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "OffsetDate",
			SchemaName: "offset_date",
		},
		{
			Name:       "OffsetID",
			SchemaName: "offset_id",
		},
		{
			Name:       "OffsetTopic",
			SchemaName: "offset_topic",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *MessagesGetForumTopicsRequest) SetFlags() {
	if !(g.Q == "") {
		g.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (g *MessagesGetForumTopicsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getForumTopics#3ba47bff as nil")
	}
	b.PutID(MessagesGetForumTopicsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetForumTopicsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getForumTopics#3ba47bff as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getForumTopics#3ba47bff: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode messages.getForumTopics#3ba47bff: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getForumTopics#3ba47bff: field peer: %w", err)
	}
	if g.Flags.Has(0) {
		b.PutString(g.Q)
	}
	b.PutInt(g.OffsetDate)
	b.PutInt(g.OffsetID)
	b.PutInt(g.OffsetTopic)
	b.PutInt(g.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetForumTopicsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getForumTopics#3ba47bff to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopics#3ba47bff: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopics#3ba47bff: field peer: %w", err)
		}
		g.Peer = value
	}
	if g.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopics#3ba47bff: field q: %w", err)
		}
		g.Q = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopics#3ba47bff: field offset_date: %w", err)
		}
		g.OffsetDate = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopics#3ba47bff: field offset_id: %w", err)
		}
		g.OffsetID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopics#3ba47bff: field offset_topic: %w", err)
		}
		g.OffsetTopic = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getForumTopics#3ba47bff: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

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

// SetQ sets value of Q conditional field.
func (g *MessagesGetForumTopicsRequest) SetQ(value string) {
	g.Flags.Set(0)
	g.Q = value
}

// GetQ returns value of Q conditional field and
// boolean which is true if field was set.
func (g *MessagesGetForumTopicsRequest) GetQ() (value string, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(0) {
		return value, false
	}
	return g.Q, true
}

// GetOffsetDate returns value of OffsetDate field.
func (g *MessagesGetForumTopicsRequest) GetOffsetDate() (value int) {
	if g == nil {
		return
	}
	return g.OffsetDate
}

// GetOffsetID returns value of OffsetID field.
func (g *MessagesGetForumTopicsRequest) GetOffsetID() (value int) {
	if g == nil {
		return
	}
	return g.OffsetID
}

// GetOffsetTopic returns value of OffsetTopic field.
func (g *MessagesGetForumTopicsRequest) GetOffsetTopic() (value int) {
	if g == nil {
		return
	}
	return g.OffsetTopic
}

// GetLimit returns value of Limit field.
func (g *MessagesGetForumTopicsRequest) GetLimit() (value int) {
	if g == nil {
		return
	}
	return g.Limit
}

// MessagesGetForumTopics invokes method messages.getForumTopics#3ba47bff returning error if any.
//
// See https://core.telegram.org/method/messages.getForumTopics for reference.
func (c *Client) MessagesGetForumTopics(ctx context.Context, request *MessagesGetForumTopicsRequest) (*MessagesForumTopics, error) {
	var result MessagesForumTopics

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