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

// AuthenticationCodeInfo represents TL type `authenticationCodeInfo#ccb82bb8`.
type AuthenticationCodeInfo struct {
	// A phone number that is being authenticated
	PhoneNumber string
	// The way the code was sent to the user
	Type AuthenticationCodeTypeClass
	// The way the next code will be sent to the user; may be null
	NextType AuthenticationCodeTypeClass
	// Timeout before the code can be re-sent, in seconds
	Timeout int32
}

// AuthenticationCodeInfoTypeID is TL type id of AuthenticationCodeInfo.
const AuthenticationCodeInfoTypeID = 0xccb82bb8

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

func (a *AuthenticationCodeInfo) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.PhoneNumber == "") {
		return false
	}
	if !(a.Type == nil) {
		return false
	}
	if !(a.NextType == nil) {
		return false
	}
	if !(a.Timeout == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AuthenticationCodeInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "authenticationCodeInfo",
		ID:   AuthenticationCodeInfoTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PhoneNumber",
			SchemaName: "phone_number",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "NextType",
			SchemaName: "next_type",
		},
		{
			Name:       "Timeout",
			SchemaName: "timeout",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AuthenticationCodeInfo) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode authenticationCodeInfo#ccb82bb8 as nil")
	}
	b.PutID(AuthenticationCodeInfoTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AuthenticationCodeInfo) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode authenticationCodeInfo#ccb82bb8 as nil")
	}
	b.PutString(a.PhoneNumber)
	if a.Type == nil {
		return fmt.Errorf("unable to encode authenticationCodeInfo#ccb82bb8: field type is nil")
	}
	if err := a.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode authenticationCodeInfo#ccb82bb8: field type: %w", err)
	}
	if a.NextType == nil {
		return fmt.Errorf("unable to encode authenticationCodeInfo#ccb82bb8: field next_type is nil")
	}
	if err := a.NextType.Encode(b); err != nil {
		return fmt.Errorf("unable to encode authenticationCodeInfo#ccb82bb8: field next_type: %w", err)
	}
	b.PutInt32(a.Timeout)
	return nil
}

// Decode implements bin.Decoder.
func (a *AuthenticationCodeInfo) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode authenticationCodeInfo#ccb82bb8 to nil")
	}
	if err := b.ConsumeID(AuthenticationCodeInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AuthenticationCodeInfo) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode authenticationCodeInfo#ccb82bb8 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: field phone_number: %w", err)
		}
		a.PhoneNumber = value
	}
	{
		value, err := DecodeAuthenticationCodeType(b)
		if err != nil {
			return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: field type: %w", err)
		}
		a.Type = value
	}
	{
		value, err := DecodeAuthenticationCodeType(b)
		if err != nil {
			return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: field next_type: %w", err)
		}
		a.NextType = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: field timeout: %w", err)
		}
		a.Timeout = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AuthenticationCodeInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode authenticationCodeInfo#ccb82bb8 as nil")
	}
	b.ObjStart()
	b.PutID("authenticationCodeInfo")
	b.Comma()
	b.FieldStart("phone_number")
	b.PutString(a.PhoneNumber)
	b.Comma()
	b.FieldStart("type")
	if a.Type == nil {
		return fmt.Errorf("unable to encode authenticationCodeInfo#ccb82bb8: field type is nil")
	}
	if err := a.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode authenticationCodeInfo#ccb82bb8: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("next_type")
	if a.NextType == nil {
		return fmt.Errorf("unable to encode authenticationCodeInfo#ccb82bb8: field next_type is nil")
	}
	if err := a.NextType.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode authenticationCodeInfo#ccb82bb8: field next_type: %w", err)
	}
	b.Comma()
	b.FieldStart("timeout")
	b.PutInt32(a.Timeout)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AuthenticationCodeInfo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode authenticationCodeInfo#ccb82bb8 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("authenticationCodeInfo"); err != nil {
				return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: %w", err)
			}
		case "phone_number":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: field phone_number: %w", err)
			}
			a.PhoneNumber = value
		case "type":
			value, err := DecodeTDLibJSONAuthenticationCodeType(b)
			if err != nil {
				return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: field type: %w", err)
			}
			a.Type = value
		case "next_type":
			value, err := DecodeTDLibJSONAuthenticationCodeType(b)
			if err != nil {
				return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: field next_type: %w", err)
			}
			a.NextType = value
		case "timeout":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode authenticationCodeInfo#ccb82bb8: field timeout: %w", err)
			}
			a.Timeout = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPhoneNumber returns value of PhoneNumber field.
func (a *AuthenticationCodeInfo) GetPhoneNumber() (value string) {
	if a == nil {
		return
	}
	return a.PhoneNumber
}

// GetType returns value of Type field.
func (a *AuthenticationCodeInfo) GetType() (value AuthenticationCodeTypeClass) {
	if a == nil {
		return
	}
	return a.Type
}

// GetNextType returns value of NextType field.
func (a *AuthenticationCodeInfo) GetNextType() (value AuthenticationCodeTypeClass) {
	if a == nil {
		return
	}
	return a.NextType
}

// GetTimeout returns value of Timeout field.
func (a *AuthenticationCodeInfo) GetTimeout() (value int32) {
	if a == nil {
		return
	}
	return a.Timeout
}
