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

// RemoveFileFromDownloadsRequest represents TL type `removeFileFromDownloads#5706bfee`.
type RemoveFileFromDownloadsRequest struct {
	// Identifier of the downloaded file
	FileID int32
	// Pass true to delete the file from the TDLib file cache
	DeleteFromCache bool
}

// RemoveFileFromDownloadsRequestTypeID is TL type id of RemoveFileFromDownloadsRequest.
const RemoveFileFromDownloadsRequestTypeID = 0x5706bfee

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

func (r *RemoveFileFromDownloadsRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.FileID == 0) {
		return false
	}
	if !(r.DeleteFromCache == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *RemoveFileFromDownloadsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "removeFileFromDownloads",
		ID:   RemoveFileFromDownloadsRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "FileID",
			SchemaName: "file_id",
		},
		{
			Name:       "DeleteFromCache",
			SchemaName: "delete_from_cache",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *RemoveFileFromDownloadsRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode removeFileFromDownloads#5706bfee as nil")
	}
	b.PutID(RemoveFileFromDownloadsRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *RemoveFileFromDownloadsRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode removeFileFromDownloads#5706bfee as nil")
	}
	b.PutInt32(r.FileID)
	b.PutBool(r.DeleteFromCache)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (r *RemoveFileFromDownloadsRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode removeFileFromDownloads#5706bfee to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode removeFileFromDownloads#5706bfee: field file_id: %w", err)
		}
		r.FileID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode removeFileFromDownloads#5706bfee: field delete_from_cache: %w", err)
		}
		r.DeleteFromCache = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *RemoveFileFromDownloadsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode removeFileFromDownloads#5706bfee as nil")
	}
	b.ObjStart()
	b.PutID("removeFileFromDownloads")
	b.Comma()
	b.FieldStart("file_id")
	b.PutInt32(r.FileID)
	b.Comma()
	b.FieldStart("delete_from_cache")
	b.PutBool(r.DeleteFromCache)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// GetDeleteFromCache returns value of DeleteFromCache field.
func (r *RemoveFileFromDownloadsRequest) GetDeleteFromCache() (value bool) {
	if r == nil {
		return
	}
	return r.DeleteFromCache
}

// RemoveFileFromDownloads invokes method removeFileFromDownloads#5706bfee returning error if any.
func (c *Client) RemoveFileFromDownloads(ctx context.Context, request *RemoveFileFromDownloadsRequest) error {
	var ok Ok

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