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

// EmailAddressResetStateAvailable represents TL type `emailAddressResetStateAvailable#8dba3100`.
type EmailAddressResetStateAvailable struct {
	// Time required to wait before the email address can be reset; 0 if the user is
	// subscribed to Telegram Premium
	WaitPeriod int32
}

// EmailAddressResetStateAvailableTypeID is TL type id of EmailAddressResetStateAvailable.
const EmailAddressResetStateAvailableTypeID = 0x8dba3100

// construct implements constructor of EmailAddressResetStateClass.
func (e EmailAddressResetStateAvailable) construct() EmailAddressResetStateClass { return &e }

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

	_ EmailAddressResetStateClass = &EmailAddressResetStateAvailable{}
)

func (e *EmailAddressResetStateAvailable) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.WaitPeriod == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EmailAddressResetStateAvailable) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emailAddressResetStateAvailable",
		ID:   EmailAddressResetStateAvailableTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "WaitPeriod",
			SchemaName: "wait_period",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmailAddressResetStateAvailable) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressResetStateAvailable#8dba3100 as nil")
	}
	b.PutID(EmailAddressResetStateAvailableTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmailAddressResetStateAvailable) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressResetStateAvailable#8dba3100 as nil")
	}
	b.PutInt32(e.WaitPeriod)
	return nil
}

// Decode implements bin.Decoder.
func (e *EmailAddressResetStateAvailable) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressResetStateAvailable#8dba3100 to nil")
	}
	if err := b.ConsumeID(EmailAddressResetStateAvailableTypeID); err != nil {
		return fmt.Errorf("unable to decode emailAddressResetStateAvailable#8dba3100: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmailAddressResetStateAvailable) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressResetStateAvailable#8dba3100 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode emailAddressResetStateAvailable#8dba3100: field wait_period: %w", err)
		}
		e.WaitPeriod = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EmailAddressResetStateAvailable) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressResetStateAvailable#8dba3100 as nil")
	}
	b.ObjStart()
	b.PutID("emailAddressResetStateAvailable")
	b.Comma()
	b.FieldStart("wait_period")
	b.PutInt32(e.WaitPeriod)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EmailAddressResetStateAvailable) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressResetStateAvailable#8dba3100 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("emailAddressResetStateAvailable"); err != nil {
				return fmt.Errorf("unable to decode emailAddressResetStateAvailable#8dba3100: %w", err)
			}
		case "wait_period":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode emailAddressResetStateAvailable#8dba3100: field wait_period: %w", err)
			}
			e.WaitPeriod = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetWaitPeriod returns value of WaitPeriod field.
func (e *EmailAddressResetStateAvailable) GetWaitPeriod() (value int32) {
	if e == nil {
		return
	}
	return e.WaitPeriod
}

// EmailAddressResetStatePending represents TL type `emailAddressResetStatePending#8f966e2b`.
type EmailAddressResetStatePending struct {
	// Left time before the email address will be reset, in seconds. updateAuthorizationState
	// is not sent when this field changes
	ResetIn int32
}

// EmailAddressResetStatePendingTypeID is TL type id of EmailAddressResetStatePending.
const EmailAddressResetStatePendingTypeID = 0x8f966e2b

// construct implements constructor of EmailAddressResetStateClass.
func (e EmailAddressResetStatePending) construct() EmailAddressResetStateClass { return &e }

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

	_ EmailAddressResetStateClass = &EmailAddressResetStatePending{}
)

func (e *EmailAddressResetStatePending) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.ResetIn == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EmailAddressResetStatePending) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emailAddressResetStatePending",
		ID:   EmailAddressResetStatePendingTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ResetIn",
			SchemaName: "reset_in",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmailAddressResetStatePending) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressResetStatePending#8f966e2b as nil")
	}
	b.PutID(EmailAddressResetStatePendingTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmailAddressResetStatePending) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressResetStatePending#8f966e2b as nil")
	}
	b.PutInt32(e.ResetIn)
	return nil
}

// Decode implements bin.Decoder.
func (e *EmailAddressResetStatePending) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressResetStatePending#8f966e2b to nil")
	}
	if err := b.ConsumeID(EmailAddressResetStatePendingTypeID); err != nil {
		return fmt.Errorf("unable to decode emailAddressResetStatePending#8f966e2b: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmailAddressResetStatePending) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressResetStatePending#8f966e2b to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode emailAddressResetStatePending#8f966e2b: field reset_in: %w", err)
		}
		e.ResetIn = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EmailAddressResetStatePending) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode emailAddressResetStatePending#8f966e2b as nil")
	}
	b.ObjStart()
	b.PutID("emailAddressResetStatePending")
	b.Comma()
	b.FieldStart("reset_in")
	b.PutInt32(e.ResetIn)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EmailAddressResetStatePending) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode emailAddressResetStatePending#8f966e2b to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("emailAddressResetStatePending"); err != nil {
				return fmt.Errorf("unable to decode emailAddressResetStatePending#8f966e2b: %w", err)
			}
		case "reset_in":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode emailAddressResetStatePending#8f966e2b: field reset_in: %w", err)
			}
			e.ResetIn = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetResetIn returns value of ResetIn field.
func (e *EmailAddressResetStatePending) GetResetIn() (value int32) {
	if e == nil {
		return
	}
	return e.ResetIn
}

// EmailAddressResetStateClassName is schema name of EmailAddressResetStateClass.
const EmailAddressResetStateClassName = "EmailAddressResetState"

// EmailAddressResetStateClass represents EmailAddressResetState generic type.
//
// Example:
//
//	g, err := tdapi.DecodeEmailAddressResetState(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.EmailAddressResetStateAvailable: // emailAddressResetStateAvailable#8dba3100
//	case *tdapi.EmailAddressResetStatePending: // emailAddressResetStatePending#8f966e2b
//	default: panic(v)
//	}
type EmailAddressResetStateClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() EmailAddressResetStateClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeEmailAddressResetState implements binary de-serialization for EmailAddressResetStateClass.
func DecodeEmailAddressResetState(buf *bin.Buffer) (EmailAddressResetStateClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case EmailAddressResetStateAvailableTypeID:
		// Decoding emailAddressResetStateAvailable#8dba3100.
		v := EmailAddressResetStateAvailable{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmailAddressResetStateClass: %w", err)
		}
		return &v, nil
	case EmailAddressResetStatePendingTypeID:
		// Decoding emailAddressResetStatePending#8f966e2b.
		v := EmailAddressResetStatePending{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmailAddressResetStateClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode EmailAddressResetStateClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONEmailAddressResetState implements binary de-serialization for EmailAddressResetStateClass.
func DecodeTDLibJSONEmailAddressResetState(buf tdjson.Decoder) (EmailAddressResetStateClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "emailAddressResetStateAvailable":
		// Decoding emailAddressResetStateAvailable#8dba3100.
		v := EmailAddressResetStateAvailable{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmailAddressResetStateClass: %w", err)
		}
		return &v, nil
	case "emailAddressResetStatePending":
		// Decoding emailAddressResetStatePending#8f966e2b.
		v := EmailAddressResetStatePending{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmailAddressResetStateClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode EmailAddressResetStateClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// EmailAddressResetState boxes the EmailAddressResetStateClass providing a helper.
type EmailAddressResetStateBox struct {
	EmailAddressResetState EmailAddressResetStateClass
}

// Decode implements bin.Decoder for EmailAddressResetStateBox.
func (b *EmailAddressResetStateBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode EmailAddressResetStateBox to nil")
	}
	v, err := DecodeEmailAddressResetState(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.EmailAddressResetState = v
	return nil
}

// Encode implements bin.Encode for EmailAddressResetStateBox.
func (b *EmailAddressResetStateBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.EmailAddressResetState == nil {
		return fmt.Errorf("unable to encode EmailAddressResetStateClass as nil")
	}
	return b.EmailAddressResetState.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for EmailAddressResetStateBox.
func (b *EmailAddressResetStateBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode EmailAddressResetStateBox to nil")
	}
	v, err := DecodeTDLibJSONEmailAddressResetState(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.EmailAddressResetState = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for EmailAddressResetStateBox.
func (b *EmailAddressResetStateBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.EmailAddressResetState == nil {
		return fmt.Errorf("unable to encode EmailAddressResetStateClass as nil")
	}
	return b.EmailAddressResetState.EncodeTDLibJSON(buf)
}
