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

// ReadFilePartRequest represents TL type `readFilePart#f5982b4a`.
type ReadFilePartRequest struct {
	// Identifier of the file. The file must be located in the TDLib file cache
	FileID int32
	// The offset from which to read the file
	Offset int64
	// Number of bytes to read. An error will be returned if there are not enough bytes
	// available in the file from the specified position. Pass 0 to read all available data
	// from the specified position
	Count int64
}

// ReadFilePartRequestTypeID is TL type id of ReadFilePartRequest.
const ReadFilePartRequestTypeID = 0xf5982b4a

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

func (r *ReadFilePartRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.FileID == 0) {
		return false
	}
	if !(r.Offset == 0) {
		return false
	}
	if !(r.Count == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *ReadFilePartRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "readFilePart",
		ID:   ReadFilePartRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "FileID",
			SchemaName: "file_id",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Count",
			SchemaName: "count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ReadFilePartRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode readFilePart#f5982b4a as nil")
	}
	b.PutID(ReadFilePartRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ReadFilePartRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode readFilePart#f5982b4a as nil")
	}
	b.PutInt32(r.FileID)
	b.PutInt53(r.Offset)
	b.PutInt53(r.Count)
	return nil
}

// Decode implements bin.Decoder.
func (r *ReadFilePartRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode readFilePart#f5982b4a to nil")
	}
	if err := b.ConsumeID(ReadFilePartRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode readFilePart#f5982b4a: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ReadFilePartRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode readFilePart#f5982b4a to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode readFilePart#f5982b4a: field file_id: %w", err)
		}
		r.FileID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode readFilePart#f5982b4a: field offset: %w", err)
		}
		r.Offset = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode readFilePart#f5982b4a: field count: %w", err)
		}
		r.Count = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *ReadFilePartRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode readFilePart#f5982b4a as nil")
	}
	b.ObjStart()
	b.PutID("readFilePart")
	b.Comma()
	b.FieldStart("file_id")
	b.PutInt32(r.FileID)
	b.Comma()
	b.FieldStart("offset")
	b.PutInt53(r.Offset)
	b.Comma()
	b.FieldStart("count")
	b.PutInt53(r.Count)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *ReadFilePartRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode readFilePart#f5982b4a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("readFilePart"); err != nil {
				return fmt.Errorf("unable to decode readFilePart#f5982b4a: %w", err)
			}
		case "file_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode readFilePart#f5982b4a: field file_id: %w", err)
			}
			r.FileID = value
		case "offset":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode readFilePart#f5982b4a: field offset: %w", err)
			}
			r.Offset = value
		case "count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode readFilePart#f5982b4a: field count: %w", err)
			}
			r.Count = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetFileID returns value of FileID field.
func (r *ReadFilePartRequest) GetFileID() (value int32) {
	if r == nil {
		return
	}
	return r.FileID
}

// GetOffset returns value of Offset field.
func (r *ReadFilePartRequest) GetOffset() (value int64) {
	if r == nil {
		return
	}
	return r.Offset
}

// GetCount returns value of Count field.
func (r *ReadFilePartRequest) GetCount() (value int64) {
	if r == nil {
		return
	}
	return r.Count
}

// ReadFilePart invokes method readFilePart#f5982b4a returning error if any.
func (c *Client) ReadFilePart(ctx context.Context, request *ReadFilePartRequest) (*Data, error) {
	var result Data

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
