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

// InputPassportElementError represents TL type `inputPassportElementError#11084de2`.
type InputPassportElementError struct {
	// Type of Telegram Passport element that has the error
	Type PassportElementTypeClass
	// Error message
	Message string
	// Error source
	Source InputPassportElementErrorSourceClass
}

// InputPassportElementErrorTypeID is TL type id of InputPassportElementError.
const InputPassportElementErrorTypeID = 0x11084de2

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

func (i *InputPassportElementError) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Type == nil) {
		return false
	}
	if !(i.Message == "") {
		return false
	}
	if !(i.Source == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InputPassportElementError) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputPassportElementError",
		ID:   InputPassportElementErrorTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "Message",
			SchemaName: "message",
		},
		{
			Name:       "Source",
			SchemaName: "source",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputPassportElementError) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPassportElementError#11084de2 as nil")
	}
	b.PutID(InputPassportElementErrorTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputPassportElementError) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPassportElementError#11084de2 as nil")
	}
	if i.Type == nil {
		return fmt.Errorf("unable to encode inputPassportElementError#11084de2: field type is nil")
	}
	if err := i.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputPassportElementError#11084de2: field type: %w", err)
	}
	b.PutString(i.Message)
	if i.Source == nil {
		return fmt.Errorf("unable to encode inputPassportElementError#11084de2: field source is nil")
	}
	if err := i.Source.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputPassportElementError#11084de2: field source: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InputPassportElementError) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPassportElementError#11084de2 to nil")
	}
	if err := b.ConsumeID(InputPassportElementErrorTypeID); err != nil {
		return fmt.Errorf("unable to decode inputPassportElementError#11084de2: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputPassportElementError) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPassportElementError#11084de2 to nil")
	}
	{
		value, err := DecodePassportElementType(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputPassportElementError#11084de2: field type: %w", err)
		}
		i.Type = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputPassportElementError#11084de2: field message: %w", err)
		}
		i.Message = value
	}
	{
		value, err := DecodeInputPassportElementErrorSource(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputPassportElementError#11084de2: field source: %w", err)
		}
		i.Source = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InputPassportElementError) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPassportElementError#11084de2 as nil")
	}
	b.ObjStart()
	b.PutID("inputPassportElementError")
	b.Comma()
	b.FieldStart("type")
	if i.Type == nil {
		return fmt.Errorf("unable to encode inputPassportElementError#11084de2: field type is nil")
	}
	if err := i.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputPassportElementError#11084de2: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("message")
	b.PutString(i.Message)
	b.Comma()
	b.FieldStart("source")
	if i.Source == nil {
		return fmt.Errorf("unable to encode inputPassportElementError#11084de2: field source is nil")
	}
	if err := i.Source.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputPassportElementError#11084de2: field source: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InputPassportElementError) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPassportElementError#11084de2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inputPassportElementError"); err != nil {
				return fmt.Errorf("unable to decode inputPassportElementError#11084de2: %w", err)
			}
		case "type":
			value, err := DecodeTDLibJSONPassportElementType(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputPassportElementError#11084de2: field type: %w", err)
			}
			i.Type = value
		case "message":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode inputPassportElementError#11084de2: field message: %w", err)
			}
			i.Message = value
		case "source":
			value, err := DecodeTDLibJSONInputPassportElementErrorSource(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputPassportElementError#11084de2: field source: %w", err)
			}
			i.Source = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (i *InputPassportElementError) GetType() (value PassportElementTypeClass) {
	if i == nil {
		return
	}
	return i.Type
}

// GetMessage returns value of Message field.
func (i *InputPassportElementError) GetMessage() (value string) {
	if i == nil {
		return
	}
	return i.Message
}

// GetSource returns value of Source field.
func (i *InputPassportElementError) GetSource() (value InputPassportElementErrorSourceClass) {
	if i == nil {
		return
	}
	return i.Source
}
