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

// GetUserRequest represents TL type `getUser#42999c0b`.
type GetUserRequest struct {
	// User identifier
	UserID int64
}

// GetUserRequestTypeID is TL type id of GetUserRequest.
const GetUserRequestTypeID = 0x42999c0b

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

func (g *GetUserRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.UserID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetUserRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getUser",
		ID:   GetUserRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetUserRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getUser#42999c0b as nil")
	}
	b.PutID(GetUserRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetUserRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getUser#42999c0b as nil")
	}
	b.PutInt53(g.UserID)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetUserRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getUser#42999c0b to nil")
	}
	if err := b.ConsumeID(GetUserRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getUser#42999c0b: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetUserRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getUser#42999c0b to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getUser#42999c0b: field user_id: %w", err)
		}
		g.UserID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetUserRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getUser#42999c0b as nil")
	}
	b.ObjStart()
	b.PutID("getUser")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(g.UserID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetUserRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getUser#42999c0b to nil")
	}

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

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

// GetUser invokes method getUser#42999c0b returning error if any.
func (c *Client) GetUser(ctx context.Context, userid int64) (*User, error) {
	var result User

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