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

// FoundPositions represents TL type `foundPositions#afab1f62`.
type FoundPositions struct {
	// Total number of matched objects
	TotalCount int32
	// The positions of the matched objects
	Positions []int32
}

// FoundPositionsTypeID is TL type id of FoundPositions.
const FoundPositionsTypeID = 0xafab1f62

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

func (f *FoundPositions) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.TotalCount == 0) {
		return false
	}
	if !(f.Positions == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FoundPositions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "foundPositions",
		ID:   FoundPositionsTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Positions",
			SchemaName: "positions",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FoundPositions) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundPositions#afab1f62 as nil")
	}
	b.PutID(FoundPositionsTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FoundPositions) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundPositions#afab1f62 as nil")
	}
	b.PutInt32(f.TotalCount)
	b.PutInt(len(f.Positions))
	for _, v := range f.Positions {
		b.PutInt32(v)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (f *FoundPositions) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundPositions#afab1f62 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode foundPositions#afab1f62: field total_count: %w", err)
		}
		f.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode foundPositions#afab1f62: field positions: %w", err)
		}

		if headerLen > 0 {
			f.Positions = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode foundPositions#afab1f62: field positions: %w", err)
			}
			f.Positions = append(f.Positions, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FoundPositions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode foundPositions#afab1f62 as nil")
	}
	b.ObjStart()
	b.PutID("foundPositions")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(f.TotalCount)
	b.Comma()
	b.FieldStart("positions")
	b.ArrStart()
	for _, v := range f.Positions {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("foundPositions"); err != nil {
				return fmt.Errorf("unable to decode foundPositions#afab1f62: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode foundPositions#afab1f62: field total_count: %w", err)
			}
			f.TotalCount = value
		case "positions":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode foundPositions#afab1f62: field positions: %w", err)
				}
				f.Positions = append(f.Positions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode foundPositions#afab1f62: field positions: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (f *FoundPositions) GetTotalCount() (value int32) {
	if f == nil {
		return
	}
	return f.TotalCount
}

// GetPositions returns value of Positions field.
func (f *FoundPositions) GetPositions() (value []int32) {
	if f == nil {
		return
	}
	return f.Positions
}
