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

// CheckAuthenticationEmailCodeRequest represents TL type `checkAuthenticationEmailCode#dd42c29f`.
type CheckAuthenticationEmailCodeRequest struct {
	// Email address authentication to check
	Code EmailAddressAuthenticationClass
}

// CheckAuthenticationEmailCodeRequestTypeID is TL type id of CheckAuthenticationEmailCodeRequest.
const CheckAuthenticationEmailCodeRequestTypeID = 0xdd42c29f

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (c *CheckAuthenticationEmailCodeRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkAuthenticationEmailCode#dd42c29f as nil")
	}
	b.PutID(CheckAuthenticationEmailCodeRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CheckAuthenticationEmailCodeRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkAuthenticationEmailCode#dd42c29f as nil")
	}
	if c.Code == nil {
		return fmt.Errorf("unable to encode checkAuthenticationEmailCode#dd42c29f: field code is nil")
	}
	if err := c.Code.Encode(b); err != nil {
		return fmt.Errorf("unable to encode checkAuthenticationEmailCode#dd42c29f: field code: %w", err)
	}
	return nil
}

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CheckAuthenticationEmailCodeRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode checkAuthenticationEmailCode#dd42c29f as nil")
	}
	b.ObjStart()
	b.PutID("checkAuthenticationEmailCode")
	b.Comma()
	b.FieldStart("code")
	if c.Code == nil {
		return fmt.Errorf("unable to encode checkAuthenticationEmailCode#dd42c29f: field code is nil")
	}
	if err := c.Code.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode checkAuthenticationEmailCode#dd42c29f: field code: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// CheckAuthenticationEmailCode invokes method checkAuthenticationEmailCode#dd42c29f returning error if any.
func (c *Client) CheckAuthenticationEmailCode(ctx context.Context, code EmailAddressAuthenticationClass) error {
	var ok Ok

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