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

// SetMenuButtonRequest represents TL type `setMenuButton#b44fc141`.
type SetMenuButtonRequest struct {
	// Identifier of the user or 0 to set menu button for all users
	UserID int64
	// New menu button
	MenuButton BotMenuButton
}

// SetMenuButtonRequestTypeID is TL type id of SetMenuButtonRequest.
const SetMenuButtonRequestTypeID = 0xb44fc141

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

func (s *SetMenuButtonRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.UserID == 0) {
		return false
	}
	if !(s.MenuButton.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetMenuButtonRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setMenuButton",
		ID:   SetMenuButtonRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "MenuButton",
			SchemaName: "menu_button",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetMenuButtonRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setMenuButton#b44fc141 as nil")
	}
	b.PutID(SetMenuButtonRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetMenuButtonRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setMenuButton#b44fc141 as nil")
	}
	b.PutInt53(s.UserID)
	if err := s.MenuButton.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setMenuButton#b44fc141: field menu_button: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetMenuButtonRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setMenuButton#b44fc141 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setMenuButton#b44fc141: field user_id: %w", err)
		}
		s.UserID = value
	}
	{
		if err := s.MenuButton.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setMenuButton#b44fc141: field menu_button: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetMenuButtonRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setMenuButton#b44fc141 as nil")
	}
	b.ObjStart()
	b.PutID("setMenuButton")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(s.UserID)
	b.Comma()
	b.FieldStart("menu_button")
	if err := s.MenuButton.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setMenuButton#b44fc141: field menu_button: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setMenuButton"); err != nil {
				return fmt.Errorf("unable to decode setMenuButton#b44fc141: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setMenuButton#b44fc141: field user_id: %w", err)
			}
			s.UserID = value
		case "menu_button":
			if err := s.MenuButton.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setMenuButton#b44fc141: field menu_button: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (s *SetMenuButtonRequest) GetUserID() (value int64) {
	if s == nil {
		return
	}
	return s.UserID
}

// GetMenuButton returns value of MenuButton field.
func (s *SetMenuButtonRequest) GetMenuButton() (value BotMenuButton) {
	if s == nil {
		return
	}
	return s.MenuButton
}

// SetMenuButton invokes method setMenuButton#b44fc141 returning error if any.
func (c *Client) SetMenuButton(ctx context.Context, request *SetMenuButtonRequest) error {
	var ok Ok

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