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

// Chats represents TL type `chats#9b93e3eb`.
type Chats struct {
	// Approximate total number of chats found
	TotalCount int32
	// List of chat identifiers
	ChatIDs []int64
}

// ChatsTypeID is TL type id of Chats.
const ChatsTypeID = 0x9b93e3eb

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

func (c *Chats) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.TotalCount == 0) {
		return false
	}
	if !(c.ChatIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *Chats) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chats",
		ID:   ChatsTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "ChatIDs",
			SchemaName: "chat_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *Chats) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chats#9b93e3eb as nil")
	}
	b.PutID(ChatsTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *Chats) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chats#9b93e3eb as nil")
	}
	b.PutInt32(c.TotalCount)
	b.PutInt(len(c.ChatIDs))
	for _, v := range c.ChatIDs {
		b.PutInt53(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *Chats) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chats#9b93e3eb to nil")
	}
	if err := b.ConsumeID(ChatsTypeID); err != nil {
		return fmt.Errorf("unable to decode chats#9b93e3eb: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *Chats) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chats#9b93e3eb to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chats#9b93e3eb: field total_count: %w", err)
		}
		c.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chats#9b93e3eb: field chat_ids: %w", err)
		}

		if headerLen > 0 {
			c.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 chats#9b93e3eb: field chat_ids: %w", err)
			}
			c.ChatIDs = append(c.ChatIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *Chats) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chats#9b93e3eb as nil")
	}
	b.ObjStart()
	b.PutID("chats")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(c.TotalCount)
	b.Comma()
	b.FieldStart("chat_ids")
	b.ArrStart()
	for _, v := range c.ChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *Chats) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chats#9b93e3eb to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chats"); err != nil {
				return fmt.Errorf("unable to decode chats#9b93e3eb: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chats#9b93e3eb: field total_count: %w", err)
			}
			c.TotalCount = value
		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 chats#9b93e3eb: field chat_ids: %w", err)
				}
				c.ChatIDs = append(c.ChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chats#9b93e3eb: field chat_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (c *Chats) GetTotalCount() (value int32) {
	if c == nil {
		return
	}
	return c.TotalCount
}

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