// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// InputPasskeyCredentialPublicKey represents TL type `inputPasskeyCredentialPublicKey#3c27b78f`.
//
// See https://core.telegram.org/constructor/inputPasskeyCredentialPublicKey for reference.
type InputPasskeyCredentialPublicKey struct {
	// ID field of InputPasskeyCredentialPublicKey.
	ID string
	// RawID field of InputPasskeyCredentialPublicKey.
	RawID string
	// Response field of InputPasskeyCredentialPublicKey.
	Response InputPasskeyResponseClass
}

// InputPasskeyCredentialPublicKeyTypeID is TL type id of InputPasskeyCredentialPublicKey.
const InputPasskeyCredentialPublicKeyTypeID = 0x3c27b78f

// construct implements constructor of InputPasskeyCredentialClass.
func (i InputPasskeyCredentialPublicKey) construct() InputPasskeyCredentialClass { return &i }

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

	_ InputPasskeyCredentialClass = &InputPasskeyCredentialPublicKey{}
)

func (i *InputPasskeyCredentialPublicKey) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.ID == "") {
		return false
	}
	if !(i.RawID == "") {
		return false
	}
	if !(i.Response == nil) {
		return false
	}

	return true
}

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

// FillFrom fills InputPasskeyCredentialPublicKey from given interface.
func (i *InputPasskeyCredentialPublicKey) FillFrom(from interface {
	GetID() (value string)
	GetRawID() (value string)
	GetResponse() (value InputPasskeyResponseClass)
}) {
	i.ID = from.GetID()
	i.RawID = from.GetRawID()
	i.Response = from.GetResponse()
}

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

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

// TypeInfo returns info about TL type.
func (i *InputPasskeyCredentialPublicKey) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputPasskeyCredentialPublicKey",
		ID:   InputPasskeyCredentialPublicKeyTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "RawID",
			SchemaName: "raw_id",
		},
		{
			Name:       "Response",
			SchemaName: "response",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputPasskeyCredentialPublicKey) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPasskeyCredentialPublicKey#3c27b78f as nil")
	}
	b.PutID(InputPasskeyCredentialPublicKeyTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputPasskeyCredentialPublicKey) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPasskeyCredentialPublicKey#3c27b78f as nil")
	}
	b.PutString(i.ID)
	b.PutString(i.RawID)
	if i.Response == nil {
		return fmt.Errorf("unable to encode inputPasskeyCredentialPublicKey#3c27b78f: field response is nil")
	}
	if err := i.Response.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputPasskeyCredentialPublicKey#3c27b78f: field response: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InputPasskeyCredentialPublicKey) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPasskeyCredentialPublicKey#3c27b78f to nil")
	}
	if err := b.ConsumeID(InputPasskeyCredentialPublicKeyTypeID); err != nil {
		return fmt.Errorf("unable to decode inputPasskeyCredentialPublicKey#3c27b78f: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputPasskeyCredentialPublicKey) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPasskeyCredentialPublicKey#3c27b78f to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputPasskeyCredentialPublicKey#3c27b78f: field id: %w", err)
		}
		i.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputPasskeyCredentialPublicKey#3c27b78f: field raw_id: %w", err)
		}
		i.RawID = value
	}
	{
		value, err := DecodeInputPasskeyResponse(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputPasskeyCredentialPublicKey#3c27b78f: field response: %w", err)
		}
		i.Response = value
	}
	return nil
}

// GetID returns value of ID field.
func (i *InputPasskeyCredentialPublicKey) GetID() (value string) {
	if i == nil {
		return
	}
	return i.ID
}

// GetRawID returns value of RawID field.
func (i *InputPasskeyCredentialPublicKey) GetRawID() (value string) {
	if i == nil {
		return
	}
	return i.RawID
}

// GetResponse returns value of Response field.
func (i *InputPasskeyCredentialPublicKey) GetResponse() (value InputPasskeyResponseClass) {
	if i == nil {
		return
	}
	return i.Response
}

// InputPasskeyCredentialFirebasePNV represents TL type `inputPasskeyCredentialFirebasePNV#5b1ccb28`.
//
// See https://core.telegram.org/constructor/inputPasskeyCredentialFirebasePNV for reference.
type InputPasskeyCredentialFirebasePNV struct {
	// PnvToken field of InputPasskeyCredentialFirebasePNV.
	PnvToken string
}

// InputPasskeyCredentialFirebasePNVTypeID is TL type id of InputPasskeyCredentialFirebasePNV.
const InputPasskeyCredentialFirebasePNVTypeID = 0x5b1ccb28

// construct implements constructor of InputPasskeyCredentialClass.
func (i InputPasskeyCredentialFirebasePNV) construct() InputPasskeyCredentialClass { return &i }

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

	_ InputPasskeyCredentialClass = &InputPasskeyCredentialFirebasePNV{}
)

func (i *InputPasskeyCredentialFirebasePNV) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.PnvToken == "") {
		return false
	}

	return true
}

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

// FillFrom fills InputPasskeyCredentialFirebasePNV from given interface.
func (i *InputPasskeyCredentialFirebasePNV) FillFrom(from interface {
	GetPnvToken() (value string)
}) {
	i.PnvToken = from.GetPnvToken()
}

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

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

// TypeInfo returns info about TL type.
func (i *InputPasskeyCredentialFirebasePNV) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputPasskeyCredentialFirebasePNV",
		ID:   InputPasskeyCredentialFirebasePNVTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PnvToken",
			SchemaName: "pnv_token",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputPasskeyCredentialFirebasePNV) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPasskeyCredentialFirebasePNV#5b1ccb28 as nil")
	}
	b.PutID(InputPasskeyCredentialFirebasePNVTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputPasskeyCredentialFirebasePNV) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPasskeyCredentialFirebasePNV#5b1ccb28 as nil")
	}
	b.PutString(i.PnvToken)
	return nil
}

// Decode implements bin.Decoder.
func (i *InputPasskeyCredentialFirebasePNV) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPasskeyCredentialFirebasePNV#5b1ccb28 to nil")
	}
	if err := b.ConsumeID(InputPasskeyCredentialFirebasePNVTypeID); err != nil {
		return fmt.Errorf("unable to decode inputPasskeyCredentialFirebasePNV#5b1ccb28: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputPasskeyCredentialFirebasePNV) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPasskeyCredentialFirebasePNV#5b1ccb28 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputPasskeyCredentialFirebasePNV#5b1ccb28: field pnv_token: %w", err)
		}
		i.PnvToken = value
	}
	return nil
}

// GetPnvToken returns value of PnvToken field.
func (i *InputPasskeyCredentialFirebasePNV) GetPnvToken() (value string) {
	if i == nil {
		return
	}
	return i.PnvToken
}

// InputPasskeyCredentialClassName is schema name of InputPasskeyCredentialClass.
const InputPasskeyCredentialClassName = "InputPasskeyCredential"

// InputPasskeyCredentialClass represents InputPasskeyCredential generic type.
//
// See https://core.telegram.org/type/InputPasskeyCredential for reference.
//
// Example:
//
//	g, err := tg.DecodeInputPasskeyCredential(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.InputPasskeyCredentialPublicKey: // inputPasskeyCredentialPublicKey#3c27b78f
//	case *tg.InputPasskeyCredentialFirebasePNV: // inputPasskeyCredentialFirebasePNV#5b1ccb28
//	default: panic(v)
//	}
type InputPasskeyCredentialClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() InputPasskeyCredentialClass

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

// DecodeInputPasskeyCredential implements binary de-serialization for InputPasskeyCredentialClass.
func DecodeInputPasskeyCredential(buf *bin.Buffer) (InputPasskeyCredentialClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case InputPasskeyCredentialPublicKeyTypeID:
		// Decoding inputPasskeyCredentialPublicKey#3c27b78f.
		v := InputPasskeyCredentialPublicKey{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode InputPasskeyCredentialClass: %w", err)
		}
		return &v, nil
	case InputPasskeyCredentialFirebasePNVTypeID:
		// Decoding inputPasskeyCredentialFirebasePNV#5b1ccb28.
		v := InputPasskeyCredentialFirebasePNV{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode InputPasskeyCredentialClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode InputPasskeyCredentialClass: %w", bin.NewUnexpectedID(id))
	}
}

// InputPasskeyCredential boxes the InputPasskeyCredentialClass providing a helper.
type InputPasskeyCredentialBox struct {
	InputPasskeyCredential InputPasskeyCredentialClass
}

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

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