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

// FoundPosition represents TL type `foundPosition#8f8adf88`.
type FoundPosition struct {
	// The position of the match
	Position int32
}

// FoundPositionTypeID is TL type id of FoundPosition.
const FoundPositionTypeID = 0x8f8adf88

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

func (f *FoundPosition) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Position == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FoundPosition) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "foundPosition",
		ID:   FoundPositionTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Position",
			SchemaName: "position",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FoundPosition) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundPosition#8f8adf88 as nil")
	}
	b.PutID(FoundPositionTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FoundPosition) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundPosition#8f8adf88 as nil")
	}
	b.PutInt32(f.Position)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (f *FoundPosition) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundPosition#8f8adf88 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode foundPosition#8f8adf88: field position: %w", err)
		}
		f.Position = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FoundPosition) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode foundPosition#8f8adf88 as nil")
	}
	b.ObjStart()
	b.PutID("foundPosition")
	b.Comma()
	b.FieldStart("position")
	b.PutInt32(f.Position)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("foundPosition"); err != nil {
				return fmt.Errorf("unable to decode foundPosition#8f8adf88: %w", err)
			}
		case "position":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode foundPosition#8f8adf88: field position: %w", err)
			}
			f.Position = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPosition returns value of Position field.
func (f *FoundPosition) GetPosition() (value int32) {
	if f == nil {
		return
	}
	return f.Position
}
