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

// ReorderBotActiveUsernamesRequest represents TL type `reorderBotActiveUsernames#acadc444`.
type ReorderBotActiveUsernamesRequest struct {
	// Identifier of the target bot
	BotUserID int64
	// The new order of active usernames. All currently active usernames must be specified
	Usernames []string
}

// ReorderBotActiveUsernamesRequestTypeID is TL type id of ReorderBotActiveUsernamesRequest.
const ReorderBotActiveUsernamesRequestTypeID = 0xacadc444

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *ReorderBotActiveUsernamesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "reorderBotActiveUsernames",
		ID:   ReorderBotActiveUsernamesRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "Usernames",
			SchemaName: "usernames",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ReorderBotActiveUsernamesRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode reorderBotActiveUsernames#acadc444 as nil")
	}
	b.PutID(ReorderBotActiveUsernamesRequestTypeID)
	return r.EncodeBare(b)
}

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

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

// DecodeBare implements bin.BareDecoder.
func (r *ReorderBotActiveUsernamesRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode reorderBotActiveUsernames#acadc444 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode reorderBotActiveUsernames#acadc444: field bot_user_id: %w", err)
		}
		r.BotUserID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode reorderBotActiveUsernames#acadc444: 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 reorderBotActiveUsernames#acadc444: field usernames: %w", err)
			}
			r.Usernames = append(r.Usernames, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *ReorderBotActiveUsernamesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode reorderBotActiveUsernames#acadc444 as nil")
	}
	b.ObjStart()
	b.PutID("reorderBotActiveUsernames")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(r.BotUserID)
	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 *ReorderBotActiveUsernamesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode reorderBotActiveUsernames#acadc444 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("reorderBotActiveUsernames"); err != nil {
				return fmt.Errorf("unable to decode reorderBotActiveUsernames#acadc444: %w", err)
			}
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode reorderBotActiveUsernames#acadc444: field bot_user_id: %w", err)
			}
			r.BotUserID = 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 reorderBotActiveUsernames#acadc444: field usernames: %w", err)
				}
				r.Usernames = append(r.Usernames, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode reorderBotActiveUsernames#acadc444: field usernames: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBotUserID returns value of BotUserID field.
func (r *ReorderBotActiveUsernamesRequest) GetBotUserID() (value int64) {
	if r == nil {
		return
	}
	return r.BotUserID
}

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

// ReorderBotActiveUsernames invokes method reorderBotActiveUsernames#acadc444 returning error if any.
func (c *Client) ReorderBotActiveUsernames(ctx context.Context, request *ReorderBotActiveUsernamesRequest) error {
	var ok Ok

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