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

// GetForumTopicRequest represents TL type `getForumTopic#e59bfe41`.
type GetForumTopicRequest struct {
	// Identifier of the chat
	ChatID int64
	// Message thread identifier of the forum topic
	MessageThreadID int64
}

// GetForumTopicRequestTypeID is TL type id of GetForumTopicRequest.
const GetForumTopicRequestTypeID = 0xe59bfe41

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

func (g *GetForumTopicRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatID == 0) {
		return false
	}
	if !(g.MessageThreadID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetForumTopicRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getForumTopic",
		ID:   GetForumTopicRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageThreadID",
			SchemaName: "message_thread_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetForumTopicRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getForumTopic#e59bfe41 as nil")
	}
	b.PutID(GetForumTopicRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetForumTopicRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getForumTopic#e59bfe41 as nil")
	}
	b.PutInt53(g.ChatID)
	b.PutInt53(g.MessageThreadID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetForumTopicRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getForumTopic#e59bfe41 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getForumTopic#e59bfe41: field chat_id: %w", err)
		}
		g.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getForumTopic#e59bfe41: field message_thread_id: %w", err)
		}
		g.MessageThreadID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetForumTopicRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getForumTopic#e59bfe41 as nil")
	}
	b.ObjStart()
	b.PutID("getForumTopic")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(g.ChatID)
	b.Comma()
	b.FieldStart("message_thread_id")
	b.PutInt53(g.MessageThreadID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getForumTopic"); err != nil {
				return fmt.Errorf("unable to decode getForumTopic#e59bfe41: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getForumTopic#e59bfe41: field chat_id: %w", err)
			}
			g.ChatID = value
		case "message_thread_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getForumTopic#e59bfe41: field message_thread_id: %w", err)
			}
			g.MessageThreadID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetMessageThreadID returns value of MessageThreadID field.
func (g *GetForumTopicRequest) GetMessageThreadID() (value int64) {
	if g == nil {
		return
	}
	return g.MessageThreadID
}

// GetForumTopic invokes method getForumTopic#e59bfe41 returning error if any.
func (c *Client) GetForumTopic(ctx context.Context, request *GetForumTopicRequest) (*ForumTopic, error) {
	var result ForumTopic

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