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

// RemoveAllFilesFromDownloadsRequest represents TL type `removeAllFilesFromDownloads#b9487686`.
type RemoveAllFilesFromDownloadsRequest struct {
	// Pass true to remove only active downloads, including paused
	OnlyActive bool
	// Pass true to remove only completed downloads
	OnlyCompleted bool
	// Pass true to delete the file from the TDLib file cache
	DeleteFromCache bool
}

// RemoveAllFilesFromDownloadsRequestTypeID is TL type id of RemoveAllFilesFromDownloadsRequest.
const RemoveAllFilesFromDownloadsRequestTypeID = 0xb9487686

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

func (r *RemoveAllFilesFromDownloadsRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.OnlyActive == false) {
		return false
	}
	if !(r.OnlyCompleted == false) {
		return false
	}
	if !(r.DeleteFromCache == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *RemoveAllFilesFromDownloadsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "removeAllFilesFromDownloads",
		ID:   RemoveAllFilesFromDownloadsRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OnlyActive",
			SchemaName: "only_active",
		},
		{
			Name:       "OnlyCompleted",
			SchemaName: "only_completed",
		},
		{
			Name:       "DeleteFromCache",
			SchemaName: "delete_from_cache",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *RemoveAllFilesFromDownloadsRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode removeAllFilesFromDownloads#b9487686 as nil")
	}
	b.PutID(RemoveAllFilesFromDownloadsRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *RemoveAllFilesFromDownloadsRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode removeAllFilesFromDownloads#b9487686 as nil")
	}
	b.PutBool(r.OnlyActive)
	b.PutBool(r.OnlyCompleted)
	b.PutBool(r.DeleteFromCache)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (r *RemoveAllFilesFromDownloadsRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode removeAllFilesFromDownloads#b9487686 to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode removeAllFilesFromDownloads#b9487686: field only_active: %w", err)
		}
		r.OnlyActive = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode removeAllFilesFromDownloads#b9487686: field only_completed: %w", err)
		}
		r.OnlyCompleted = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode removeAllFilesFromDownloads#b9487686: field delete_from_cache: %w", err)
		}
		r.DeleteFromCache = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *RemoveAllFilesFromDownloadsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode removeAllFilesFromDownloads#b9487686 as nil")
	}
	b.ObjStart()
	b.PutID("removeAllFilesFromDownloads")
	b.Comma()
	b.FieldStart("only_active")
	b.PutBool(r.OnlyActive)
	b.Comma()
	b.FieldStart("only_completed")
	b.PutBool(r.OnlyCompleted)
	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 *RemoveAllFilesFromDownloadsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode removeAllFilesFromDownloads#b9487686 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("removeAllFilesFromDownloads"); err != nil {
				return fmt.Errorf("unable to decode removeAllFilesFromDownloads#b9487686: %w", err)
			}
		case "only_active":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode removeAllFilesFromDownloads#b9487686: field only_active: %w", err)
			}
			r.OnlyActive = value
		case "only_completed":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode removeAllFilesFromDownloads#b9487686: field only_completed: %w", err)
			}
			r.OnlyCompleted = value
		case "delete_from_cache":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode removeAllFilesFromDownloads#b9487686: field delete_from_cache: %w", err)
			}
			r.DeleteFromCache = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetOnlyActive returns value of OnlyActive field.
func (r *RemoveAllFilesFromDownloadsRequest) GetOnlyActive() (value bool) {
	if r == nil {
		return
	}
	return r.OnlyActive
}

// GetOnlyCompleted returns value of OnlyCompleted field.
func (r *RemoveAllFilesFromDownloadsRequest) GetOnlyCompleted() (value bool) {
	if r == nil {
		return
	}
	return r.OnlyCompleted
}

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

// RemoveAllFilesFromDownloads invokes method removeAllFilesFromDownloads#b9487686 returning error if any.
func (c *Client) RemoveAllFilesFromDownloads(ctx context.Context, request *RemoveAllFilesFromDownloadsRequest) error {
	var ok Ok

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