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

// GroupCallVideoSourceGroup represents TL type `groupCallVideoSourceGroup#4ef8a4af`.
type GroupCallVideoSourceGroup struct {
	// The semantics of sources, one of "SIM" or "FID"
	Semantics string
	// The list of synchronization source identifiers
	SourceIDs []int32
}

// GroupCallVideoSourceGroupTypeID is TL type id of GroupCallVideoSourceGroup.
const GroupCallVideoSourceGroupTypeID = 0x4ef8a4af

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

func (g *GroupCallVideoSourceGroup) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Semantics == "") {
		return false
	}
	if !(g.SourceIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GroupCallVideoSourceGroup) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "groupCallVideoSourceGroup",
		ID:   GroupCallVideoSourceGroupTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Semantics",
			SchemaName: "semantics",
		},
		{
			Name:       "SourceIDs",
			SchemaName: "source_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GroupCallVideoSourceGroup) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallVideoSourceGroup#4ef8a4af as nil")
	}
	b.PutID(GroupCallVideoSourceGroupTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GroupCallVideoSourceGroup) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallVideoSourceGroup#4ef8a4af as nil")
	}
	b.PutString(g.Semantics)
	b.PutInt(len(g.SourceIDs))
	for _, v := range g.SourceIDs {
		b.PutInt32(v)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GroupCallVideoSourceGroup) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode groupCallVideoSourceGroup#4ef8a4af to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallVideoSourceGroup#4ef8a4af: field semantics: %w", err)
		}
		g.Semantics = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallVideoSourceGroup#4ef8a4af: field source_ids: %w", err)
		}

		if headerLen > 0 {
			g.SourceIDs = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode groupCallVideoSourceGroup#4ef8a4af: field source_ids: %w", err)
			}
			g.SourceIDs = append(g.SourceIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GroupCallVideoSourceGroup) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallVideoSourceGroup#4ef8a4af as nil")
	}
	b.ObjStart()
	b.PutID("groupCallVideoSourceGroup")
	b.Comma()
	b.FieldStart("semantics")
	b.PutString(g.Semantics)
	b.Comma()
	b.FieldStart("source_ids")
	b.ArrStart()
	for _, v := range g.SourceIDs {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GroupCallVideoSourceGroup) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode groupCallVideoSourceGroup#4ef8a4af to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("groupCallVideoSourceGroup"); err != nil {
				return fmt.Errorf("unable to decode groupCallVideoSourceGroup#4ef8a4af: %w", err)
			}
		case "semantics":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode groupCallVideoSourceGroup#4ef8a4af: field semantics: %w", err)
			}
			g.Semantics = value
		case "source_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode groupCallVideoSourceGroup#4ef8a4af: field source_ids: %w", err)
				}
				g.SourceIDs = append(g.SourceIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode groupCallVideoSourceGroup#4ef8a4af: field source_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSemantics returns value of Semantics field.
func (g *GroupCallVideoSourceGroup) GetSemantics() (value string) {
	if g == nil {
		return
	}
	return g.Semantics
}

// GetSourceIDs returns value of SourceIDs field.
func (g *GroupCallVideoSourceGroup) GetSourceIDs() (value []int32) {
	if g == nil {
		return
	}
	return g.SourceIDs
}
