// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// ReorderSupergroupActiveUsernamesRequest represents TL type `reorderSupergroupActiveUsernames#663c7f3b`.
type ReorderSupergroupActiveUsernamesRequest struct {
	// Identifier of the supergroup or channel
	SupergroupID int64
	// The new order of active usernames. All currently active usernames must be specified
	Usernames []string
}

// ReorderSupergroupActiveUsernamesRequestTypeID is TL type id of ReorderSupergroupActiveUsernamesRequest.
const ReorderSupergroupActiveUsernamesRequestTypeID = 0x663c7f3b

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

func (r *ReorderSupergroupActiveUsernamesRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.SupergroupID == 0) {
		return false
	}
	if !(r.Usernames == nil) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*ReorderSupergroupActiveUsernamesRequest) TypeName() string {
	return "reorderSupergroupActiveUsernames"
}

// TypeInfo returns info about TL type.
func (r *ReorderSupergroupActiveUsernamesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "reorderSupergroupActiveUsernames",
		ID:   ReorderSupergroupActiveUsernamesRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SupergroupID",
			SchemaName: "supergroup_id",
		},
		{
			Name:       "Usernames",
			SchemaName: "usernames",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ReorderSupergroupActiveUsernamesRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode reorderSupergroupActiveUsernames#663c7f3b as nil")
	}
	b.PutID(ReorderSupergroupActiveUsernamesRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ReorderSupergroupActiveUsernamesRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode reorderSupergroupActiveUsernames#663c7f3b as nil")
	}
	b.PutInt53(r.SupergroupID)
	b.PutInt(len(r.Usernames))
	for _, v := range r.Usernames {
		b.PutString(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *ReorderSupergroupActiveUsernamesRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode reorderSupergroupActiveUsernames#663c7f3b to nil")
	}
	if err := b.ConsumeID(ReorderSupergroupActiveUsernamesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode reorderSupergroupActiveUsernames#663c7f3b: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ReorderSupergroupActiveUsernamesRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode reorderSupergroupActiveUsernames#663c7f3b to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode reorderSupergroupActiveUsernames#663c7f3b: field supergroup_id: %w", err)
		}
		r.SupergroupID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode reorderSupergroupActiveUsernames#663c7f3b: field usernames: %w", err)
		}

		if headerLen > 0 {
			r.Usernames = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode reorderSupergroupActiveUsernames#663c7f3b: field usernames: %w", err)
			}
			r.Usernames = append(r.Usernames, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *ReorderSupergroupActiveUsernamesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode reorderSupergroupActiveUsernames#663c7f3b as nil")
	}
	b.ObjStart()
	b.PutID("reorderSupergroupActiveUsernames")
	b.Comma()
	b.FieldStart("supergroup_id")
	b.PutInt53(r.SupergroupID)
	b.Comma()
	b.FieldStart("usernames")
	b.ArrStart()
	for _, v := range r.Usernames {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *ReorderSupergroupActiveUsernamesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode reorderSupergroupActiveUsernames#663c7f3b to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("reorderSupergroupActiveUsernames"); err != nil {
				return fmt.Errorf("unable to decode reorderSupergroupActiveUsernames#663c7f3b: %w", err)
			}
		case "supergroup_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode reorderSupergroupActiveUsernames#663c7f3b: field supergroup_id: %w", err)
			}
			r.SupergroupID = value
		case "usernames":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.String()
				if err != nil {
					return fmt.Errorf("unable to decode reorderSupergroupActiveUsernames#663c7f3b: field usernames: %w", err)
				}
				r.Usernames = append(r.Usernames, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode reorderSupergroupActiveUsernames#663c7f3b: field usernames: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSupergroupID returns value of SupergroupID field.
func (r *ReorderSupergroupActiveUsernamesRequest) GetSupergroupID() (value int64) {
	if r == nil {
		return
	}
	return r.SupergroupID
}

// GetUsernames returns value of Usernames field.
func (r *ReorderSupergroupActiveUsernamesRequest) GetUsernames() (value []string) {
	if r == nil {
		return
	}
	return r.Usernames
}

// ReorderSupergroupActiveUsernames invokes method reorderSupergroupActiveUsernames#663c7f3b returning error if any.
func (c *Client) ReorderSupergroupActiveUsernames(ctx context.Context, request *ReorderSupergroupActiveUsernamesRequest) error {
	var ok Ok

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