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

// GroupCallParticipantVideoInfo represents TL type `groupCallParticipantVideoInfo#70f7eff6`.
type GroupCallParticipantVideoInfo struct {
	// List of synchronization source groups of the video
	SourceGroups []GroupCallVideoSourceGroup
	// Video channel endpoint identifier
	EndpointID string
	// True, if the video is paused. This flag needs to be ignored, if new video frames are
	// received
	IsPaused bool
}

// GroupCallParticipantVideoInfoTypeID is TL type id of GroupCallParticipantVideoInfo.
const GroupCallParticipantVideoInfoTypeID = 0x70f7eff6

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

func (g *GroupCallParticipantVideoInfo) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.SourceGroups == nil) {
		return false
	}
	if !(g.EndpointID == "") {
		return false
	}
	if !(g.IsPaused == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GroupCallParticipantVideoInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "groupCallParticipantVideoInfo",
		ID:   GroupCallParticipantVideoInfoTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SourceGroups",
			SchemaName: "source_groups",
		},
		{
			Name:       "EndpointID",
			SchemaName: "endpoint_id",
		},
		{
			Name:       "IsPaused",
			SchemaName: "is_paused",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GroupCallParticipantVideoInfo) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallParticipantVideoInfo#70f7eff6 as nil")
	}
	b.PutID(GroupCallParticipantVideoInfoTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GroupCallParticipantVideoInfo) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallParticipantVideoInfo#70f7eff6 as nil")
	}
	b.PutInt(len(g.SourceGroups))
	for idx, v := range g.SourceGroups {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare groupCallParticipantVideoInfo#70f7eff6: field source_groups element with index %d: %w", idx, err)
		}
	}
	b.PutString(g.EndpointID)
	b.PutBool(g.IsPaused)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GroupCallParticipantVideoInfo) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode groupCallParticipantVideoInfo#70f7eff6 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallParticipantVideoInfo#70f7eff6: field source_groups: %w", err)
		}

		if headerLen > 0 {
			g.SourceGroups = make([]GroupCallVideoSourceGroup, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value GroupCallVideoSourceGroup
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare groupCallParticipantVideoInfo#70f7eff6: field source_groups: %w", err)
			}
			g.SourceGroups = append(g.SourceGroups, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallParticipantVideoInfo#70f7eff6: field endpoint_id: %w", err)
		}
		g.EndpointID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallParticipantVideoInfo#70f7eff6: field is_paused: %w", err)
		}
		g.IsPaused = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GroupCallParticipantVideoInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallParticipantVideoInfo#70f7eff6 as nil")
	}
	b.ObjStart()
	b.PutID("groupCallParticipantVideoInfo")
	b.Comma()
	b.FieldStart("source_groups")
	b.ArrStart()
	for idx, v := range g.SourceGroups {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode groupCallParticipantVideoInfo#70f7eff6: field source_groups element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("endpoint_id")
	b.PutString(g.EndpointID)
	b.Comma()
	b.FieldStart("is_paused")
	b.PutBool(g.IsPaused)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GroupCallParticipantVideoInfo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode groupCallParticipantVideoInfo#70f7eff6 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("groupCallParticipantVideoInfo"); err != nil {
				return fmt.Errorf("unable to decode groupCallParticipantVideoInfo#70f7eff6: %w", err)
			}
		case "source_groups":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value GroupCallVideoSourceGroup
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode groupCallParticipantVideoInfo#70f7eff6: field source_groups: %w", err)
				}
				g.SourceGroups = append(g.SourceGroups, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode groupCallParticipantVideoInfo#70f7eff6: field source_groups: %w", err)
			}
		case "endpoint_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode groupCallParticipantVideoInfo#70f7eff6: field endpoint_id: %w", err)
			}
			g.EndpointID = value
		case "is_paused":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode groupCallParticipantVideoInfo#70f7eff6: field is_paused: %w", err)
			}
			g.IsPaused = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSourceGroups returns value of SourceGroups field.
func (g *GroupCallParticipantVideoInfo) GetSourceGroups() (value []GroupCallVideoSourceGroup) {
	if g == nil {
		return
	}
	return g.SourceGroups
}

// GetEndpointID returns value of EndpointID field.
func (g *GroupCallParticipantVideoInfo) GetEndpointID() (value string) {
	if g == nil {
		return
	}
	return g.EndpointID
}

// GetIsPaused returns value of IsPaused field.
func (g *GroupCallParticipantVideoInfo) GetIsPaused() (value bool) {
	if g == nil {
		return
	}
	return g.IsPaused
}
