// Code generated by gotdgen, DO NOT EDIT.

package mt

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

// DhGenOk represents TL type `dh_gen_ok#3bcbf734`.
type DhGenOk struct {
	// Nonce field of DhGenOk.
	Nonce bin.Int128
	// ServerNonce field of DhGenOk.
	ServerNonce bin.Int128
	// NewNonceHash1 field of DhGenOk.
	NewNonceHash1 bin.Int128
}

// DhGenOkTypeID is TL type id of DhGenOk.
const DhGenOkTypeID = 0x3bcbf734

// construct implements constructor of SetClientDHParamsAnswerClass.
func (d DhGenOk) construct() SetClientDHParamsAnswerClass { return &d }

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

	_ SetClientDHParamsAnswerClass = &DhGenOk{}
)

func (d *DhGenOk) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Nonce == bin.Int128{}) {
		return false
	}
	if !(d.ServerNonce == bin.Int128{}) {
		return false
	}
	if !(d.NewNonceHash1 == bin.Int128{}) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DhGenOk) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "dh_gen_ok",
		ID:   DhGenOkTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Nonce",
			SchemaName: "nonce",
		},
		{
			Name:       "ServerNonce",
			SchemaName: "server_nonce",
		},
		{
			Name:       "NewNonceHash1",
			SchemaName: "new_nonce_hash1",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DhGenOk) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode dh_gen_ok#3bcbf734 as nil")
	}
	b.PutID(DhGenOkTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DhGenOk) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode dh_gen_ok#3bcbf734 as nil")
	}
	b.PutInt128(d.Nonce)
	b.PutInt128(d.ServerNonce)
	b.PutInt128(d.NewNonceHash1)
	return nil
}

// Decode implements bin.Decoder.
func (d *DhGenOk) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode dh_gen_ok#3bcbf734 to nil")
	}
	if err := b.ConsumeID(DhGenOkTypeID); err != nil {
		return fmt.Errorf("unable to decode dh_gen_ok#3bcbf734: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DhGenOk) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode dh_gen_ok#3bcbf734 to nil")
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_ok#3bcbf734: field nonce: %w", err)
		}
		d.Nonce = value
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_ok#3bcbf734: field server_nonce: %w", err)
		}
		d.ServerNonce = value
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_ok#3bcbf734: field new_nonce_hash1: %w", err)
		}
		d.NewNonceHash1 = value
	}
	return nil
}

// GetNonce returns value of Nonce field.
func (d *DhGenOk) GetNonce() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.Nonce
}

// GetServerNonce returns value of ServerNonce field.
func (d *DhGenOk) GetServerNonce() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.ServerNonce
}

// GetNewNonceHash1 returns value of NewNonceHash1 field.
func (d *DhGenOk) GetNewNonceHash1() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.NewNonceHash1
}

// DhGenRetry represents TL type `dh_gen_retry#46dc1fb9`.
type DhGenRetry struct {
	// Nonce field of DhGenRetry.
	Nonce bin.Int128
	// ServerNonce field of DhGenRetry.
	ServerNonce bin.Int128
	// NewNonceHash2 field of DhGenRetry.
	NewNonceHash2 bin.Int128
}

// DhGenRetryTypeID is TL type id of DhGenRetry.
const DhGenRetryTypeID = 0x46dc1fb9

// construct implements constructor of SetClientDHParamsAnswerClass.
func (d DhGenRetry) construct() SetClientDHParamsAnswerClass { return &d }

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

	_ SetClientDHParamsAnswerClass = &DhGenRetry{}
)

func (d *DhGenRetry) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Nonce == bin.Int128{}) {
		return false
	}
	if !(d.ServerNonce == bin.Int128{}) {
		return false
	}
	if !(d.NewNonceHash2 == bin.Int128{}) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DhGenRetry) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "dh_gen_retry",
		ID:   DhGenRetryTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Nonce",
			SchemaName: "nonce",
		},
		{
			Name:       "ServerNonce",
			SchemaName: "server_nonce",
		},
		{
			Name:       "NewNonceHash2",
			SchemaName: "new_nonce_hash2",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DhGenRetry) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode dh_gen_retry#46dc1fb9 as nil")
	}
	b.PutID(DhGenRetryTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DhGenRetry) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode dh_gen_retry#46dc1fb9 as nil")
	}
	b.PutInt128(d.Nonce)
	b.PutInt128(d.ServerNonce)
	b.PutInt128(d.NewNonceHash2)
	return nil
}

// Decode implements bin.Decoder.
func (d *DhGenRetry) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode dh_gen_retry#46dc1fb9 to nil")
	}
	if err := b.ConsumeID(DhGenRetryTypeID); err != nil {
		return fmt.Errorf("unable to decode dh_gen_retry#46dc1fb9: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DhGenRetry) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode dh_gen_retry#46dc1fb9 to nil")
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_retry#46dc1fb9: field nonce: %w", err)
		}
		d.Nonce = value
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_retry#46dc1fb9: field server_nonce: %w", err)
		}
		d.ServerNonce = value
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_retry#46dc1fb9: field new_nonce_hash2: %w", err)
		}
		d.NewNonceHash2 = value
	}
	return nil
}

// GetNonce returns value of Nonce field.
func (d *DhGenRetry) GetNonce() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.Nonce
}

// GetServerNonce returns value of ServerNonce field.
func (d *DhGenRetry) GetServerNonce() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.ServerNonce
}

// GetNewNonceHash2 returns value of NewNonceHash2 field.
func (d *DhGenRetry) GetNewNonceHash2() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.NewNonceHash2
}

// DhGenFail represents TL type `dh_gen_fail#a69dae02`.
type DhGenFail struct {
	// Nonce field of DhGenFail.
	Nonce bin.Int128
	// ServerNonce field of DhGenFail.
	ServerNonce bin.Int128
	// NewNonceHash3 field of DhGenFail.
	NewNonceHash3 bin.Int128
}

// DhGenFailTypeID is TL type id of DhGenFail.
const DhGenFailTypeID = 0xa69dae02

// construct implements constructor of SetClientDHParamsAnswerClass.
func (d DhGenFail) construct() SetClientDHParamsAnswerClass { return &d }

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

	_ SetClientDHParamsAnswerClass = &DhGenFail{}
)

func (d *DhGenFail) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Nonce == bin.Int128{}) {
		return false
	}
	if !(d.ServerNonce == bin.Int128{}) {
		return false
	}
	if !(d.NewNonceHash3 == bin.Int128{}) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DhGenFail) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "dh_gen_fail",
		ID:   DhGenFailTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Nonce",
			SchemaName: "nonce",
		},
		{
			Name:       "ServerNonce",
			SchemaName: "server_nonce",
		},
		{
			Name:       "NewNonceHash3",
			SchemaName: "new_nonce_hash3",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DhGenFail) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode dh_gen_fail#a69dae02 as nil")
	}
	b.PutID(DhGenFailTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DhGenFail) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode dh_gen_fail#a69dae02 as nil")
	}
	b.PutInt128(d.Nonce)
	b.PutInt128(d.ServerNonce)
	b.PutInt128(d.NewNonceHash3)
	return nil
}

// Decode implements bin.Decoder.
func (d *DhGenFail) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode dh_gen_fail#a69dae02 to nil")
	}
	if err := b.ConsumeID(DhGenFailTypeID); err != nil {
		return fmt.Errorf("unable to decode dh_gen_fail#a69dae02: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DhGenFail) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode dh_gen_fail#a69dae02 to nil")
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_fail#a69dae02: field nonce: %w", err)
		}
		d.Nonce = value
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_fail#a69dae02: field server_nonce: %w", err)
		}
		d.ServerNonce = value
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode dh_gen_fail#a69dae02: field new_nonce_hash3: %w", err)
		}
		d.NewNonceHash3 = value
	}
	return nil
}

// GetNonce returns value of Nonce field.
func (d *DhGenFail) GetNonce() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.Nonce
}

// GetServerNonce returns value of ServerNonce field.
func (d *DhGenFail) GetServerNonce() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.ServerNonce
}

// GetNewNonceHash3 returns value of NewNonceHash3 field.
func (d *DhGenFail) GetNewNonceHash3() (value bin.Int128) {
	if d == nil {
		return
	}
	return d.NewNonceHash3
}

// SetClientDHParamsAnswerClassName is schema name of SetClientDHParamsAnswerClass.
const SetClientDHParamsAnswerClassName = "Set_client_DH_params_answer"

// SetClientDHParamsAnswerClass represents Set_client_DH_params_answer generic type.
//
// Example:
//
//	g, err := mt.DecodeSetClientDHParamsAnswer(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *mt.DhGenOk: // dh_gen_ok#3bcbf734
//	case *mt.DhGenRetry: // dh_gen_retry#46dc1fb9
//	case *mt.DhGenFail: // dh_gen_fail#a69dae02
//	default: panic(v)
//	}
type SetClientDHParamsAnswerClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() SetClientDHParamsAnswerClass

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

	// Nonce field of DhGenOk.
	GetNonce() (value bin.Int128)
	// ServerNonce field of DhGenOk.
	GetServerNonce() (value bin.Int128)
}

// DecodeSetClientDHParamsAnswer implements binary de-serialization for SetClientDHParamsAnswerClass.
func DecodeSetClientDHParamsAnswer(buf *bin.Buffer) (SetClientDHParamsAnswerClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case DhGenOkTypeID:
		// Decoding dh_gen_ok#3bcbf734.
		v := DhGenOk{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SetClientDHParamsAnswerClass: %w", err)
		}
		return &v, nil
	case DhGenRetryTypeID:
		// Decoding dh_gen_retry#46dc1fb9.
		v := DhGenRetry{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SetClientDHParamsAnswerClass: %w", err)
		}
		return &v, nil
	case DhGenFailTypeID:
		// Decoding dh_gen_fail#a69dae02.
		v := DhGenFail{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SetClientDHParamsAnswerClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode SetClientDHParamsAnswerClass: %w", bin.NewUnexpectedID(id))
	}
}

// SetClientDHParamsAnswer boxes the SetClientDHParamsAnswerClass providing a helper.
type SetClientDHParamsAnswerBox struct {
	Set_client_DH_params_answer SetClientDHParamsAnswerClass
}

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

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