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

// DownloadFileRequest represents TL type `downloadFile#3f253234`.
type DownloadFileRequest struct {
	// Identifier of the file to download
	FileID int32
	// Priority of the download (1-32). The higher the priority, the earlier the file will be
	// downloaded. If the priorities of two files are equal, then the last one for which
	// downloadFile/addFileToDownloads was called will be downloaded first
	Priority int32
	// The starting position from which the file needs to be downloaded
	Offset int64
	// Number of bytes which need to be downloaded starting from the "offset" position before
	// the download will automatically be canceled; use 0 to download without a limit
	Limit int64
	// Pass true to return response only after the file download has succeeded, has failed,
	// has been canceled, or a new downloadFile request with different offset/limit
	// parameters was sent; pass false to return file state immediately, just after the
	// download has been started
	Synchronous bool
}

// DownloadFileRequestTypeID is TL type id of DownloadFileRequest.
const DownloadFileRequestTypeID = 0x3f253234

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

func (d *DownloadFileRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.FileID == 0) {
		return false
	}
	if !(d.Priority == 0) {
		return false
	}
	if !(d.Offset == 0) {
		return false
	}
	if !(d.Limit == 0) {
		return false
	}
	if !(d.Synchronous == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DownloadFileRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "downloadFile",
		ID:   DownloadFileRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "FileID",
			SchemaName: "file_id",
		},
		{
			Name:       "Priority",
			SchemaName: "priority",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "Synchronous",
			SchemaName: "synchronous",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DownloadFileRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode downloadFile#3f253234 as nil")
	}
	b.PutID(DownloadFileRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DownloadFileRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode downloadFile#3f253234 as nil")
	}
	b.PutInt32(d.FileID)
	b.PutInt32(d.Priority)
	b.PutInt53(d.Offset)
	b.PutInt53(d.Limit)
	b.PutBool(d.Synchronous)
	return nil
}

// Decode implements bin.Decoder.
func (d *DownloadFileRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode downloadFile#3f253234 to nil")
	}
	if err := b.ConsumeID(DownloadFileRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode downloadFile#3f253234: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DownloadFileRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode downloadFile#3f253234 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode downloadFile#3f253234: field file_id: %w", err)
		}
		d.FileID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode downloadFile#3f253234: field priority: %w", err)
		}
		d.Priority = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode downloadFile#3f253234: field offset: %w", err)
		}
		d.Offset = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode downloadFile#3f253234: field limit: %w", err)
		}
		d.Limit = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode downloadFile#3f253234: field synchronous: %w", err)
		}
		d.Synchronous = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DownloadFileRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode downloadFile#3f253234 as nil")
	}
	b.ObjStart()
	b.PutID("downloadFile")
	b.Comma()
	b.FieldStart("file_id")
	b.PutInt32(d.FileID)
	b.Comma()
	b.FieldStart("priority")
	b.PutInt32(d.Priority)
	b.Comma()
	b.FieldStart("offset")
	b.PutInt53(d.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt53(d.Limit)
	b.Comma()
	b.FieldStart("synchronous")
	b.PutBool(d.Synchronous)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DownloadFileRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode downloadFile#3f253234 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("downloadFile"); err != nil {
				return fmt.Errorf("unable to decode downloadFile#3f253234: %w", err)
			}
		case "file_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode downloadFile#3f253234: field file_id: %w", err)
			}
			d.FileID = value
		case "priority":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode downloadFile#3f253234: field priority: %w", err)
			}
			d.Priority = value
		case "offset":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode downloadFile#3f253234: field offset: %w", err)
			}
			d.Offset = value
		case "limit":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode downloadFile#3f253234: field limit: %w", err)
			}
			d.Limit = value
		case "synchronous":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode downloadFile#3f253234: field synchronous: %w", err)
			}
			d.Synchronous = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetPriority returns value of Priority field.
func (d *DownloadFileRequest) GetPriority() (value int32) {
	if d == nil {
		return
	}
	return d.Priority
}

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

// GetLimit returns value of Limit field.
func (d *DownloadFileRequest) GetLimit() (value int64) {
	if d == nil {
		return
	}
	return d.Limit
}

// GetSynchronous returns value of Synchronous field.
func (d *DownloadFileRequest) GetSynchronous() (value bool) {
	if d == nil {
		return
	}
	return d.Synchronous
}

// DownloadFile invokes method downloadFile#3f253234 returning error if any.
func (c *Client) DownloadFile(ctx context.Context, request *DownloadFileRequest) (*File, error) {
	var result File

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