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

// IdentityDocument represents TL type `identityDocument#289299ff`.
type IdentityDocument struct {
	// Document number; 1-24 characters
	Number string
	// Document expiration date; may be null if not applicable
	ExpirationDate Date
	// Front side of the document
	FrontSide DatedFile
	// Reverse side of the document; only for driver license and identity card; may be null
	ReverseSide DatedFile
	// Selfie with the document; may be null
	Selfie DatedFile
	// List of files containing a certified English translation of the document
	Translation []DatedFile
}

// IdentityDocumentTypeID is TL type id of IdentityDocument.
const IdentityDocumentTypeID = 0x289299ff

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *IdentityDocument) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "identityDocument",
		ID:   IdentityDocumentTypeID,
	}
	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 *IdentityDocument) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode identityDocument#289299ff as nil")
	}
	b.PutID(IdentityDocumentTypeID)
	return i.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (i *IdentityDocument) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode identityDocument#289299ff to nil")
	}
	if err := b.ConsumeID(IdentityDocumentTypeID); err != nil {
		return fmt.Errorf("unable to decode identityDocument#289299ff: %w", err)
	}
	return i.DecodeBare(b)
}

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

		if headerLen > 0 {
			i.Translation = make([]DatedFile, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value DatedFile
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare identityDocument#289299ff: field translation: %w", err)
			}
			i.Translation = append(i.Translation, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *IdentityDocument) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode identityDocument#289299ff as nil")
	}
	b.ObjStart()
	b.PutID("identityDocument")
	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 identityDocument#289299ff: field expiration_date: %w", err)
	}
	b.Comma()
	b.FieldStart("front_side")
	if err := i.FrontSide.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode identityDocument#289299ff: field front_side: %w", err)
	}
	b.Comma()
	b.FieldStart("reverse_side")
	if err := i.ReverseSide.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode identityDocument#289299ff: field reverse_side: %w", err)
	}
	b.Comma()
	b.FieldStart("selfie")
	if err := i.Selfie.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode identityDocument#289299ff: field selfie: %w", err)
	}
	b.Comma()
	b.FieldStart("translation")
	b.ArrStart()
	for idx, v := range i.Translation {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode identityDocument#289299ff: 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 *IdentityDocument) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode identityDocument#289299ff to nil")
	}

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

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

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

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

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

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

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