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

// CancelDownloadFileRequest represents TL type `cancelDownloadFile#8b8052de`.
type CancelDownloadFileRequest struct {
	// Identifier of a file to stop downloading
	FileID int32
	// Pass true to stop downloading only if it hasn't been started, i.e. request hasn't been
	// sent to server
	OnlyIfPending bool
}

// CancelDownloadFileRequestTypeID is TL type id of CancelDownloadFileRequest.
const CancelDownloadFileRequestTypeID = 0x8b8052de

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

func (c *CancelDownloadFileRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.FileID == 0) {
		return false
	}
	if !(c.OnlyIfPending == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CancelDownloadFileRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "cancelDownloadFile",
		ID:   CancelDownloadFileRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "FileID",
			SchemaName: "file_id",
		},
		{
			Name:       "OnlyIfPending",
			SchemaName: "only_if_pending",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CancelDownloadFileRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode cancelDownloadFile#8b8052de as nil")
	}
	b.PutID(CancelDownloadFileRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CancelDownloadFileRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode cancelDownloadFile#8b8052de as nil")
	}
	b.PutInt32(c.FileID)
	b.PutBool(c.OnlyIfPending)
	return nil
}

// Decode implements bin.Decoder.
func (c *CancelDownloadFileRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode cancelDownloadFile#8b8052de to nil")
	}
	if err := b.ConsumeID(CancelDownloadFileRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode cancelDownloadFile#8b8052de: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CancelDownloadFileRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode cancelDownloadFile#8b8052de to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode cancelDownloadFile#8b8052de: field file_id: %w", err)
		}
		c.FileID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode cancelDownloadFile#8b8052de: field only_if_pending: %w", err)
		}
		c.OnlyIfPending = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CancelDownloadFileRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode cancelDownloadFile#8b8052de as nil")
	}
	b.ObjStart()
	b.PutID("cancelDownloadFile")
	b.Comma()
	b.FieldStart("file_id")
	b.PutInt32(c.FileID)
	b.Comma()
	b.FieldStart("only_if_pending")
	b.PutBool(c.OnlyIfPending)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CancelDownloadFileRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode cancelDownloadFile#8b8052de to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("cancelDownloadFile"); err != nil {
				return fmt.Errorf("unable to decode cancelDownloadFile#8b8052de: %w", err)
			}
		case "file_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode cancelDownloadFile#8b8052de: field file_id: %w", err)
			}
			c.FileID = value
		case "only_if_pending":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode cancelDownloadFile#8b8052de: field only_if_pending: %w", err)
			}
			c.OnlyIfPending = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetOnlyIfPending returns value of OnlyIfPending field.
func (c *CancelDownloadFileRequest) GetOnlyIfPending() (value bool) {
	if c == nil {
		return
	}
	return c.OnlyIfPending
}

// CancelDownloadFile invokes method cancelDownloadFile#8b8052de returning error if any.
func (c *Client) CancelDownloadFile(ctx context.Context, request *CancelDownloadFileRequest) error {
	var ok Ok

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