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

// EmailAddressAuthenticationCodeInfo represents TL type `emailAddressAuthenticationCodeInfo#449be223`.
type EmailAddressAuthenticationCodeInfo struct {
	// Pattern of the email address to which an authentication code was sent
	EmailAddressPattern string
	// Length of the code; 0 if unknown
	Length int32
}

// EmailAddressAuthenticationCodeInfoTypeID is TL type id of EmailAddressAuthenticationCodeInfo.
const EmailAddressAuthenticationCodeInfoTypeID = 0x449be223

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

func (e *EmailAddressAuthenticationCodeInfo) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.EmailAddressPattern == "") {
		return false
	}
	if !(e.Length == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EmailAddressAuthenticationCodeInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emailAddressAuthenticationCodeInfo",
		ID:   EmailAddressAuthenticationCodeInfoTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "EmailAddressPattern",
			SchemaName: "email_address_pattern",
		},
		{
			Name:       "Length",
			SchemaName: "length",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmailAddressAuthenticationCodeInfo) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressAuthenticationCodeInfo#449be223 as nil")
	}
	b.PutID(EmailAddressAuthenticationCodeInfoTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmailAddressAuthenticationCodeInfo) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressAuthenticationCodeInfo#449be223 as nil")
	}
	b.PutString(e.EmailAddressPattern)
	b.PutInt32(e.Length)
	return nil
}

// Decode implements bin.Decoder.
func (e *EmailAddressAuthenticationCodeInfo) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressAuthenticationCodeInfo#449be223 to nil")
	}
	if err := b.ConsumeID(EmailAddressAuthenticationCodeInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode emailAddressAuthenticationCodeInfo#449be223: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmailAddressAuthenticationCodeInfo) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressAuthenticationCodeInfo#449be223 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode emailAddressAuthenticationCodeInfo#449be223: field email_address_pattern: %w", err)
		}
		e.EmailAddressPattern = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode emailAddressAuthenticationCodeInfo#449be223: field length: %w", err)
		}
		e.Length = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EmailAddressAuthenticationCodeInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressAuthenticationCodeInfo#449be223 as nil")
	}
	b.ObjStart()
	b.PutID("emailAddressAuthenticationCodeInfo")
	b.Comma()
	b.FieldStart("email_address_pattern")
	b.PutString(e.EmailAddressPattern)
	b.Comma()
	b.FieldStart("length")
	b.PutInt32(e.Length)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EmailAddressAuthenticationCodeInfo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressAuthenticationCodeInfo#449be223 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("emailAddressAuthenticationCodeInfo"); err != nil {
				return fmt.Errorf("unable to decode emailAddressAuthenticationCodeInfo#449be223: %w", err)
			}
		case "email_address_pattern":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode emailAddressAuthenticationCodeInfo#449be223: field email_address_pattern: %w", err)
			}
			e.EmailAddressPattern = value
		case "length":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode emailAddressAuthenticationCodeInfo#449be223: field length: %w", err)
			}
			e.Length = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetEmailAddressPattern returns value of EmailAddressPattern field.
func (e *EmailAddressAuthenticationCodeInfo) GetEmailAddressPattern() (value string) {
	if e == nil {
		return
	}
	return e.EmailAddressPattern
}

// GetLength returns value of Length field.
func (e *EmailAddressAuthenticationCodeInfo) GetLength() (value int32) {
	if e == nil {
		return
	}
	return e.Length
}
