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

// FirebaseDeviceVerificationParametersSafetyNet represents TL type `firebaseDeviceVerificationParametersSafetyNet#2b96b2f1`.
type FirebaseDeviceVerificationParametersSafetyNet struct {
	// Nonce to pass to the SafetyNet Attestation API
	Nonce []byte
}

// FirebaseDeviceVerificationParametersSafetyNetTypeID is TL type id of FirebaseDeviceVerificationParametersSafetyNet.
const FirebaseDeviceVerificationParametersSafetyNetTypeID = 0x2b96b2f1

// construct implements constructor of FirebaseDeviceVerificationParametersClass.
func (f FirebaseDeviceVerificationParametersSafetyNet) construct() FirebaseDeviceVerificationParametersClass {
	return &f
}

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

	_ FirebaseDeviceVerificationParametersClass = &FirebaseDeviceVerificationParametersSafetyNet{}
)

func (f *FirebaseDeviceVerificationParametersSafetyNet) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Nonce == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FirebaseDeviceVerificationParametersSafetyNet) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "firebaseDeviceVerificationParametersSafetyNet",
		ID:   FirebaseDeviceVerificationParametersSafetyNetTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Nonce",
			SchemaName: "nonce",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FirebaseDeviceVerificationParametersSafetyNet) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1 as nil")
	}
	b.PutID(FirebaseDeviceVerificationParametersSafetyNetTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FirebaseDeviceVerificationParametersSafetyNet) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1 as nil")
	}
	b.PutBytes(f.Nonce)
	return nil
}

// Decode implements bin.Decoder.
func (f *FirebaseDeviceVerificationParametersSafetyNet) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1 to nil")
	}
	if err := b.ConsumeID(FirebaseDeviceVerificationParametersSafetyNetTypeID); err != nil {
		return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FirebaseDeviceVerificationParametersSafetyNet) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1 to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1: field nonce: %w", err)
		}
		f.Nonce = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FirebaseDeviceVerificationParametersSafetyNet) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1 as nil")
	}
	b.ObjStart()
	b.PutID("firebaseDeviceVerificationParametersSafetyNet")
	b.Comma()
	b.FieldStart("nonce")
	b.PutBytes(f.Nonce)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *FirebaseDeviceVerificationParametersSafetyNet) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("firebaseDeviceVerificationParametersSafetyNet"); err != nil {
				return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1: %w", err)
			}
		case "nonce":
			value, err := b.Bytes()
			if err != nil {
				return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersSafetyNet#2b96b2f1: field nonce: %w", err)
			}
			f.Nonce = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetNonce returns value of Nonce field.
func (f *FirebaseDeviceVerificationParametersSafetyNet) GetNonce() (value []byte) {
	if f == nil {
		return
	}
	return f.Nonce
}

// FirebaseDeviceVerificationParametersPlayIntegrity represents TL type `firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a`.
type FirebaseDeviceVerificationParametersPlayIntegrity struct {
	// Base64url-encoded nonce to pass to the Play Integrity API
	Nonce string
	// Cloud project number to pass to the Play Integrity API
	CloudProjectNumber int64
}

// FirebaseDeviceVerificationParametersPlayIntegrityTypeID is TL type id of FirebaseDeviceVerificationParametersPlayIntegrity.
const FirebaseDeviceVerificationParametersPlayIntegrityTypeID = 0xcaf4a58a

// construct implements constructor of FirebaseDeviceVerificationParametersClass.
func (f FirebaseDeviceVerificationParametersPlayIntegrity) construct() FirebaseDeviceVerificationParametersClass {
	return &f
}

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

	_ FirebaseDeviceVerificationParametersClass = &FirebaseDeviceVerificationParametersPlayIntegrity{}
)

func (f *FirebaseDeviceVerificationParametersPlayIntegrity) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Nonce == "") {
		return false
	}
	if !(f.CloudProjectNumber == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "firebaseDeviceVerificationParametersPlayIntegrity",
		ID:   FirebaseDeviceVerificationParametersPlayIntegrityTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Nonce",
			SchemaName: "nonce",
		},
		{
			Name:       "CloudProjectNumber",
			SchemaName: "cloud_project_number",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a as nil")
	}
	b.PutID(FirebaseDeviceVerificationParametersPlayIntegrityTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a as nil")
	}
	b.PutString(f.Nonce)
	b.PutLong(f.CloudProjectNumber)
	return nil
}

// Decode implements bin.Decoder.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a to nil")
	}
	if err := b.ConsumeID(FirebaseDeviceVerificationParametersPlayIntegrityTypeID); err != nil {
		return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a: field nonce: %w", err)
		}
		f.Nonce = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a: field cloud_project_number: %w", err)
		}
		f.CloudProjectNumber = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a as nil")
	}
	b.ObjStart()
	b.PutID("firebaseDeviceVerificationParametersPlayIntegrity")
	b.Comma()
	b.FieldStart("nonce")
	b.PutString(f.Nonce)
	b.Comma()
	b.FieldStart("cloud_project_number")
	b.PutLong(f.CloudProjectNumber)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("firebaseDeviceVerificationParametersPlayIntegrity"); err != nil {
				return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a: %w", err)
			}
		case "nonce":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a: field nonce: %w", err)
			}
			f.Nonce = value
		case "cloud_project_number":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a: field cloud_project_number: %w", err)
			}
			f.CloudProjectNumber = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetNonce returns value of Nonce field.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) GetNonce() (value string) {
	if f == nil {
		return
	}
	return f.Nonce
}

// GetCloudProjectNumber returns value of CloudProjectNumber field.
func (f *FirebaseDeviceVerificationParametersPlayIntegrity) GetCloudProjectNumber() (value int64) {
	if f == nil {
		return
	}
	return f.CloudProjectNumber
}

// FirebaseDeviceVerificationParametersClassName is schema name of FirebaseDeviceVerificationParametersClass.
const FirebaseDeviceVerificationParametersClassName = "FirebaseDeviceVerificationParameters"

// FirebaseDeviceVerificationParametersClass represents FirebaseDeviceVerificationParameters generic type.
//
// Example:
//
//	g, err := tdapi.DecodeFirebaseDeviceVerificationParameters(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.FirebaseDeviceVerificationParametersSafetyNet: // firebaseDeviceVerificationParametersSafetyNet#2b96b2f1
//	case *tdapi.FirebaseDeviceVerificationParametersPlayIntegrity: // firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a
//	default: panic(v)
//	}
type FirebaseDeviceVerificationParametersClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() FirebaseDeviceVerificationParametersClass

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

// DecodeFirebaseDeviceVerificationParameters implements binary de-serialization for FirebaseDeviceVerificationParametersClass.
func DecodeFirebaseDeviceVerificationParameters(buf *bin.Buffer) (FirebaseDeviceVerificationParametersClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case FirebaseDeviceVerificationParametersSafetyNetTypeID:
		// Decoding firebaseDeviceVerificationParametersSafetyNet#2b96b2f1.
		v := FirebaseDeviceVerificationParametersSafetyNet{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode FirebaseDeviceVerificationParametersClass: %w", err)
		}
		return &v, nil
	case FirebaseDeviceVerificationParametersPlayIntegrityTypeID:
		// Decoding firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a.
		v := FirebaseDeviceVerificationParametersPlayIntegrity{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode FirebaseDeviceVerificationParametersClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode FirebaseDeviceVerificationParametersClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONFirebaseDeviceVerificationParameters implements binary de-serialization for FirebaseDeviceVerificationParametersClass.
func DecodeTDLibJSONFirebaseDeviceVerificationParameters(buf tdjson.Decoder) (FirebaseDeviceVerificationParametersClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "firebaseDeviceVerificationParametersSafetyNet":
		// Decoding firebaseDeviceVerificationParametersSafetyNet#2b96b2f1.
		v := FirebaseDeviceVerificationParametersSafetyNet{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode FirebaseDeviceVerificationParametersClass: %w", err)
		}
		return &v, nil
	case "firebaseDeviceVerificationParametersPlayIntegrity":
		// Decoding firebaseDeviceVerificationParametersPlayIntegrity#caf4a58a.
		v := FirebaseDeviceVerificationParametersPlayIntegrity{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode FirebaseDeviceVerificationParametersClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode FirebaseDeviceVerificationParametersClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// FirebaseDeviceVerificationParameters boxes the FirebaseDeviceVerificationParametersClass providing a helper.
type FirebaseDeviceVerificationParametersBox struct {
	FirebaseDeviceVerificationParameters FirebaseDeviceVerificationParametersClass
}

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

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

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

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