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

// ChatJoinRequests represents TL type `chatJoinRequests#b2c5fd31`.
type ChatJoinRequests struct {
	// Approximate total number of requests found
	TotalCount int32
	// List of the requests
	Requests []ChatJoinRequest
}

// ChatJoinRequestsTypeID is TL type id of ChatJoinRequests.
const ChatJoinRequestsTypeID = 0xb2c5fd31

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatJoinRequests) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatJoinRequests",
		ID:   ChatJoinRequestsTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Requests",
			SchemaName: "requests",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatJoinRequests) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatJoinRequests#b2c5fd31 as nil")
	}
	b.PutID(ChatJoinRequestsTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatJoinRequests) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatJoinRequests#b2c5fd31 as nil")
	}
	b.PutInt32(c.TotalCount)
	b.PutInt(len(c.Requests))
	for idx, v := range c.Requests {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare chatJoinRequests#b2c5fd31: field requests element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

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

		if headerLen > 0 {
			c.Requests = make([]ChatJoinRequest, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ChatJoinRequest
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare chatJoinRequests#b2c5fd31: field requests: %w", err)
			}
			c.Requests = append(c.Requests, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatJoinRequests) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatJoinRequests#b2c5fd31 as nil")
	}
	b.ObjStart()
	b.PutID("chatJoinRequests")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(c.TotalCount)
	b.Comma()
	b.FieldStart("requests")
	b.ArrStart()
	for idx, v := range c.Requests {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode chatJoinRequests#b2c5fd31: field requests 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 (c *ChatJoinRequests) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatJoinRequests#b2c5fd31 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatJoinRequests"); err != nil {
				return fmt.Errorf("unable to decode chatJoinRequests#b2c5fd31: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatJoinRequests#b2c5fd31: field total_count: %w", err)
			}
			c.TotalCount = value
		case "requests":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value ChatJoinRequest
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode chatJoinRequests#b2c5fd31: field requests: %w", err)
				}
				c.Requests = append(c.Requests, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatJoinRequests#b2c5fd31: field requests: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetRequests returns value of Requests field.
func (c *ChatJoinRequests) GetRequests() (value []ChatJoinRequest) {
	if c == nil {
		return
	}
	return c.Requests
}
