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

// StorageStatistics represents TL type `storageStatistics#69b98672`.
type StorageStatistics struct {
	// Total size of files, in bytes
	Size int64
	// Total number of files
	Count int32
	// Statistics split by chats
	ByChat []StorageStatisticsByChat
}

// StorageStatisticsTypeID is TL type id of StorageStatistics.
const StorageStatisticsTypeID = 0x69b98672

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

func (s *StorageStatistics) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Size == 0) {
		return false
	}
	if !(s.Count == 0) {
		return false
	}
	if !(s.ByChat == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StorageStatistics) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storageStatistics",
		ID:   StorageStatisticsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Size",
			SchemaName: "size",
		},
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "ByChat",
			SchemaName: "by_chat",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StorageStatistics) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storageStatistics#69b98672 as nil")
	}
	b.PutID(StorageStatisticsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StorageStatistics) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storageStatistics#69b98672 as nil")
	}
	b.PutInt53(s.Size)
	b.PutInt32(s.Count)
	b.PutInt(len(s.ByChat))
	for idx, v := range s.ByChat {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare storageStatistics#69b98672: field by_chat element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StorageStatistics) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storageStatistics#69b98672 to nil")
	}
	if err := b.ConsumeID(StorageStatisticsTypeID); err != nil {
		return fmt.Errorf("unable to decode storageStatistics#69b98672: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StorageStatistics) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storageStatistics#69b98672 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode storageStatistics#69b98672: field size: %w", err)
		}
		s.Size = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode storageStatistics#69b98672: field count: %w", err)
		}
		s.Count = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode storageStatistics#69b98672: field by_chat: %w", err)
		}

		if headerLen > 0 {
			s.ByChat = make([]StorageStatisticsByChat, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StorageStatisticsByChat
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare storageStatistics#69b98672: field by_chat: %w", err)
			}
			s.ByChat = append(s.ByChat, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StorageStatistics) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storageStatistics#69b98672 as nil")
	}
	b.ObjStart()
	b.PutID("storageStatistics")
	b.Comma()
	b.FieldStart("size")
	b.PutInt53(s.Size)
	b.Comma()
	b.FieldStart("count")
	b.PutInt32(s.Count)
	b.Comma()
	b.FieldStart("by_chat")
	b.ArrStart()
	for idx, v := range s.ByChat {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode storageStatistics#69b98672: field by_chat element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StorageStatistics) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode storageStatistics#69b98672 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storageStatistics"); err != nil {
				return fmt.Errorf("unable to decode storageStatistics#69b98672: %w", err)
			}
		case "size":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode storageStatistics#69b98672: field size: %w", err)
			}
			s.Size = value
		case "count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode storageStatistics#69b98672: field count: %w", err)
			}
			s.Count = value
		case "by_chat":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value StorageStatisticsByChat
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode storageStatistics#69b98672: field by_chat: %w", err)
				}
				s.ByChat = append(s.ByChat, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode storageStatistics#69b98672: field by_chat: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// GetByChat returns value of ByChat field.
func (s *StorageStatistics) GetByChat() (value []StorageStatisticsByChat) {
	if s == nil {
		return
	}
	return s.ByChat
}
