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

// OptimizeStorageRequest represents TL type `optimizeStorage#ef73c8c5`.
type OptimizeStorageRequest struct {
	// Limit on the total size of files after deletion, in bytes. Pass -1 to use the default
	// limit
	Size int64
	// Limit on the time that has passed since the last time a file was accessed (or creation
	// time for some filesystems). Pass -1 to use the default limit
	TTL int32
	// Limit on the total number of files after deletion. Pass -1 to use the default limit
	Count int32
	// The amount of time after the creation of a file during which it can't be deleted, in
	// seconds. Pass -1 to use the default value
	ImmunityDelay int32
	// If non-empty, only files with the given types are considered. By default, all types
	// except thumbnails, profile photos, stickers and wallpapers are deleted
	FileTypes []FileTypeClass
	// If non-empty, only files from the given chats are considered. Use 0 as chat identifier
	// to delete files not belonging to any chat (e.g., profile photos)
	ChatIDs []int64
	// If non-empty, files from the given chats are excluded. Use 0 as chat identifier to
	// exclude all files not belonging to any chat (e.g., profile photos)
	ExcludeChatIDs []int64
	// Pass true if statistics about the files that were deleted must be returned instead of
	// the whole storage usage statistics. Affects only returned statistics
	ReturnDeletedFileStatistics bool
	// Same as in getStorageStatistics. Affects only returned statistics
	ChatLimit int32
}

// OptimizeStorageRequestTypeID is TL type id of OptimizeStorageRequest.
const OptimizeStorageRequestTypeID = 0xef73c8c5

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

func (o *OptimizeStorageRequest) Zero() bool {
	if o == nil {
		return true
	}
	if !(o.Size == 0) {
		return false
	}
	if !(o.TTL == 0) {
		return false
	}
	if !(o.Count == 0) {
		return false
	}
	if !(o.ImmunityDelay == 0) {
		return false
	}
	if !(o.FileTypes == nil) {
		return false
	}
	if !(o.ChatIDs == nil) {
		return false
	}
	if !(o.ExcludeChatIDs == nil) {
		return false
	}
	if !(o.ReturnDeletedFileStatistics == false) {
		return false
	}
	if !(o.ChatLimit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (o *OptimizeStorageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "optimizeStorage",
		ID:   OptimizeStorageRequestTypeID,
	}
	if o == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Size",
			SchemaName: "size",
		},
		{
			Name:       "TTL",
			SchemaName: "ttl",
		},
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "ImmunityDelay",
			SchemaName: "immunity_delay",
		},
		{
			Name:       "FileTypes",
			SchemaName: "file_types",
		},
		{
			Name:       "ChatIDs",
			SchemaName: "chat_ids",
		},
		{
			Name:       "ExcludeChatIDs",
			SchemaName: "exclude_chat_ids",
		},
		{
			Name:       "ReturnDeletedFileStatistics",
			SchemaName: "return_deleted_file_statistics",
		},
		{
			Name:       "ChatLimit",
			SchemaName: "chat_limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (o *OptimizeStorageRequest) Encode(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode optimizeStorage#ef73c8c5 as nil")
	}
	b.PutID(OptimizeStorageRequestTypeID)
	return o.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (o *OptimizeStorageRequest) EncodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode optimizeStorage#ef73c8c5 as nil")
	}
	b.PutInt53(o.Size)
	b.PutInt32(o.TTL)
	b.PutInt32(o.Count)
	b.PutInt32(o.ImmunityDelay)
	b.PutInt(len(o.FileTypes))
	for idx, v := range o.FileTypes {
		if v == nil {
			return fmt.Errorf("unable to encode optimizeStorage#ef73c8c5: field file_types element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare optimizeStorage#ef73c8c5: field file_types element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(o.ChatIDs))
	for _, v := range o.ChatIDs {
		b.PutInt53(v)
	}
	b.PutInt(len(o.ExcludeChatIDs))
	for _, v := range o.ExcludeChatIDs {
		b.PutInt53(v)
	}
	b.PutBool(o.ReturnDeletedFileStatistics)
	b.PutInt32(o.ChatLimit)
	return nil
}

// Decode implements bin.Decoder.
func (o *OptimizeStorageRequest) Decode(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't decode optimizeStorage#ef73c8c5 to nil")
	}
	if err := b.ConsumeID(OptimizeStorageRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: %w", err)
	}
	return o.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (o *OptimizeStorageRequest) DecodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't decode optimizeStorage#ef73c8c5 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field size: %w", err)
		}
		o.Size = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field ttl: %w", err)
		}
		o.TTL = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field count: %w", err)
		}
		o.Count = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field immunity_delay: %w", err)
		}
		o.ImmunityDelay = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field file_types: %w", err)
		}

		if headerLen > 0 {
			o.FileTypes = make([]FileTypeClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeFileType(b)
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field file_types: %w", err)
			}
			o.FileTypes = append(o.FileTypes, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field chat_ids: %w", err)
		}

		if headerLen > 0 {
			o.ChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field chat_ids: %w", err)
			}
			o.ChatIDs = append(o.ChatIDs, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field exclude_chat_ids: %w", err)
		}

		if headerLen > 0 {
			o.ExcludeChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field exclude_chat_ids: %w", err)
			}
			o.ExcludeChatIDs = append(o.ExcludeChatIDs, value)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field return_deleted_file_statistics: %w", err)
		}
		o.ReturnDeletedFileStatistics = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field chat_limit: %w", err)
		}
		o.ChatLimit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (o *OptimizeStorageRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if o == nil {
		return fmt.Errorf("can't encode optimizeStorage#ef73c8c5 as nil")
	}
	b.ObjStart()
	b.PutID("optimizeStorage")
	b.Comma()
	b.FieldStart("size")
	b.PutInt53(o.Size)
	b.Comma()
	b.FieldStart("ttl")
	b.PutInt32(o.TTL)
	b.Comma()
	b.FieldStart("count")
	b.PutInt32(o.Count)
	b.Comma()
	b.FieldStart("immunity_delay")
	b.PutInt32(o.ImmunityDelay)
	b.Comma()
	b.FieldStart("file_types")
	b.ArrStart()
	for idx, v := range o.FileTypes {
		if v == nil {
			return fmt.Errorf("unable to encode optimizeStorage#ef73c8c5: field file_types element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode optimizeStorage#ef73c8c5: field file_types element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("chat_ids")
	b.ArrStart()
	for _, v := range o.ChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("exclude_chat_ids")
	b.ArrStart()
	for _, v := range o.ExcludeChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("return_deleted_file_statistics")
	b.PutBool(o.ReturnDeletedFileStatistics)
	b.Comma()
	b.FieldStart("chat_limit")
	b.PutInt32(o.ChatLimit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (o *OptimizeStorageRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if o == nil {
		return fmt.Errorf("can't decode optimizeStorage#ef73c8c5 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("optimizeStorage"); err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: %w", err)
			}
		case "size":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field size: %w", err)
			}
			o.Size = value
		case "ttl":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field ttl: %w", err)
			}
			o.TTL = value
		case "count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field count: %w", err)
			}
			o.Count = value
		case "immunity_delay":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field immunity_delay: %w", err)
			}
			o.ImmunityDelay = value
		case "file_types":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONFileType(b)
				if err != nil {
					return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field file_types: %w", err)
				}
				o.FileTypes = append(o.FileTypes, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field file_types: %w", err)
			}
		case "chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field chat_ids: %w", err)
				}
				o.ChatIDs = append(o.ChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field chat_ids: %w", err)
			}
		case "exclude_chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field exclude_chat_ids: %w", err)
				}
				o.ExcludeChatIDs = append(o.ExcludeChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field exclude_chat_ids: %w", err)
			}
		case "return_deleted_file_statistics":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field return_deleted_file_statistics: %w", err)
			}
			o.ReturnDeletedFileStatistics = value
		case "chat_limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode optimizeStorage#ef73c8c5: field chat_limit: %w", err)
			}
			o.ChatLimit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSize returns value of Size field.
func (o *OptimizeStorageRequest) GetSize() (value int64) {
	if o == nil {
		return
	}
	return o.Size
}

// GetTTL returns value of TTL field.
func (o *OptimizeStorageRequest) GetTTL() (value int32) {
	if o == nil {
		return
	}
	return o.TTL
}

// GetCount returns value of Count field.
func (o *OptimizeStorageRequest) GetCount() (value int32) {
	if o == nil {
		return
	}
	return o.Count
}

// GetImmunityDelay returns value of ImmunityDelay field.
func (o *OptimizeStorageRequest) GetImmunityDelay() (value int32) {
	if o == nil {
		return
	}
	return o.ImmunityDelay
}

// GetFileTypes returns value of FileTypes field.
func (o *OptimizeStorageRequest) GetFileTypes() (value []FileTypeClass) {
	if o == nil {
		return
	}
	return o.FileTypes
}

// GetChatIDs returns value of ChatIDs field.
func (o *OptimizeStorageRequest) GetChatIDs() (value []int64) {
	if o == nil {
		return
	}
	return o.ChatIDs
}

// GetExcludeChatIDs returns value of ExcludeChatIDs field.
func (o *OptimizeStorageRequest) GetExcludeChatIDs() (value []int64) {
	if o == nil {
		return
	}
	return o.ExcludeChatIDs
}

// GetReturnDeletedFileStatistics returns value of ReturnDeletedFileStatistics field.
func (o *OptimizeStorageRequest) GetReturnDeletedFileStatistics() (value bool) {
	if o == nil {
		return
	}
	return o.ReturnDeletedFileStatistics
}

// GetChatLimit returns value of ChatLimit field.
func (o *OptimizeStorageRequest) GetChatLimit() (value int32) {
	if o == nil {
		return
	}
	return o.ChatLimit
}

// OptimizeStorage invokes method optimizeStorage#ef73c8c5 returning error if any.
func (c *Client) OptimizeStorage(ctx context.Context, request *OptimizeStorageRequest) (*StorageStatistics, error) {
	var result StorageStatistics

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