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

// GetChatMessageCalendarRequest represents TL type `getChatMessageCalendar#61fd785c`.
type GetChatMessageCalendarRequest struct {
	// Identifier of the chat in which to return information about messages
	ChatID int64
	// Pass topic identifier to get the result only in specific topic; pass null to get the
	// result in all topics; forum topics aren't supported
	TopicID MessageTopicClass
	// Filter for message content. Filters searchMessagesFilterEmpty,
	// searchMessagesFilterMention, searchMessagesFilterUnreadMention, and
	// searchMessagesFilterUnreadReaction are unsupported in this function
	Filter SearchMessagesFilterClass
	// The message identifier from which to return information about messages; use 0 to get
	// results from the last message
	FromMessageID int64
}

// GetChatMessageCalendarRequestTypeID is TL type id of GetChatMessageCalendarRequest.
const GetChatMessageCalendarRequestTypeID = 0x61fd785c

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatMessageCalendarRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChatMessageCalendar",
		ID:   GetChatMessageCalendarRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "TopicID",
			SchemaName: "topic_id",
		},
		{
			Name:       "Filter",
			SchemaName: "filter",
		},
		{
			Name:       "FromMessageID",
			SchemaName: "from_message_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetChatMessageCalendarRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatMessageCalendar#61fd785c as nil")
	}
	b.PutID(GetChatMessageCalendarRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetChatMessageCalendarRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatMessageCalendar#61fd785c as nil")
	}
	b.PutInt53(g.ChatID)
	if g.TopicID == nil {
		return fmt.Errorf("unable to encode getChatMessageCalendar#61fd785c: field topic_id is nil")
	}
	if err := g.TopicID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getChatMessageCalendar#61fd785c: field topic_id: %w", err)
	}
	if g.Filter == nil {
		return fmt.Errorf("unable to encode getChatMessageCalendar#61fd785c: field filter is nil")
	}
	if err := g.Filter.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getChatMessageCalendar#61fd785c: field filter: %w", err)
	}
	b.PutInt53(g.FromMessageID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetChatMessageCalendarRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatMessageCalendar#61fd785c to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: field chat_id: %w", err)
		}
		g.ChatID = value
	}
	{
		value, err := DecodeMessageTopic(b)
		if err != nil {
			return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: field topic_id: %w", err)
		}
		g.TopicID = value
	}
	{
		value, err := DecodeSearchMessagesFilter(b)
		if err != nil {
			return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: field filter: %w", err)
		}
		g.Filter = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: field from_message_id: %w", err)
		}
		g.FromMessageID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetChatMessageCalendarRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatMessageCalendar#61fd785c as nil")
	}
	b.ObjStart()
	b.PutID("getChatMessageCalendar")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(g.ChatID)
	b.Comma()
	b.FieldStart("topic_id")
	if g.TopicID == nil {
		return fmt.Errorf("unable to encode getChatMessageCalendar#61fd785c: field topic_id is nil")
	}
	if err := g.TopicID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getChatMessageCalendar#61fd785c: field topic_id: %w", err)
	}
	b.Comma()
	b.FieldStart("filter")
	if g.Filter == nil {
		return fmt.Errorf("unable to encode getChatMessageCalendar#61fd785c: field filter is nil")
	}
	if err := g.Filter.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getChatMessageCalendar#61fd785c: field filter: %w", err)
	}
	b.Comma()
	b.FieldStart("from_message_id")
	b.PutInt53(g.FromMessageID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getChatMessageCalendar"); err != nil {
				return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: field chat_id: %w", err)
			}
			g.ChatID = value
		case "topic_id":
			value, err := DecodeTDLibJSONMessageTopic(b)
			if err != nil {
				return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: field topic_id: %w", err)
			}
			g.TopicID = value
		case "filter":
			value, err := DecodeTDLibJSONSearchMessagesFilter(b)
			if err != nil {
				return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: field filter: %w", err)
			}
			g.Filter = value
		case "from_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getChatMessageCalendar#61fd785c: field from_message_id: %w", err)
			}
			g.FromMessageID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// GetFilter returns value of Filter field.
func (g *GetChatMessageCalendarRequest) GetFilter() (value SearchMessagesFilterClass) {
	if g == nil {
		return
	}
	return g.Filter
}

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

// GetChatMessageCalendar invokes method getChatMessageCalendar#61fd785c returning error if any.
func (c *Client) GetChatMessageCalendar(ctx context.Context, request *GetChatMessageCalendarRequest) (*MessageCalendar, error) {
	var result MessageCalendar

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