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

// CheckLoginEmailAddressCodeRequest represents TL type `checkLoginEmailAddressCode#a951fc62`.
type CheckLoginEmailAddressCodeRequest struct {
	// Email address authentication to check
	Code EmailAddressAuthenticationClass
}

// CheckLoginEmailAddressCodeRequestTypeID is TL type id of CheckLoginEmailAddressCodeRequest.
const CheckLoginEmailAddressCodeRequestTypeID = 0xa951fc62

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (c *CheckLoginEmailAddressCodeRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkLoginEmailAddressCode#a951fc62 as nil")
	}
	b.PutID(CheckLoginEmailAddressCodeRequestTypeID)
	return c.EncodeBare(b)
}

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

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

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

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

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

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

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

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

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