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

// SetCommandsRequest represents TL type `setCommands#b0e0217a`.
type SetCommandsRequest struct {
	// The scope to which the commands are relevant; pass null to change commands in the
	// default bot command scope
	Scope BotCommandScopeClass
	// A two-letter ISO 639-1 language code. If empty, the commands will be applied to all
	// users from the given scope, for which language there are no dedicated commands
	LanguageCode string
	// List of the bot's commands
	Commands []BotCommand
}

// SetCommandsRequestTypeID is TL type id of SetCommandsRequest.
const SetCommandsRequestTypeID = 0xb0e0217a

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

func (s *SetCommandsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Scope == nil) {
		return false
	}
	if !(s.LanguageCode == "") {
		return false
	}
	if !(s.Commands == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetCommandsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setCommands",
		ID:   SetCommandsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Scope",
			SchemaName: "scope",
		},
		{
			Name:       "LanguageCode",
			SchemaName: "language_code",
		},
		{
			Name:       "Commands",
			SchemaName: "commands",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetCommandsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setCommands#b0e0217a as nil")
	}
	b.PutID(SetCommandsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetCommandsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setCommands#b0e0217a as nil")
	}
	if s.Scope == nil {
		return fmt.Errorf("unable to encode setCommands#b0e0217a: field scope is nil")
	}
	if err := s.Scope.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setCommands#b0e0217a: field scope: %w", err)
	}
	b.PutString(s.LanguageCode)
	b.PutInt(len(s.Commands))
	for idx, v := range s.Commands {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare setCommands#b0e0217a: field commands element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetCommandsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setCommands#b0e0217a to nil")
	}
	if err := b.ConsumeID(SetCommandsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setCommands#b0e0217a: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetCommandsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setCommands#b0e0217a to nil")
	}
	{
		value, err := DecodeBotCommandScope(b)
		if err != nil {
			return fmt.Errorf("unable to decode setCommands#b0e0217a: field scope: %w", err)
		}
		s.Scope = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setCommands#b0e0217a: field language_code: %w", err)
		}
		s.LanguageCode = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode setCommands#b0e0217a: field commands: %w", err)
		}

		if headerLen > 0 {
			s.Commands = make([]BotCommand, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BotCommand
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare setCommands#b0e0217a: field commands: %w", err)
			}
			s.Commands = append(s.Commands, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetCommandsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setCommands#b0e0217a as nil")
	}
	b.ObjStart()
	b.PutID("setCommands")
	b.Comma()
	b.FieldStart("scope")
	if s.Scope == nil {
		return fmt.Errorf("unable to encode setCommands#b0e0217a: field scope is nil")
	}
	if err := s.Scope.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setCommands#b0e0217a: field scope: %w", err)
	}
	b.Comma()
	b.FieldStart("language_code")
	b.PutString(s.LanguageCode)
	b.Comma()
	b.FieldStart("commands")
	b.ArrStart()
	for idx, v := range s.Commands {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode setCommands#b0e0217a: field commands element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetCommandsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setCommands#b0e0217a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setCommands"); err != nil {
				return fmt.Errorf("unable to decode setCommands#b0e0217a: %w", err)
			}
		case "scope":
			value, err := DecodeTDLibJSONBotCommandScope(b)
			if err != nil {
				return fmt.Errorf("unable to decode setCommands#b0e0217a: field scope: %w", err)
			}
			s.Scope = value
		case "language_code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setCommands#b0e0217a: field language_code: %w", err)
			}
			s.LanguageCode = value
		case "commands":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value BotCommand
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode setCommands#b0e0217a: field commands: %w", err)
				}
				s.Commands = append(s.Commands, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode setCommands#b0e0217a: field commands: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetScope returns value of Scope field.
func (s *SetCommandsRequest) GetScope() (value BotCommandScopeClass) {
	if s == nil {
		return
	}
	return s.Scope
}

// GetLanguageCode returns value of LanguageCode field.
func (s *SetCommandsRequest) GetLanguageCode() (value string) {
	if s == nil {
		return
	}
	return s.LanguageCode
}

// GetCommands returns value of Commands field.
func (s *SetCommandsRequest) GetCommands() (value []BotCommand) {
	if s == nil {
		return
	}
	return s.Commands
}

// SetCommands invokes method setCommands#b0e0217a returning error if any.
func (c *Client) SetCommands(ctx context.Context, request *SetCommandsRequest) error {
	var ok Ok

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