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

// AccountVerifyEmailRequest represents TL type `account.verifyEmail#32da4cf`.
// Verify an email address.
//
// See https://core.telegram.org/method/account.verifyEmail for reference.
type AccountVerifyEmailRequest struct {
	// Verification purpose
	Purpose EmailVerifyPurposeClass
	// Email verification code or token
	Verification EmailVerificationClass
}

// AccountVerifyEmailRequestTypeID is TL type id of AccountVerifyEmailRequest.
const AccountVerifyEmailRequestTypeID = 0x32da4cf

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

func (v *AccountVerifyEmailRequest) Zero() bool {
	if v == nil {
		return true
	}
	if !(v.Purpose == nil) {
		return false
	}
	if !(v.Verification == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountVerifyEmailRequest from given interface.
func (v *AccountVerifyEmailRequest) FillFrom(from interface {
	GetPurpose() (value EmailVerifyPurposeClass)
	GetVerification() (value EmailVerificationClass)
}) {
	v.Purpose = from.GetPurpose()
	v.Verification = from.GetVerification()
}

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

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

// TypeInfo returns info about TL type.
func (v *AccountVerifyEmailRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.verifyEmail",
		ID:   AccountVerifyEmailRequestTypeID,
	}
	if v == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Purpose",
			SchemaName: "purpose",
		},
		{
			Name:       "Verification",
			SchemaName: "verification",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (v *AccountVerifyEmailRequest) Encode(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't encode account.verifyEmail#32da4cf as nil")
	}
	b.PutID(AccountVerifyEmailRequestTypeID)
	return v.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (v *AccountVerifyEmailRequest) EncodeBare(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't encode account.verifyEmail#32da4cf as nil")
	}
	if v.Purpose == nil {
		return fmt.Errorf("unable to encode account.verifyEmail#32da4cf: field purpose is nil")
	}
	if err := v.Purpose.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.verifyEmail#32da4cf: field purpose: %w", err)
	}
	if v.Verification == nil {
		return fmt.Errorf("unable to encode account.verifyEmail#32da4cf: field verification is nil")
	}
	if err := v.Verification.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.verifyEmail#32da4cf: field verification: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (v *AccountVerifyEmailRequest) Decode(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't decode account.verifyEmail#32da4cf to nil")
	}
	if err := b.ConsumeID(AccountVerifyEmailRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.verifyEmail#32da4cf: %w", err)
	}
	return v.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (v *AccountVerifyEmailRequest) DecodeBare(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't decode account.verifyEmail#32da4cf to nil")
	}
	{
		value, err := DecodeEmailVerifyPurpose(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.verifyEmail#32da4cf: field purpose: %w", err)
		}
		v.Purpose = value
	}
	{
		value, err := DecodeEmailVerification(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.verifyEmail#32da4cf: field verification: %w", err)
		}
		v.Verification = value
	}
	return nil
}

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

// GetVerification returns value of Verification field.
func (v *AccountVerifyEmailRequest) GetVerification() (value EmailVerificationClass) {
	if v == nil {
		return
	}
	return v.Verification
}

// AccountVerifyEmail invokes method account.verifyEmail#32da4cf returning error if any.
// Verify an email address.
//
// Possible errors:
//
//	400 EMAIL_INVALID: The specified email is invalid.
//	400 EMAIL_VERIFY_EXPIRED: The verification email has expired.
//
// See https://core.telegram.org/method/account.verifyEmail for reference.
func (c *Client) AccountVerifyEmail(ctx context.Context, request *AccountVerifyEmailRequest) (AccountEmailVerifiedClass, error) {
	var result AccountEmailVerifiedBox

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