// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// AccountSendVerifyEmailCodeRequest represents TL type `account.sendVerifyEmailCode#98e037bb`.
// Send an email verification code.
//
// See https://core.telegram.org/method/account.sendVerifyEmailCode for reference.
type AccountSendVerifyEmailCodeRequest struct {
	// Verification purpose.
	Purpose EmailVerifyPurposeClass
	// The email where to send the code.
	Email string
}

// AccountSendVerifyEmailCodeRequestTypeID is TL type id of AccountSendVerifyEmailCodeRequest.
const AccountSendVerifyEmailCodeRequestTypeID = 0x98e037bb

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

func (s *AccountSendVerifyEmailCodeRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Purpose == nil) {
		return false
	}
	if !(s.Email == "") {
		return false
	}

	return true
}

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

// FillFrom fills AccountSendVerifyEmailCodeRequest from given interface.
func (s *AccountSendVerifyEmailCodeRequest) FillFrom(from interface {
	GetPurpose() (value EmailVerifyPurposeClass)
	GetEmail() (value string)
}) {
	s.Purpose = from.GetPurpose()
	s.Email = from.GetEmail()
}

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

// TypeName returns name of type in TL schema.
func (*AccountSendVerifyEmailCodeRequest) TypeName() string {
	return "account.sendVerifyEmailCode"
}

// TypeInfo returns info about TL type.
func (s *AccountSendVerifyEmailCodeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.sendVerifyEmailCode",
		ID:   AccountSendVerifyEmailCodeRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Purpose",
			SchemaName: "purpose",
		},
		{
			Name:       "Email",
			SchemaName: "email",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *AccountSendVerifyEmailCodeRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode account.sendVerifyEmailCode#98e037bb as nil")
	}
	b.PutID(AccountSendVerifyEmailCodeRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *AccountSendVerifyEmailCodeRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode account.sendVerifyEmailCode#98e037bb as nil")
	}
	if s.Purpose == nil {
		return fmt.Errorf("unable to encode account.sendVerifyEmailCode#98e037bb: field purpose is nil")
	}
	if err := s.Purpose.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.sendVerifyEmailCode#98e037bb: field purpose: %w", err)
	}
	b.PutString(s.Email)
	return nil
}

// Decode implements bin.Decoder.
func (s *AccountSendVerifyEmailCodeRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode account.sendVerifyEmailCode#98e037bb to nil")
	}
	if err := b.ConsumeID(AccountSendVerifyEmailCodeRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.sendVerifyEmailCode#98e037bb: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *AccountSendVerifyEmailCodeRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode account.sendVerifyEmailCode#98e037bb to nil")
	}
	{
		value, err := DecodeEmailVerifyPurpose(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.sendVerifyEmailCode#98e037bb: field purpose: %w", err)
		}
		s.Purpose = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode account.sendVerifyEmailCode#98e037bb: field email: %w", err)
		}
		s.Email = value
	}
	return nil
}

// GetPurpose returns value of Purpose field.
func (s *AccountSendVerifyEmailCodeRequest) GetPurpose() (value EmailVerifyPurposeClass) {
	if s == nil {
		return
	}
	return s.Purpose
}

// GetEmail returns value of Email field.
func (s *AccountSendVerifyEmailCodeRequest) GetEmail() (value string) {
	if s == nil {
		return
	}
	return s.Email
}

// AccountSendVerifyEmailCode invokes method account.sendVerifyEmailCode#98e037bb returning error if any.
// Send an email verification code.
//
// Possible errors:
//
//	400 EMAIL_INVALID: The specified email is invalid.
//	400 EMAIL_NOT_SETUP: In order to change the login email with emailVerifyPurposeLoginChange, an existing login email must already be set using emailVerifyPurposeLoginSetup.
//	400 PHONE_HASH_EXPIRED: An invalid or expired phone_code_hash was provided.
//	400 PHONE_NUMBER_INVALID: The phone number is invalid.
//
// See https://core.telegram.org/method/account.sendVerifyEmailCode for reference.
func (c *Client) AccountSendVerifyEmailCode(ctx context.Context, request *AccountSendVerifyEmailCodeRequest) (*AccountSentEmailCode, error) {
	var result AccountSentEmailCode

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