// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// SecurePlainPhone represents TL type `securePlainPhone#7d6099dd`.
// Phone number to use in telegram passport¹: it must be verified, first »².
//
// Links:
//  1. https://core.telegram.org/passport
//  2. https://core.telegram.org/passport/encryption#secureplaindata
//
// See https://core.telegram.org/constructor/securePlainPhone for reference.
type SecurePlainPhone struct {
	// Phone number
	Phone string
}

// SecurePlainPhoneTypeID is TL type id of SecurePlainPhone.
const SecurePlainPhoneTypeID = 0x7d6099dd

// construct implements constructor of SecurePlainDataClass.
func (s SecurePlainPhone) construct() SecurePlainDataClass { return &s }

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

	_ SecurePlainDataClass = &SecurePlainPhone{}
)

func (s *SecurePlainPhone) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Phone == "") {
		return false
	}

	return true
}

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

// FillFrom fills SecurePlainPhone from given interface.
func (s *SecurePlainPhone) FillFrom(from interface {
	GetPhone() (value string)
}) {
	s.Phone = from.GetPhone()
}

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

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

// TypeInfo returns info about TL type.
func (s *SecurePlainPhone) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "securePlainPhone",
		ID:   SecurePlainPhoneTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Phone",
			SchemaName: "phone",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SecurePlainPhone) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePlainPhone#7d6099dd as nil")
	}
	b.PutID(SecurePlainPhoneTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SecurePlainPhone) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePlainPhone#7d6099dd as nil")
	}
	b.PutString(s.Phone)
	return nil
}

// Decode implements bin.Decoder.
func (s *SecurePlainPhone) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePlainPhone#7d6099dd to nil")
	}
	if err := b.ConsumeID(SecurePlainPhoneTypeID); err != nil {
		return fmt.Errorf("unable to decode securePlainPhone#7d6099dd: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SecurePlainPhone) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePlainPhone#7d6099dd to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode securePlainPhone#7d6099dd: field phone: %w", err)
		}
		s.Phone = value
	}
	return nil
}

// GetPhone returns value of Phone field.
func (s *SecurePlainPhone) GetPhone() (value string) {
	if s == nil {
		return
	}
	return s.Phone
}

// SecurePlainEmail represents TL type `securePlainEmail#21ec5a5f`.
// Email address to use in telegram passport¹: it must be verified, first »².
//
// Links:
//  1. https://core.telegram.org/passport
//  2. https://core.telegram.org/passport/encryption#secureplaindata
//
// See https://core.telegram.org/constructor/securePlainEmail for reference.
type SecurePlainEmail struct {
	// Email address
	Email string
}

// SecurePlainEmailTypeID is TL type id of SecurePlainEmail.
const SecurePlainEmailTypeID = 0x21ec5a5f

// construct implements constructor of SecurePlainDataClass.
func (s SecurePlainEmail) construct() SecurePlainDataClass { return &s }

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

	_ SecurePlainDataClass = &SecurePlainEmail{}
)

func (s *SecurePlainEmail) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Email == "") {
		return false
	}

	return true
}

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

// FillFrom fills SecurePlainEmail from given interface.
func (s *SecurePlainEmail) FillFrom(from interface {
	GetEmail() (value string)
}) {
	s.Email = from.GetEmail()
}

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

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

// TypeInfo returns info about TL type.
func (s *SecurePlainEmail) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "securePlainEmail",
		ID:   SecurePlainEmailTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Email",
			SchemaName: "email",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SecurePlainEmail) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePlainEmail#21ec5a5f as nil")
	}
	b.PutID(SecurePlainEmailTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SecurePlainEmail) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePlainEmail#21ec5a5f as nil")
	}
	b.PutString(s.Email)
	return nil
}

// Decode implements bin.Decoder.
func (s *SecurePlainEmail) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePlainEmail#21ec5a5f to nil")
	}
	if err := b.ConsumeID(SecurePlainEmailTypeID); err != nil {
		return fmt.Errorf("unable to decode securePlainEmail#21ec5a5f: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SecurePlainEmail) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePlainEmail#21ec5a5f to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode securePlainEmail#21ec5a5f: field email: %w", err)
		}
		s.Email = value
	}
	return nil
}

// GetEmail returns value of Email field.
func (s *SecurePlainEmail) GetEmail() (value string) {
	if s == nil {
		return
	}
	return s.Email
}

// SecurePlainDataClassName is schema name of SecurePlainDataClass.
const SecurePlainDataClassName = "SecurePlainData"

// SecurePlainDataClass represents SecurePlainData generic type.
//
// See https://core.telegram.org/type/SecurePlainData for reference.
//
// Example:
//
//	g, err := tg.DecodeSecurePlainData(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.SecurePlainPhone: // securePlainPhone#7d6099dd
//	case *tg.SecurePlainEmail: // securePlainEmail#21ec5a5f
//	default: panic(v)
//	}
type SecurePlainDataClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() SecurePlainDataClass

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

// DecodeSecurePlainData implements binary de-serialization for SecurePlainDataClass.
func DecodeSecurePlainData(buf *bin.Buffer) (SecurePlainDataClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case SecurePlainPhoneTypeID:
		// Decoding securePlainPhone#7d6099dd.
		v := SecurePlainPhone{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SecurePlainDataClass: %w", err)
		}
		return &v, nil
	case SecurePlainEmailTypeID:
		// Decoding securePlainEmail#21ec5a5f.
		v := SecurePlainEmail{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SecurePlainDataClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode SecurePlainDataClass: %w", bin.NewUnexpectedID(id))
	}
}

// SecurePlainData boxes the SecurePlainDataClass providing a helper.
type SecurePlainDataBox struct {
	SecurePlainData SecurePlainDataClass
}

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

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