// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// ChannelsReorderPinnedForumTopicsRequest represents TL type `channels.reorderPinnedForumTopics#2950a18f`.
// Reorder pinned forum topics
//
// See https://core.telegram.org/method/channels.reorderPinnedForumTopics for reference.
type ChannelsReorderPinnedForumTopicsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, topics pinned server-side but not present in the order field will be unpinned.
	Force bool
	// Supergroup ID
	Channel InputChannelClass
	// Topic IDs »¹
	//
	// Links:
	//  1) https://core.telegram.org/api/forum
	Order []int
}

// ChannelsReorderPinnedForumTopicsRequestTypeID is TL type id of ChannelsReorderPinnedForumTopicsRequest.
const ChannelsReorderPinnedForumTopicsRequestTypeID = 0x2950a18f

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

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

	return true
}

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

// FillFrom fills ChannelsReorderPinnedForumTopicsRequest from given interface.
func (r *ChannelsReorderPinnedForumTopicsRequest) FillFrom(from interface {
	GetForce() (value bool)
	GetChannel() (value InputChannelClass)
	GetOrder() (value []int)
}) {
	r.Force = from.GetForce()
	r.Channel = from.GetChannel()
	r.Order = from.GetOrder()
}

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

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

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

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

// Encode implements bin.Encoder.
func (r *ChannelsReorderPinnedForumTopicsRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode channels.reorderPinnedForumTopics#2950a18f as nil")
	}
	b.PutID(ChannelsReorderPinnedForumTopicsRequestTypeID)
	return r.EncodeBare(b)
}

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

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

// DecodeBare implements bin.BareDecoder.
func (r *ChannelsReorderPinnedForumTopicsRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode channels.reorderPinnedForumTopics#2950a18f to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode channels.reorderPinnedForumTopics#2950a18f: field flags: %w", err)
		}
	}
	r.Force = r.Flags.Has(0)
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.reorderPinnedForumTopics#2950a18f: field channel: %w", err)
		}
		r.Channel = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode channels.reorderPinnedForumTopics#2950a18f: 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 channels.reorderPinnedForumTopics#2950a18f: field order: %w", err)
			}
			r.Order = append(r.Order, value)
		}
	}
	return nil
}

// SetForce sets value of Force conditional field.
func (r *ChannelsReorderPinnedForumTopicsRequest) 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 *ChannelsReorderPinnedForumTopicsRequest) GetForce() (value bool) {
	if r == nil {
		return
	}
	return r.Flags.Has(0)
}

// GetChannel returns value of Channel field.
func (r *ChannelsReorderPinnedForumTopicsRequest) GetChannel() (value InputChannelClass) {
	if r == nil {
		return
	}
	return r.Channel
}

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

// GetChannelAsNotEmpty returns mapped value of Channel field.
func (r *ChannelsReorderPinnedForumTopicsRequest) GetChannelAsNotEmpty() (NotEmptyInputChannel, bool) {
	return r.Channel.AsNotEmpty()
}

// ChannelsReorderPinnedForumTopics invokes method channels.reorderPinnedForumTopics#2950a18f returning error if any.
// Reorder pinned forum topics
//
// See https://core.telegram.org/method/channels.reorderPinnedForumTopics for reference.
// Can be used by bots.
func (c *Client) ChannelsReorderPinnedForumTopics(ctx context.Context, request *ChannelsReorderPinnedForumTopicsRequest) (UpdatesClass, error) {
	var result UpdatesBox

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