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

// PersonalDocument represents TL type `personalDocument#a8357e38`.
type PersonalDocument struct {
	// List of files containing the pages of the document
	Files []DatedFile
	// List of files containing a certified English translation of the document
	Translation []DatedFile
}

// PersonalDocumentTypeID is TL type id of PersonalDocument.
const PersonalDocumentTypeID = 0xa8357e38

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

func (p *PersonalDocument) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Files == nil) {
		return false
	}
	if !(p.Translation == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PersonalDocument) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "personalDocument",
		ID:   PersonalDocumentTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Files",
			SchemaName: "files",
		},
		{
			Name:       "Translation",
			SchemaName: "translation",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PersonalDocument) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode personalDocument#a8357e38 as nil")
	}
	b.PutID(PersonalDocumentTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PersonalDocument) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode personalDocument#a8357e38 as nil")
	}
	b.PutInt(len(p.Files))
	for idx, v := range p.Files {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare personalDocument#a8357e38: field files element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(p.Translation))
	for idx, v := range p.Translation {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare personalDocument#a8357e38: field translation element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PersonalDocument) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode personalDocument#a8357e38 to nil")
	}
	if err := b.ConsumeID(PersonalDocumentTypeID); err != nil {
		return fmt.Errorf("unable to decode personalDocument#a8357e38: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PersonalDocument) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode personalDocument#a8357e38 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode personalDocument#a8357e38: field files: %w", err)
		}

		if headerLen > 0 {
			p.Files = 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 personalDocument#a8357e38: field files: %w", err)
			}
			p.Files = append(p.Files, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode personalDocument#a8357e38: field translation: %w", err)
		}

		if headerLen > 0 {
			p.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 personalDocument#a8357e38: field translation: %w", err)
			}
			p.Translation = append(p.Translation, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PersonalDocument) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode personalDocument#a8357e38 as nil")
	}
	b.ObjStart()
	b.PutID("personalDocument")
	b.Comma()
	b.FieldStart("files")
	b.ArrStart()
	for idx, v := range p.Files {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode personalDocument#a8357e38: field files element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("translation")
	b.ArrStart()
	for idx, v := range p.Translation {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode personalDocument#a8357e38: 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 (p *PersonalDocument) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode personalDocument#a8357e38 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("personalDocument"); err != nil {
				return fmt.Errorf("unable to decode personalDocument#a8357e38: %w", err)
			}
		case "files":
			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 personalDocument#a8357e38: field files: %w", err)
				}
				p.Files = append(p.Files, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode personalDocument#a8357e38: field files: %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 personalDocument#a8357e38: field translation: %w", err)
				}
				p.Translation = append(p.Translation, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode personalDocument#a8357e38: field translation: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetFiles returns value of Files field.
func (p *PersonalDocument) GetFiles() (value []DatedFile) {
	if p == nil {
		return
	}
	return p.Files
}

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