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

// GetDirectMessagesChatTopicHistoryRequest represents TL type `getDirectMessagesChatTopicHistory#3db438c4`.
type GetDirectMessagesChatTopicHistoryRequest struct {
	// Chat identifier of the channel direct messages chat
	ChatID int64
	// Identifier of the topic which messages will be fetched
	TopicID int64
	// Identifier of the message starting from which messages must be fetched; use 0 to get
	// results from the last message
	FromMessageID int64
	// Specify 0 to get results from exactly the message from_message_id or a negative offset
	// up to 99 to get additionally some newer messages
	Offset int32
	// The maximum number of messages to be returned; must be positive and can't be greater
	// than 100. If the offset is negative, the limit must be greater than or equal to
	// -offset.
	Limit int32
}

// GetDirectMessagesChatTopicHistoryRequestTypeID is TL type id of GetDirectMessagesChatTopicHistoryRequest.
const GetDirectMessagesChatTopicHistoryRequestTypeID = 0x3db438c4

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

func (g *GetDirectMessagesChatTopicHistoryRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatID == 0) {
		return false
	}
	if !(g.TopicID == 0) {
		return false
	}
	if !(g.FromMessageID == 0) {
		return false
	}
	if !(g.Offset == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetDirectMessagesChatTopicHistoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getDirectMessagesChatTopicHistory",
		ID:   GetDirectMessagesChatTopicHistoryRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "TopicID",
			SchemaName: "topic_id",
		},
		{
			Name:       "FromMessageID",
			SchemaName: "from_message_id",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetDirectMessagesChatTopicHistoryRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getDirectMessagesChatTopicHistory#3db438c4 as nil")
	}
	b.PutID(GetDirectMessagesChatTopicHistoryRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetDirectMessagesChatTopicHistoryRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getDirectMessagesChatTopicHistory#3db438c4 as nil")
	}
	b.PutInt53(g.ChatID)
	b.PutInt53(g.TopicID)
	b.PutInt53(g.FromMessageID)
	b.PutInt32(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetDirectMessagesChatTopicHistoryRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getDirectMessagesChatTopicHistory#3db438c4 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field chat_id: %w", err)
		}
		g.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field topic_id: %w", err)
		}
		g.TopicID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field from_message_id: %w", err)
		}
		g.FromMessageID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetDirectMessagesChatTopicHistoryRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getDirectMessagesChatTopicHistory#3db438c4 as nil")
	}
	b.ObjStart()
	b.PutID("getDirectMessagesChatTopicHistory")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(g.ChatID)
	b.Comma()
	b.FieldStart("topic_id")
	b.PutInt53(g.TopicID)
	b.Comma()
	b.FieldStart("from_message_id")
	b.PutInt53(g.FromMessageID)
	b.Comma()
	b.FieldStart("offset")
	b.PutInt32(g.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getDirectMessagesChatTopicHistory"); err != nil {
				return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field chat_id: %w", err)
			}
			g.ChatID = value
		case "topic_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field topic_id: %w", err)
			}
			g.TopicID = value
		case "from_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field from_message_id: %w", err)
			}
			g.FromMessageID = value
		case "offset":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getDirectMessagesChatTopicHistory#3db438c4: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetTopicID returns value of TopicID field.
func (g *GetDirectMessagesChatTopicHistoryRequest) GetTopicID() (value int64) {
	if g == nil {
		return
	}
	return g.TopicID
}

// GetFromMessageID returns value of FromMessageID field.
func (g *GetDirectMessagesChatTopicHistoryRequest) GetFromMessageID() (value int64) {
	if g == nil {
		return
	}
	return g.FromMessageID
}

// GetOffset returns value of Offset field.
func (g *GetDirectMessagesChatTopicHistoryRequest) GetOffset() (value int32) {
	if g == nil {
		return
	}
	return g.Offset
}

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

// GetDirectMessagesChatTopicHistory invokes method getDirectMessagesChatTopicHistory#3db438c4 returning error if any.
func (c *Client) GetDirectMessagesChatTopicHistory(ctx context.Context, request *GetDirectMessagesChatTopicHistoryRequest) (*Messages, error) {
	var result Messages

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