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

// CheckEmailAddressVerificationCodeRequest represents TL type `checkEmailAddressVerificationCode#e695db03`.
type CheckEmailAddressVerificationCodeRequest struct {
	// Verification code to check
	Code string
}

// CheckEmailAddressVerificationCodeRequestTypeID is TL type id of CheckEmailAddressVerificationCodeRequest.
const CheckEmailAddressVerificationCodeRequestTypeID = 0xe695db03

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

func (c *CheckEmailAddressVerificationCodeRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Code == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CheckEmailAddressVerificationCodeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "checkEmailAddressVerificationCode",
		ID:   CheckEmailAddressVerificationCodeRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Code",
			SchemaName: "code",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CheckEmailAddressVerificationCodeRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkEmailAddressVerificationCode#e695db03 as nil")
	}
	b.PutID(CheckEmailAddressVerificationCodeRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CheckEmailAddressVerificationCodeRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkEmailAddressVerificationCode#e695db03 as nil")
	}
	b.PutString(c.Code)
	return nil
}

// Decode implements bin.Decoder.
func (c *CheckEmailAddressVerificationCodeRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode checkEmailAddressVerificationCode#e695db03 to nil")
	}
	if err := b.ConsumeID(CheckEmailAddressVerificationCodeRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode checkEmailAddressVerificationCode#e695db03: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CheckEmailAddressVerificationCodeRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode checkEmailAddressVerificationCode#e695db03 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode checkEmailAddressVerificationCode#e695db03: field code: %w", err)
		}
		c.Code = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CheckEmailAddressVerificationCodeRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode checkEmailAddressVerificationCode#e695db03 as nil")
	}
	b.ObjStart()
	b.PutID("checkEmailAddressVerificationCode")
	b.Comma()
	b.FieldStart("code")
	b.PutString(c.Code)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CheckEmailAddressVerificationCodeRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode checkEmailAddressVerificationCode#e695db03 to nil")
	}

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

// GetCode returns value of Code field.
func (c *CheckEmailAddressVerificationCodeRequest) GetCode() (value string) {
	if c == nil {
		return
	}
	return c.Code
}

// CheckEmailAddressVerificationCode invokes method checkEmailAddressVerificationCode#e695db03 returning error if any.
func (c *Client) CheckEmailAddressVerificationCode(ctx context.Context, code string) error {
	var ok Ok

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