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

// MessagesGetSavedDialogsRequest represents TL type `messages.getSavedDialogs#1e91fc99`.
// Returns the current saved dialog list »¹ or monoforum topic list »².
//
// Links:
//  1. https://core.telegram.org/api/saved-messages
//  2. https://core.telegram.org/api/monoforum
//
// See https://core.telegram.org/method/messages.getSavedDialogs for reference.
type MessagesGetSavedDialogsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Exclude pinned dialogs
	ExcludePinned bool
	// If set, fetches the topic list of the passed monoforum, otherwise fetches the saved
	// dialog list.
	//
	// Use SetParentPeer and GetParentPeer helpers.
	ParentPeer InputPeerClass
	// Offsets for pagination, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	OffsetDate int
	// Offsets for pagination, for more info click here¹ (top_message ID used for
	// pagination)
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	OffsetID int
	// Offset peer for pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	OffsetPeer InputPeerClass
	// Number of list elements to be returned
	Limit int
	// Hash used for caching, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int64
}

// MessagesGetSavedDialogsRequestTypeID is TL type id of MessagesGetSavedDialogsRequest.
const MessagesGetSavedDialogsRequestTypeID = 0x1e91fc99

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

func (g *MessagesGetSavedDialogsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.ExcludePinned == false) {
		return false
	}
	if !(g.ParentPeer == nil) {
		return false
	}
	if !(g.OffsetDate == 0) {
		return false
	}
	if !(g.OffsetID == 0) {
		return false
	}
	if !(g.OffsetPeer == nil) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}
	if !(g.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetSavedDialogsRequest from given interface.
func (g *MessagesGetSavedDialogsRequest) FillFrom(from interface {
	GetExcludePinned() (value bool)
	GetParentPeer() (value InputPeerClass, ok bool)
	GetOffsetDate() (value int)
	GetOffsetID() (value int)
	GetOffsetPeer() (value InputPeerClass)
	GetLimit() (value int)
	GetHash() (value int64)
}) {
	g.ExcludePinned = from.GetExcludePinned()
	if val, ok := from.GetParentPeer(); ok {
		g.ParentPeer = val
	}

	g.OffsetDate = from.GetOffsetDate()
	g.OffsetID = from.GetOffsetID()
	g.OffsetPeer = from.GetOffsetPeer()
	g.Limit = from.GetLimit()
	g.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetSavedDialogsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getSavedDialogs",
		ID:   MessagesGetSavedDialogsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ExcludePinned",
			SchemaName: "exclude_pinned",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "ParentPeer",
			SchemaName: "parent_peer",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "OffsetDate",
			SchemaName: "offset_date",
		},
		{
			Name:       "OffsetID",
			SchemaName: "offset_id",
		},
		{
			Name:       "OffsetPeer",
			SchemaName: "offset_peer",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (g *MessagesGetSavedDialogsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSavedDialogs#1e91fc99 as nil")
	}
	b.PutID(MessagesGetSavedDialogsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetSavedDialogsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSavedDialogs#1e91fc99 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSavedDialogs#1e91fc99: field flags: %w", err)
	}
	if g.Flags.Has(1) {
		if g.ParentPeer == nil {
			return fmt.Errorf("unable to encode messages.getSavedDialogs#1e91fc99: field parent_peer is nil")
		}
		if err := g.ParentPeer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.getSavedDialogs#1e91fc99: field parent_peer: %w", err)
		}
	}
	b.PutInt(g.OffsetDate)
	b.PutInt(g.OffsetID)
	if g.OffsetPeer == nil {
		return fmt.Errorf("unable to encode messages.getSavedDialogs#1e91fc99: field offset_peer is nil")
	}
	if err := g.OffsetPeer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSavedDialogs#1e91fc99: field offset_peer: %w", err)
	}
	b.PutInt(g.Limit)
	b.PutLong(g.Hash)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetSavedDialogsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getSavedDialogs#1e91fc99 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogs#1e91fc99: field flags: %w", err)
		}
	}
	g.ExcludePinned = g.Flags.Has(0)
	if g.Flags.Has(1) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogs#1e91fc99: field parent_peer: %w", err)
		}
		g.ParentPeer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogs#1e91fc99: field offset_date: %w", err)
		}
		g.OffsetDate = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogs#1e91fc99: field offset_id: %w", err)
		}
		g.OffsetID = value
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogs#1e91fc99: field offset_peer: %w", err)
		}
		g.OffsetPeer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogs#1e91fc99: field limit: %w", err)
		}
		g.Limit = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSavedDialogs#1e91fc99: field hash: %w", err)
		}
		g.Hash = value
	}
	return nil
}

// SetExcludePinned sets value of ExcludePinned conditional field.
func (g *MessagesGetSavedDialogsRequest) SetExcludePinned(value bool) {
	if value {
		g.Flags.Set(0)
		g.ExcludePinned = true
	} else {
		g.Flags.Unset(0)
		g.ExcludePinned = false
	}
}

// GetExcludePinned returns value of ExcludePinned conditional field.
func (g *MessagesGetSavedDialogsRequest) GetExcludePinned() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

// SetParentPeer sets value of ParentPeer conditional field.
func (g *MessagesGetSavedDialogsRequest) 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 *MessagesGetSavedDialogsRequest) GetParentPeer() (value InputPeerClass, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(1) {
		return value, false
	}
	return g.ParentPeer, true
}

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

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

// GetOffsetPeer returns value of OffsetPeer field.
func (g *MessagesGetSavedDialogsRequest) GetOffsetPeer() (value InputPeerClass) {
	if g == nil {
		return
	}
	return g.OffsetPeer
}

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

// GetHash returns value of Hash field.
func (g *MessagesGetSavedDialogsRequest) GetHash() (value int64) {
	if g == nil {
		return
	}
	return g.Hash
}

// MessagesGetSavedDialogs invokes method messages.getSavedDialogs#1e91fc99 returning error if any.
// Returns the current saved dialog list »¹ or monoforum topic list »².
//
// Links:
//  1. https://core.telegram.org/api/saved-messages
//  2. https://core.telegram.org/api/monoforum
//
// See https://core.telegram.org/method/messages.getSavedDialogs for reference.
func (c *Client) MessagesGetSavedDialogs(ctx context.Context, request *MessagesGetSavedDialogsRequest) (MessagesSavedDialogsClass, error) {
	var result MessagesSavedDialogsBox

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