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

// GetRecoveryEmailAddressRequest represents TL type `getRecoveryEmailAddress#a0f1b9fd`.
type GetRecoveryEmailAddressRequest struct {
	// The 2-step verification password for the current user
	Password string
}

// GetRecoveryEmailAddressRequestTypeID is TL type id of GetRecoveryEmailAddressRequest.
const GetRecoveryEmailAddressRequestTypeID = 0xa0f1b9fd

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

func (g *GetRecoveryEmailAddressRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Password == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetRecoveryEmailAddressRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getRecoveryEmailAddress",
		ID:   GetRecoveryEmailAddressRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Password",
			SchemaName: "password",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetRecoveryEmailAddressRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getRecoveryEmailAddress#a0f1b9fd as nil")
	}
	b.PutID(GetRecoveryEmailAddressRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetRecoveryEmailAddressRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getRecoveryEmailAddress#a0f1b9fd as nil")
	}
	b.PutString(g.Password)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetRecoveryEmailAddressRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getRecoveryEmailAddress#a0f1b9fd to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getRecoveryEmailAddress#a0f1b9fd: field password: %w", err)
		}
		g.Password = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetRecoveryEmailAddressRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getRecoveryEmailAddress#a0f1b9fd as nil")
	}
	b.ObjStart()
	b.PutID("getRecoveryEmailAddress")
	b.Comma()
	b.FieldStart("password")
	b.PutString(g.Password)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getRecoveryEmailAddress"); err != nil {
				return fmt.Errorf("unable to decode getRecoveryEmailAddress#a0f1b9fd: %w", err)
			}
		case "password":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getRecoveryEmailAddress#a0f1b9fd: field password: %w", err)
			}
			g.Password = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPassword returns value of Password field.
func (g *GetRecoveryEmailAddressRequest) GetPassword() (value string) {
	if g == nil {
		return
	}
	return g.Password
}

// GetRecoveryEmailAddress invokes method getRecoveryEmailAddress#a0f1b9fd returning error if any.
func (c *Client) GetRecoveryEmailAddress(ctx context.Context, password string) (*RecoveryEmailAddress, error) {
	var result RecoveryEmailAddress

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