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

// GetStorageStatisticsRequest represents TL type `getStorageStatistics#cd254b37`.
type GetStorageStatisticsRequest struct {
	// The maximum number of chats with the largest storage usage for which separate
	// statistics need to be returned. All other chats will be grouped in entries with
	// chat_id == 0. If the chat info database is not used, the chat_limit is ignored and is
	// always set to 0
	ChatLimit int32
}

// GetStorageStatisticsRequestTypeID is TL type id of GetStorageStatisticsRequest.
const GetStorageStatisticsRequestTypeID = 0xcd254b37

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

func (g *GetStorageStatisticsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatLimit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetStorageStatisticsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getStorageStatistics",
		ID:   GetStorageStatisticsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatLimit",
			SchemaName: "chat_limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetStorageStatisticsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStorageStatistics#cd254b37 as nil")
	}
	b.PutID(GetStorageStatisticsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetStorageStatisticsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStorageStatistics#cd254b37 as nil")
	}
	b.PutInt32(g.ChatLimit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetStorageStatisticsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStorageStatistics#cd254b37 to nil")
	}
	if err := b.ConsumeID(GetStorageStatisticsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getStorageStatistics#cd254b37: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetStorageStatisticsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStorageStatistics#cd254b37 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getStorageStatistics#cd254b37: field chat_limit: %w", err)
		}
		g.ChatLimit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetStorageStatisticsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getStorageStatistics#cd254b37 as nil")
	}
	b.ObjStart()
	b.PutID("getStorageStatistics")
	b.Comma()
	b.FieldStart("chat_limit")
	b.PutInt32(g.ChatLimit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetStorageStatisticsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getStorageStatistics#cd254b37 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getStorageStatistics"); err != nil {
				return fmt.Errorf("unable to decode getStorageStatistics#cd254b37: %w", err)
			}
		case "chat_limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getStorageStatistics#cd254b37: field chat_limit: %w", err)
			}
			g.ChatLimit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetStorageStatistics invokes method getStorageStatistics#cd254b37 returning error if any.
func (c *Client) GetStorageStatistics(ctx context.Context, chatlimit int32) (*StorageStatistics, error) {
	var result StorageStatistics

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