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

// MessagesGetSavedDialogsByIDRequest represents TL type `messages.getSavedDialogsByID#6f6f9c96`.
// Obtain information about specific saved message dialogs »¹ or monoforum topics »².
//
// Links:
//  1. https://core.telegram.org/api/saved-messages#saved-message-dialogs
//  2. https://core.telegram.org/api/monoforum
//
// See https://core.telegram.org/method/messages.getSavedDialogsByID for reference.
type MessagesGetSavedDialogsByIDRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, fetches monoforum topics »¹, otherwise fetches saved message dialogs »².
	//
	// Links:
	//  1) https://core.telegram.org/api/monoforum
	//  2) https://core.telegram.org/api/saved-messages#saved-message-dialogs
	//
	// Use SetParentPeer and GetParentPeer helpers.
	ParentPeer InputPeerClass
	// IDs of dialogs (topics) to fetch.
	IDs []InputPeerClass
}

// MessagesGetSavedDialogsByIDRequestTypeID is TL type id of MessagesGetSavedDialogsByIDRequest.
const MessagesGetSavedDialogsByIDRequestTypeID = 0x6f6f9c96

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

func (g *MessagesGetSavedDialogsByIDRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.ParentPeer == nil) {
		return false
	}
	if !(g.IDs == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetSavedDialogsByIDRequest from given interface.
func (g *MessagesGetSavedDialogsByIDRequest) FillFrom(from interface {
	GetParentPeer() (value InputPeerClass, ok bool)
	GetIDs() (value []InputPeerClass)
}) {
	if val, ok := from.GetParentPeer(); ok {
		g.ParentPeer = val
	}

	g.IDs = from.GetIDs()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetSavedDialogsByIDRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getSavedDialogsByID",
		ID:   MessagesGetSavedDialogsByIDRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ParentPeer",
			SchemaName: "parent_peer",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "IDs",
			SchemaName: "ids",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *MessagesGetSavedDialogsByIDRequest) SetFlags() {
	if !(g.ParentPeer == nil) {
		g.Flags.Set(1)
	}
}

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

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetSavedDialogsByIDRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSavedDialogsByID#6f6f9c96 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSavedDialogsByID#6f6f9c96: field flags: %w", err)
	}
	if g.Flags.Has(1) {
		if g.ParentPeer == nil {
			return fmt.Errorf("unable to encode messages.getSavedDialogsByID#6f6f9c96: field parent_peer is nil")
		}
		if err := g.ParentPeer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.getSavedDialogsByID#6f6f9c96: field parent_peer: %w", err)
		}
	}
	b.PutVectorHeader(len(g.IDs))
	for idx, v := range g.IDs {
		if v == nil {
			return fmt.Errorf("unable to encode messages.getSavedDialogsByID#6f6f9c96: field ids element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.getSavedDialogsByID#6f6f9c96: field ids element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetSavedDialogsByIDRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getSavedDialogsByID#6f6f9c96 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogsByID#6f6f9c96: field flags: %w", err)
		}
	}
	if g.Flags.Has(1) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogsByID#6f6f9c96: field parent_peer: %w", err)
		}
		g.ParentPeer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogsByID#6f6f9c96: field ids: %w", err)
		}

		if headerLen > 0 {
			g.IDs = make([]InputPeerClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputPeer(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.getSavedDialogsByID#6f6f9c96: field ids: %w", err)
			}
			g.IDs = append(g.IDs, value)
		}
	}
	return nil
}

// SetParentPeer sets value of ParentPeer conditional field.
func (g *MessagesGetSavedDialogsByIDRequest) SetParentPeer(value InputPeerClass) {
	g.Flags.Set(1)
	g.ParentPeer = value
}

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

// GetIDs returns value of IDs field.
func (g *MessagesGetSavedDialogsByIDRequest) GetIDs() (value []InputPeerClass) {
	if g == nil {
		return
	}
	return g.IDs
}

// MapIDs returns field IDs wrapped in InputPeerClassArray helper.
func (g *MessagesGetSavedDialogsByIDRequest) MapIDs() (value InputPeerClassArray) {
	return InputPeerClassArray(g.IDs)
}

// MessagesGetSavedDialogsByID invokes method messages.getSavedDialogsByID#6f6f9c96 returning error if any.
// Obtain information about specific saved message dialogs »¹ or monoforum topics »².
//
// Links:
//  1. https://core.telegram.org/api/saved-messages#saved-message-dialogs
//  2. https://core.telegram.org/api/monoforum
//
// See https://core.telegram.org/method/messages.getSavedDialogsByID for reference.
func (c *Client) MessagesGetSavedDialogsByID(ctx context.Context, request *MessagesGetSavedDialogsByIDRequest) (MessagesSavedDialogsClass, error) {
	var result MessagesSavedDialogsBox

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