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

// InputIdentityDocument represents TL type `inputIdentityDocument#185f2052`.
type InputIdentityDocument struct {
	// Document number; 1-24 characters
	Number string
	// Document expiration date; pass null if not applicable
	ExpirationDate Date
	// Front side of the document
	FrontSide InputFileClass
	// Reverse side of the document; only for driver license and identity card; pass null
	// otherwise
	ReverseSide InputFileClass
	// Selfie with the document; pass null if unavailable
	Selfie InputFileClass
	// List of files containing a certified English translation of the document
	Translation []InputFileClass
}

// InputIdentityDocumentTypeID is TL type id of InputIdentityDocument.
const InputIdentityDocumentTypeID = 0x185f2052

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

func (i *InputIdentityDocument) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Number == "") {
		return false
	}
	if !(i.ExpirationDate.Zero()) {
		return false
	}
	if !(i.FrontSide == nil) {
		return false
	}
	if !(i.ReverseSide == nil) {
		return false
	}
	if !(i.Selfie == nil) {
		return false
	}
	if !(i.Translation == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InputIdentityDocument) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputIdentityDocument",
		ID:   InputIdentityDocumentTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Number",
			SchemaName: "number",
		},
		{
			Name:       "ExpirationDate",
			SchemaName: "expiration_date",
		},
		{
			Name:       "FrontSide",
			SchemaName: "front_side",
		},
		{
			Name:       "ReverseSide",
			SchemaName: "reverse_side",
		},
		{
			Name:       "Selfie",
			SchemaName: "selfie",
		},
		{
			Name:       "Translation",
			SchemaName: "translation",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputIdentityDocument) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputIdentityDocument#185f2052 as nil")
	}
	b.PutID(InputIdentityDocumentTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputIdentityDocument) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputIdentityDocument#185f2052 as nil")
	}
	b.PutString(i.Number)
	if err := i.ExpirationDate.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field expiration_date: %w", err)
	}
	if i.FrontSide == nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field front_side is nil")
	}
	if err := i.FrontSide.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field front_side: %w", err)
	}
	if i.ReverseSide == nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field reverse_side is nil")
	}
	if err := i.ReverseSide.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field reverse_side: %w", err)
	}
	if i.Selfie == nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field selfie is nil")
	}
	if err := i.Selfie.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field selfie: %w", err)
	}
	b.PutInt(len(i.Translation))
	for idx, v := range i.Translation {
		if v == nil {
			return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field translation element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare inputIdentityDocument#185f2052: field translation element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InputIdentityDocument) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputIdentityDocument#185f2052 to nil")
	}
	if err := b.ConsumeID(InputIdentityDocumentTypeID); err != nil {
		return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputIdentityDocument) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputIdentityDocument#185f2052 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field number: %w", err)
		}
		i.Number = value
	}
	{
		if err := i.ExpirationDate.Decode(b); err != nil {
			return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field expiration_date: %w", err)
		}
	}
	{
		value, err := DecodeInputFile(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field front_side: %w", err)
		}
		i.FrontSide = value
	}
	{
		value, err := DecodeInputFile(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field reverse_side: %w", err)
		}
		i.ReverseSide = value
	}
	{
		value, err := DecodeInputFile(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field selfie: %w", err)
		}
		i.Selfie = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field translation: %w", err)
		}

		if headerLen > 0 {
			i.Translation = make([]InputFileClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field translation: %w", err)
			}
			i.Translation = append(i.Translation, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InputIdentityDocument) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inputIdentityDocument#185f2052 as nil")
	}
	b.ObjStart()
	b.PutID("inputIdentityDocument")
	b.Comma()
	b.FieldStart("number")
	b.PutString(i.Number)
	b.Comma()
	b.FieldStart("expiration_date")
	if err := i.ExpirationDate.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field expiration_date: %w", err)
	}
	b.Comma()
	b.FieldStart("front_side")
	if i.FrontSide == nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field front_side is nil")
	}
	if err := i.FrontSide.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field front_side: %w", err)
	}
	b.Comma()
	b.FieldStart("reverse_side")
	if i.ReverseSide == nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field reverse_side is nil")
	}
	if err := i.ReverseSide.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field reverse_side: %w", err)
	}
	b.Comma()
	b.FieldStart("selfie")
	if i.Selfie == nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field selfie is nil")
	}
	if err := i.Selfie.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field selfie: %w", err)
	}
	b.Comma()
	b.FieldStart("translation")
	b.ArrStart()
	for idx, v := range i.Translation {
		if v == nil {
			return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field translation element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode inputIdentityDocument#185f2052: field translation element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InputIdentityDocument) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inputIdentityDocument#185f2052 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inputIdentityDocument"); err != nil {
				return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: %w", err)
			}
		case "number":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field number: %w", err)
			}
			i.Number = value
		case "expiration_date":
			if err := i.ExpirationDate.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field expiration_date: %w", err)
			}
		case "front_side":
			value, err := DecodeTDLibJSONInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field front_side: %w", err)
			}
			i.FrontSide = value
		case "reverse_side":
			value, err := DecodeTDLibJSONInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field reverse_side: %w", err)
			}
			i.ReverseSide = value
		case "selfie":
			value, err := DecodeTDLibJSONInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field selfie: %w", err)
			}
			i.Selfie = value
		case "translation":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONInputFile(b)
				if err != nil {
					return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field translation: %w", err)
				}
				i.Translation = append(i.Translation, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode inputIdentityDocument#185f2052: field translation: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetNumber returns value of Number field.
func (i *InputIdentityDocument) GetNumber() (value string) {
	if i == nil {
		return
	}
	return i.Number
}

// GetExpirationDate returns value of ExpirationDate field.
func (i *InputIdentityDocument) GetExpirationDate() (value Date) {
	if i == nil {
		return
	}
	return i.ExpirationDate
}

// GetFrontSide returns value of FrontSide field.
func (i *InputIdentityDocument) GetFrontSide() (value InputFileClass) {
	if i == nil {
		return
	}
	return i.FrontSide
}

// GetReverseSide returns value of ReverseSide field.
func (i *InputIdentityDocument) GetReverseSide() (value InputFileClass) {
	if i == nil {
		return
	}
	return i.ReverseSide
}

// GetSelfie returns value of Selfie field.
func (i *InputIdentityDocument) GetSelfie() (value InputFileClass) {
	if i == nil {
		return
	}
	return i.Selfie
}

// GetTranslation returns value of Translation field.
func (i *InputIdentityDocument) GetTranslation() (value []InputFileClass) {
	if i == nil {
		return
	}
	return i.Translation
}
