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

// GetForumTopicsRequest represents TL type `getForumTopics#fbab7d5a`.
type GetForumTopicsRequest struct {
	// Identifier of the forum chat
	ChatID int64
	// Query to search for in the forum topic's name
	Query string
	// The date starting from which the results need to be fetched. Use 0 or any date in the
	// future to get results from the last topic
	OffsetDate int32
	// The message identifier of the last message in the last found topic, or 0 for the first
	// request
	OffsetMessageID int64
	// The message thread identifier of the last found topic, or 0 for the first request
	OffsetMessageThreadID int64
	// The maximum number of forum topics to be returned; up to 100. For optimal performance,
	// the number of returned forum topics is chosen by TDLib and can be smaller than the
	// specified limit
	Limit int32
}

// GetForumTopicsRequestTypeID is TL type id of GetForumTopicsRequest.
const GetForumTopicsRequestTypeID = 0xfbab7d5a

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

func (g *GetForumTopicsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatID == 0) {
		return false
	}
	if !(g.Query == "") {
		return false
	}
	if !(g.OffsetDate == 0) {
		return false
	}
	if !(g.OffsetMessageID == 0) {
		return false
	}
	if !(g.OffsetMessageThreadID == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetForumTopicsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getForumTopics",
		ID:   GetForumTopicsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "OffsetDate",
			SchemaName: "offset_date",
		},
		{
			Name:       "OffsetMessageID",
			SchemaName: "offset_message_id",
		},
		{
			Name:       "OffsetMessageThreadID",
			SchemaName: "offset_message_thread_id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

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

// EncodeBare implements bin.BareEncoder.
func (g *GetForumTopicsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getForumTopics#fbab7d5a as nil")
	}
	b.PutInt53(g.ChatID)
	b.PutString(g.Query)
	b.PutInt32(g.OffsetDate)
	b.PutInt53(g.OffsetMessageID)
	b.PutInt53(g.OffsetMessageThreadID)
	b.PutInt32(g.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetForumTopicsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getForumTopics#fbab7d5a to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field chat_id: %w", err)
		}
		g.ChatID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field query: %w", err)
		}
		g.Query = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field offset_date: %w", err)
		}
		g.OffsetDate = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field offset_message_id: %w", err)
		}
		g.OffsetMessageID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field offset_message_thread_id: %w", err)
		}
		g.OffsetMessageThreadID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetForumTopicsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getForumTopics#fbab7d5a as nil")
	}
	b.ObjStart()
	b.PutID("getForumTopics")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(g.ChatID)
	b.Comma()
	b.FieldStart("query")
	b.PutString(g.Query)
	b.Comma()
	b.FieldStart("offset_date")
	b.PutInt32(g.OffsetDate)
	b.Comma()
	b.FieldStart("offset_message_id")
	b.PutInt53(g.OffsetMessageID)
	b.Comma()
	b.FieldStart("offset_message_thread_id")
	b.PutInt53(g.OffsetMessageThreadID)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetForumTopicsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getForumTopics#fbab7d5a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getForumTopics"); err != nil {
				return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field chat_id: %w", err)
			}
			g.ChatID = value
		case "query":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field query: %w", err)
			}
			g.Query = value
		case "offset_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field offset_date: %w", err)
			}
			g.OffsetDate = value
		case "offset_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field offset_message_id: %w", err)
			}
			g.OffsetMessageID = value
		case "offset_message_thread_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field offset_message_thread_id: %w", err)
			}
			g.OffsetMessageThreadID = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getForumTopics#fbab7d5a: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetQuery returns value of Query field.
func (g *GetForumTopicsRequest) GetQuery() (value string) {
	if g == nil {
		return
	}
	return g.Query
}

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

// GetOffsetMessageID returns value of OffsetMessageID field.
func (g *GetForumTopicsRequest) GetOffsetMessageID() (value int64) {
	if g == nil {
		return
	}
	return g.OffsetMessageID
}

// GetOffsetMessageThreadID returns value of OffsetMessageThreadID field.
func (g *GetForumTopicsRequest) GetOffsetMessageThreadID() (value int64) {
	if g == nil {
		return
	}
	return g.OffsetMessageThreadID
}

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

// GetForumTopics invokes method getForumTopics#fbab7d5a returning error if any.
func (c *Client) GetForumTopics(ctx context.Context, request *GetForumTopicsRequest) (*ForumTopics, error) {
	var result ForumTopics

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