// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// PhoneGroupCallStreamChannels represents TL type `phone.groupCallStreamChannels#d0e482b2`.
// Info about RTMP streams in a group call or livestream
//
// See https://core.telegram.org/constructor/phone.groupCallStreamChannels for reference.
type PhoneGroupCallStreamChannels struct {
	// RTMP streams
	Channels []GroupCallStreamChannel
}

// PhoneGroupCallStreamChannelsTypeID is TL type id of PhoneGroupCallStreamChannels.
const PhoneGroupCallStreamChannelsTypeID = 0xd0e482b2

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

func (g *PhoneGroupCallStreamChannels) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Channels == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneGroupCallStreamChannels from given interface.
func (g *PhoneGroupCallStreamChannels) FillFrom(from interface {
	GetChannels() (value []GroupCallStreamChannel)
}) {
	g.Channels = from.GetChannels()
}

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

// TypeName returns name of type in TL schema.
func (*PhoneGroupCallStreamChannels) TypeName() string {
	return "phone.groupCallStreamChannels"
}

// TypeInfo returns info about TL type.
func (g *PhoneGroupCallStreamChannels) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.groupCallStreamChannels",
		ID:   PhoneGroupCallStreamChannelsTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Channels",
			SchemaName: "channels",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *PhoneGroupCallStreamChannels) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode phone.groupCallStreamChannels#d0e482b2 as nil")
	}
	b.PutID(PhoneGroupCallStreamChannelsTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *PhoneGroupCallStreamChannels) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode phone.groupCallStreamChannels#d0e482b2 as nil")
	}
	b.PutVectorHeader(len(g.Channels))
	for idx, v := range g.Channels {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode phone.groupCallStreamChannels#d0e482b2: field channels element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *PhoneGroupCallStreamChannels) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode phone.groupCallStreamChannels#d0e482b2 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode phone.groupCallStreamChannels#d0e482b2: field channels: %w", err)
		}

		if headerLen > 0 {
			g.Channels = make([]GroupCallStreamChannel, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value GroupCallStreamChannel
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode phone.groupCallStreamChannels#d0e482b2: field channels: %w", err)
			}
			g.Channels = append(g.Channels, value)
		}
	}
	return nil
}

// GetChannels returns value of Channels field.
func (g *PhoneGroupCallStreamChannels) GetChannels() (value []GroupCallStreamChannel) {
	if g == nil {
		return
	}
	return g.Channels
}
