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

// FoundFileDownloads represents TL type `foundFileDownloads#c7db4128`.
type FoundFileDownloads struct {
	// Total number of suitable files, ignoring offset
	TotalCounts DownloadedFileCounts
	// The list of files
	Files []FileDownload
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// FoundFileDownloadsTypeID is TL type id of FoundFileDownloads.
const FoundFileDownloadsTypeID = 0xc7db4128

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

func (f *FoundFileDownloads) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.TotalCounts.Zero()) {
		return false
	}
	if !(f.Files == nil) {
		return false
	}
	if !(f.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FoundFileDownloads) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "foundFileDownloads",
		ID:   FoundFileDownloadsTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCounts",
			SchemaName: "total_counts",
		},
		{
			Name:       "Files",
			SchemaName: "files",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FoundFileDownloads) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundFileDownloads#c7db4128 as nil")
	}
	b.PutID(FoundFileDownloadsTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FoundFileDownloads) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundFileDownloads#c7db4128 as nil")
	}
	if err := f.TotalCounts.Encode(b); err != nil {
		return fmt.Errorf("unable to encode foundFileDownloads#c7db4128: field total_counts: %w", err)
	}
	b.PutInt(len(f.Files))
	for idx, v := range f.Files {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare foundFileDownloads#c7db4128: field files element with index %d: %w", idx, err)
		}
	}
	b.PutString(f.NextOffset)
	return nil
}

// Decode implements bin.Decoder.
func (f *FoundFileDownloads) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundFileDownloads#c7db4128 to nil")
	}
	if err := b.ConsumeID(FoundFileDownloadsTypeID); err != nil {
		return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FoundFileDownloads) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundFileDownloads#c7db4128 to nil")
	}
	{
		if err := f.TotalCounts.Decode(b); err != nil {
			return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: field total_counts: %w", err)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: field files: %w", err)
		}

		if headerLen > 0 {
			f.Files = make([]FileDownload, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value FileDownload
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare foundFileDownloads#c7db4128: field files: %w", err)
			}
			f.Files = append(f.Files, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: field next_offset: %w", err)
		}
		f.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FoundFileDownloads) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode foundFileDownloads#c7db4128 as nil")
	}
	b.ObjStart()
	b.PutID("foundFileDownloads")
	b.Comma()
	b.FieldStart("total_counts")
	if err := f.TotalCounts.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode foundFileDownloads#c7db4128: field total_counts: %w", err)
	}
	b.Comma()
	b.FieldStart("files")
	b.ArrStart()
	for idx, v := range f.Files {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode foundFileDownloads#c7db4128: field files element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(f.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *FoundFileDownloads) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode foundFileDownloads#c7db4128 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("foundFileDownloads"); err != nil {
				return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: %w", err)
			}
		case "total_counts":
			if err := f.TotalCounts.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: field total_counts: %w", err)
			}
		case "files":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value FileDownload
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: field files: %w", err)
				}
				f.Files = append(f.Files, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: field files: %w", err)
			}
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode foundFileDownloads#c7db4128: field next_offset: %w", err)
			}
			f.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCounts returns value of TotalCounts field.
func (f *FoundFileDownloads) GetTotalCounts() (value DownloadedFileCounts) {
	if f == nil {
		return
	}
	return f.TotalCounts
}

// GetFiles returns value of Files field.
func (f *FoundFileDownloads) GetFiles() (value []FileDownload) {
	if f == nil {
		return
	}
	return f.Files
}

// GetNextOffset returns value of NextOffset field.
func (f *FoundFileDownloads) GetNextOffset() (value string) {
	if f == nil {
		return
	}
	return f.NextOffset
}
