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

// MessagesGetSearchResultsCalendarRequest represents TL type `messages.getSearchResultsCalendar#6aa3f6bd`.
// Returns information about the next messages of the specified type in the chat split by
// days.
// Returns the results in reverse chronological order.
// Can return partial results for the last returned day.
//
// See https://core.telegram.org/method/messages.getSearchResultsCalendar for reference.
type MessagesGetSearchResultsCalendarRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Peer where to search
	Peer InputPeerClass
	// Search within the saved message dialog »¹ with this ID.
	//
	// Links:
	//  1) https://core.telegram.org/api/saved-messages
	//
	// Use SetSavedPeerID and GetSavedPeerID helpers.
	SavedPeerID InputPeerClass
	// Message filter, inputMessagesFilterEmpty¹, inputMessagesFilterMyMentions² filters
	// are not supported by this method.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/inputMessagesFilterEmpty
	//  2) https://core.telegram.org/constructor/inputMessagesFilterMyMentions
	Filter MessagesFilterClass
	// Offsets for pagination, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	OffsetID int
	// Offsets for pagination, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	OffsetDate int
}

// MessagesGetSearchResultsCalendarRequestTypeID is TL type id of MessagesGetSearchResultsCalendarRequest.
const MessagesGetSearchResultsCalendarRequestTypeID = 0x6aa3f6bd

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

func (g *MessagesGetSearchResultsCalendarRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.SavedPeerID == nil) {
		return false
	}
	if !(g.Filter == nil) {
		return false
	}
	if !(g.OffsetID == 0) {
		return false
	}
	if !(g.OffsetDate == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetSearchResultsCalendarRequest from given interface.
func (g *MessagesGetSearchResultsCalendarRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetSavedPeerID() (value InputPeerClass, ok bool)
	GetFilter() (value MessagesFilterClass)
	GetOffsetID() (value int)
	GetOffsetDate() (value int)
}) {
	g.Peer = from.GetPeer()
	if val, ok := from.GetSavedPeerID(); ok {
		g.SavedPeerID = val
	}

	g.Filter = from.GetFilter()
	g.OffsetID = from.GetOffsetID()
	g.OffsetDate = from.GetOffsetDate()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetSearchResultsCalendarRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getSearchResultsCalendar",
		ID:   MessagesGetSearchResultsCalendarRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "SavedPeerID",
			SchemaName: "saved_peer_id",
			Null:       !g.Flags.Has(2),
		},
		{
			Name:       "Filter",
			SchemaName: "filter",
		},
		{
			Name:       "OffsetID",
			SchemaName: "offset_id",
		},
		{
			Name:       "OffsetDate",
			SchemaName: "offset_date",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *MessagesGetSearchResultsCalendarRequest) SetFlags() {
	if !(g.SavedPeerID == nil) {
		g.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (g *MessagesGetSearchResultsCalendarRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSearchResultsCalendar#6aa3f6bd as nil")
	}
	b.PutID(MessagesGetSearchResultsCalendarRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetSearchResultsCalendarRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSearchResultsCalendar#6aa3f6bd as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSearchResultsCalendar#6aa3f6bd: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode messages.getSearchResultsCalendar#6aa3f6bd: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSearchResultsCalendar#6aa3f6bd: field peer: %w", err)
	}
	if g.Flags.Has(2) {
		if g.SavedPeerID == nil {
			return fmt.Errorf("unable to encode messages.getSearchResultsCalendar#6aa3f6bd: field saved_peer_id is nil")
		}
		if err := g.SavedPeerID.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.getSearchResultsCalendar#6aa3f6bd: field saved_peer_id: %w", err)
		}
	}
	if g.Filter == nil {
		return fmt.Errorf("unable to encode messages.getSearchResultsCalendar#6aa3f6bd: field filter is nil")
	}
	if err := g.Filter.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSearchResultsCalendar#6aa3f6bd: field filter: %w", err)
	}
	b.PutInt(g.OffsetID)
	b.PutInt(g.OffsetDate)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetSearchResultsCalendarRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getSearchResultsCalendar#6aa3f6bd to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.getSearchResultsCalendar#6aa3f6bd: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSearchResultsCalendar#6aa3f6bd: field peer: %w", err)
		}
		g.Peer = value
	}
	if g.Flags.Has(2) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSearchResultsCalendar#6aa3f6bd: field saved_peer_id: %w", err)
		}
		g.SavedPeerID = value
	}
	{
		value, err := DecodeMessagesFilter(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSearchResultsCalendar#6aa3f6bd: field filter: %w", err)
		}
		g.Filter = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSearchResultsCalendar#6aa3f6bd: field offset_id: %w", err)
		}
		g.OffsetID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSearchResultsCalendar#6aa3f6bd: field offset_date: %w", err)
		}
		g.OffsetDate = value
	}
	return nil
}

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

// SetSavedPeerID sets value of SavedPeerID conditional field.
func (g *MessagesGetSearchResultsCalendarRequest) SetSavedPeerID(value InputPeerClass) {
	g.Flags.Set(2)
	g.SavedPeerID = value
}

// GetSavedPeerID returns value of SavedPeerID conditional field and
// boolean which is true if field was set.
func (g *MessagesGetSearchResultsCalendarRequest) GetSavedPeerID() (value InputPeerClass, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(2) {
		return value, false
	}
	return g.SavedPeerID, true
}

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

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

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

// MessagesGetSearchResultsCalendar invokes method messages.getSearchResultsCalendar#6aa3f6bd returning error if any.
// Returns information about the next messages of the specified type in the chat split by
// days.
// Returns the results in reverse chronological order.
// Can return partial results for the last returned day.
//
// Possible errors:
//
//	400 FILTER_NOT_SUPPORTED: The specified filter cannot be used in this context.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/messages.getSearchResultsCalendar for reference.
func (c *Client) MessagesGetSearchResultsCalendar(ctx context.Context, request *MessagesGetSearchResultsCalendarRequest) (*MessagesSearchResultsCalendar, error) {
	var result MessagesSearchResultsCalendar

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