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

// GroupCallParticipants represents TL type `groupCallParticipants#1281190b`.
type GroupCallParticipants struct {
	// Total number of group call participants
	TotalCount int32
	// Identifiers of the participants
	ParticipantIDs []MessageSenderClass
}

// GroupCallParticipantsTypeID is TL type id of GroupCallParticipants.
const GroupCallParticipantsTypeID = 0x1281190b

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

func (g *GroupCallParticipants) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.TotalCount == 0) {
		return false
	}
	if !(g.ParticipantIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GroupCallParticipants) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "groupCallParticipants",
		ID:   GroupCallParticipantsTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "ParticipantIDs",
			SchemaName: "participant_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GroupCallParticipants) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallParticipants#1281190b as nil")
	}
	b.PutID(GroupCallParticipantsTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GroupCallParticipants) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallParticipants#1281190b as nil")
	}
	b.PutInt32(g.TotalCount)
	b.PutInt(len(g.ParticipantIDs))
	for idx, v := range g.ParticipantIDs {
		if v == nil {
			return fmt.Errorf("unable to encode groupCallParticipants#1281190b: field participant_ids element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare groupCallParticipants#1281190b: field participant_ids element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

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

		if headerLen > 0 {
			g.ParticipantIDs = make([]MessageSenderClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode groupCallParticipants#1281190b: field participant_ids: %w", err)
			}
			g.ParticipantIDs = append(g.ParticipantIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GroupCallParticipants) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallParticipants#1281190b as nil")
	}
	b.ObjStart()
	b.PutID("groupCallParticipants")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(g.TotalCount)
	b.Comma()
	b.FieldStart("participant_ids")
	b.ArrStart()
	for idx, v := range g.ParticipantIDs {
		if v == nil {
			return fmt.Errorf("unable to encode groupCallParticipants#1281190b: field participant_ids element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode groupCallParticipants#1281190b: field participant_ids 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 (g *GroupCallParticipants) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode groupCallParticipants#1281190b to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("groupCallParticipants"); err != nil {
				return fmt.Errorf("unable to decode groupCallParticipants#1281190b: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode groupCallParticipants#1281190b: field total_count: %w", err)
			}
			g.TotalCount = value
		case "participant_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONMessageSender(b)
				if err != nil {
					return fmt.Errorf("unable to decode groupCallParticipants#1281190b: field participant_ids: %w", err)
				}
				g.ParticipantIDs = append(g.ParticipantIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode groupCallParticipants#1281190b: field participant_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetParticipantIDs returns value of ParticipantIDs field.
func (g *GroupCallParticipants) GetParticipantIDs() (value []MessageSenderClass) {
	if g == nil {
		return
	}
	return g.ParticipantIDs
}
