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

// CancelPreliminaryUploadFileRequest represents TL type `cancelPreliminaryUploadFile#311446be`.
type CancelPreliminaryUploadFileRequest struct {
	// Identifier of the file to stop uploading
	FileID int32
}

// CancelPreliminaryUploadFileRequestTypeID is TL type id of CancelPreliminaryUploadFileRequest.
const CancelPreliminaryUploadFileRequestTypeID = 0x311446be

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (c *CancelPreliminaryUploadFileRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode cancelPreliminaryUploadFile#311446be as nil")
	}
	b.PutID(CancelPreliminaryUploadFileRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CancelPreliminaryUploadFileRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode cancelPreliminaryUploadFile#311446be as nil")
	}
	b.PutInt32(c.FileID)
	return nil
}

// Decode implements bin.Decoder.
func (c *CancelPreliminaryUploadFileRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode cancelPreliminaryUploadFile#311446be to nil")
	}
	if err := b.ConsumeID(CancelPreliminaryUploadFileRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode cancelPreliminaryUploadFile#311446be: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CancelPreliminaryUploadFileRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode cancelPreliminaryUploadFile#311446be to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode cancelPreliminaryUploadFile#311446be: field file_id: %w", err)
		}
		c.FileID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CancelPreliminaryUploadFileRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode cancelPreliminaryUploadFile#311446be as nil")
	}
	b.ObjStart()
	b.PutID("cancelPreliminaryUploadFile")
	b.Comma()
	b.FieldStart("file_id")
	b.PutInt32(c.FileID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CancelPreliminaryUploadFileRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode cancelPreliminaryUploadFile#311446be to nil")
	}

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

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

// CancelPreliminaryUploadFile invokes method cancelPreliminaryUploadFile#311446be returning error if any.
func (c *Client) CancelPreliminaryUploadFile(ctx context.Context, fileid int32) error {
	var ok Ok

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