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

// MessagesReorderPinnedForumTopicsRequest represents TL type `messages.reorderPinnedForumTopics#e7841f0`.
//
// See https://core.telegram.org/method/messages.reorderPinnedForumTopics for reference.
type MessagesReorderPinnedForumTopicsRequest struct {
	// Flags field of MessagesReorderPinnedForumTopicsRequest.
	Flags bin.Fields
	// Force field of MessagesReorderPinnedForumTopicsRequest.
	Force bool
	// Peer field of MessagesReorderPinnedForumTopicsRequest.
	Peer InputPeerClass
	// Order field of MessagesReorderPinnedForumTopicsRequest.
	Order []int
}

// MessagesReorderPinnedForumTopicsRequestTypeID is TL type id of MessagesReorderPinnedForumTopicsRequest.
const MessagesReorderPinnedForumTopicsRequestTypeID = 0xe7841f0

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

func (r *MessagesReorderPinnedForumTopicsRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.Force == false) {
		return false
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.Order == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesReorderPinnedForumTopicsRequest from given interface.
func (r *MessagesReorderPinnedForumTopicsRequest) FillFrom(from interface {
	GetForce() (value bool)
	GetPeer() (value InputPeerClass)
	GetOrder() (value []int)
}) {
	r.Force = from.GetForce()
	r.Peer = from.GetPeer()
	r.Order = from.GetOrder()
}

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

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

// TypeInfo returns info about TL type.
func (r *MessagesReorderPinnedForumTopicsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.reorderPinnedForumTopics",
		ID:   MessagesReorderPinnedForumTopicsRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Force",
			SchemaName: "force",
			Null:       !r.Flags.Has(0),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Order",
			SchemaName: "order",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *MessagesReorderPinnedForumTopicsRequest) SetFlags() {
	if !(r.Force == false) {
		r.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (r *MessagesReorderPinnedForumTopicsRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.reorderPinnedForumTopics#e7841f0 as nil")
	}
	b.PutID(MessagesReorderPinnedForumTopicsRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesReorderPinnedForumTopicsRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.reorderPinnedForumTopics#e7841f0 as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.reorderPinnedForumTopics#e7841f0: field flags: %w", err)
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode messages.reorderPinnedForumTopics#e7841f0: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.reorderPinnedForumTopics#e7841f0: field peer: %w", err)
	}
	b.PutVectorHeader(len(r.Order))
	for _, v := range r.Order {
		b.PutInt(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *MessagesReorderPinnedForumTopicsRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.reorderPinnedForumTopics#e7841f0 to nil")
	}
	if err := b.ConsumeID(MessagesReorderPinnedForumTopicsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.reorderPinnedForumTopics#e7841f0: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *MessagesReorderPinnedForumTopicsRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.reorderPinnedForumTopics#e7841f0 to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.reorderPinnedForumTopics#e7841f0: field flags: %w", err)
		}
	}
	r.Force = r.Flags.Has(0)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.reorderPinnedForumTopics#e7841f0: field peer: %w", err)
		}
		r.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.reorderPinnedForumTopics#e7841f0: field order: %w", err)
		}

		if headerLen > 0 {
			r.Order = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode messages.reorderPinnedForumTopics#e7841f0: field order: %w", err)
			}
			r.Order = append(r.Order, value)
		}
	}
	return nil
}

// SetForce sets value of Force conditional field.
func (r *MessagesReorderPinnedForumTopicsRequest) SetForce(value bool) {
	if value {
		r.Flags.Set(0)
		r.Force = true
	} else {
		r.Flags.Unset(0)
		r.Force = false
	}
}

// GetForce returns value of Force conditional field.
func (r *MessagesReorderPinnedForumTopicsRequest) GetForce() (value bool) {
	if r == nil {
		return
	}
	return r.Flags.Has(0)
}

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

// GetOrder returns value of Order field.
func (r *MessagesReorderPinnedForumTopicsRequest) GetOrder() (value []int) {
	if r == nil {
		return
	}
	return r.Order
}

// MessagesReorderPinnedForumTopics invokes method messages.reorderPinnedForumTopics#e7841f0 returning error if any.
//
// See https://core.telegram.org/method/messages.reorderPinnedForumTopics for reference.
func (c *Client) MessagesReorderPinnedForumTopics(ctx context.Context, request *MessagesReorderPinnedForumTopicsRequest) (UpdatesClass, error) {
	var result UpdatesBox

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