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

// GetVideoChatStreamSegmentRequest represents TL type `getVideoChatStreamSegment#2e1847f4`.
type GetVideoChatStreamSegmentRequest struct {
	// Group call identifier
	GroupCallID int32
	// Point in time when the stream segment begins; Unix timestamp in milliseconds
	TimeOffset int64
	// Segment duration scale; 0-1. Segment's duration is 1000/(2**scale) milliseconds
	Scale int32
	// Identifier of an audio/video channel to get as received from tgcalls
	ChannelID int32
	// Video quality as received from tgcalls; pass null to get the worst available quality
	VideoQuality GroupCallVideoQualityClass
}

// GetVideoChatStreamSegmentRequestTypeID is TL type id of GetVideoChatStreamSegmentRequest.
const GetVideoChatStreamSegmentRequestTypeID = 0x2e1847f4

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

func (g *GetVideoChatStreamSegmentRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.GroupCallID == 0) {
		return false
	}
	if !(g.TimeOffset == 0) {
		return false
	}
	if !(g.Scale == 0) {
		return false
	}
	if !(g.ChannelID == 0) {
		return false
	}
	if !(g.VideoQuality == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetVideoChatStreamSegmentRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getVideoChatStreamSegment",
		ID:   GetVideoChatStreamSegmentRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GroupCallID",
			SchemaName: "group_call_id",
		},
		{
			Name:       "TimeOffset",
			SchemaName: "time_offset",
		},
		{
			Name:       "Scale",
			SchemaName: "scale",
		},
		{
			Name:       "ChannelID",
			SchemaName: "channel_id",
		},
		{
			Name:       "VideoQuality",
			SchemaName: "video_quality",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetVideoChatStreamSegmentRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getVideoChatStreamSegment#2e1847f4 as nil")
	}
	b.PutID(GetVideoChatStreamSegmentRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetVideoChatStreamSegmentRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getVideoChatStreamSegment#2e1847f4 as nil")
	}
	b.PutInt32(g.GroupCallID)
	b.PutInt53(g.TimeOffset)
	b.PutInt32(g.Scale)
	b.PutInt32(g.ChannelID)
	if g.VideoQuality == nil {
		return fmt.Errorf("unable to encode getVideoChatStreamSegment#2e1847f4: field video_quality is nil")
	}
	if err := g.VideoQuality.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getVideoChatStreamSegment#2e1847f4: field video_quality: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetVideoChatStreamSegmentRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getVideoChatStreamSegment#2e1847f4 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field group_call_id: %w", err)
		}
		g.GroupCallID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field time_offset: %w", err)
		}
		g.TimeOffset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field scale: %w", err)
		}
		g.Scale = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field channel_id: %w", err)
		}
		g.ChannelID = value
	}
	{
		value, err := DecodeGroupCallVideoQuality(b)
		if err != nil {
			return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field video_quality: %w", err)
		}
		g.VideoQuality = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetVideoChatStreamSegmentRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getVideoChatStreamSegment#2e1847f4 as nil")
	}
	b.ObjStart()
	b.PutID("getVideoChatStreamSegment")
	b.Comma()
	b.FieldStart("group_call_id")
	b.PutInt32(g.GroupCallID)
	b.Comma()
	b.FieldStart("time_offset")
	b.PutInt53(g.TimeOffset)
	b.Comma()
	b.FieldStart("scale")
	b.PutInt32(g.Scale)
	b.Comma()
	b.FieldStart("channel_id")
	b.PutInt32(g.ChannelID)
	b.Comma()
	b.FieldStart("video_quality")
	if g.VideoQuality == nil {
		return fmt.Errorf("unable to encode getVideoChatStreamSegment#2e1847f4: field video_quality is nil")
	}
	if err := g.VideoQuality.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getVideoChatStreamSegment#2e1847f4: field video_quality: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetVideoChatStreamSegmentRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getVideoChatStreamSegment#2e1847f4 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getVideoChatStreamSegment"); err != nil {
				return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: %w", err)
			}
		case "group_call_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field group_call_id: %w", err)
			}
			g.GroupCallID = value
		case "time_offset":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field time_offset: %w", err)
			}
			g.TimeOffset = value
		case "scale":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field scale: %w", err)
			}
			g.Scale = value
		case "channel_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field channel_id: %w", err)
			}
			g.ChannelID = value
		case "video_quality":
			value, err := DecodeTDLibJSONGroupCallVideoQuality(b)
			if err != nil {
				return fmt.Errorf("unable to decode getVideoChatStreamSegment#2e1847f4: field video_quality: %w", err)
			}
			g.VideoQuality = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetGroupCallID returns value of GroupCallID field.
func (g *GetVideoChatStreamSegmentRequest) GetGroupCallID() (value int32) {
	if g == nil {
		return
	}
	return g.GroupCallID
}

// GetTimeOffset returns value of TimeOffset field.
func (g *GetVideoChatStreamSegmentRequest) GetTimeOffset() (value int64) {
	if g == nil {
		return
	}
	return g.TimeOffset
}

// GetScale returns value of Scale field.
func (g *GetVideoChatStreamSegmentRequest) GetScale() (value int32) {
	if g == nil {
		return
	}
	return g.Scale
}

// GetChannelID returns value of ChannelID field.
func (g *GetVideoChatStreamSegmentRequest) GetChannelID() (value int32) {
	if g == nil {
		return
	}
	return g.ChannelID
}

// GetVideoQuality returns value of VideoQuality field.
func (g *GetVideoChatStreamSegmentRequest) GetVideoQuality() (value GroupCallVideoQualityClass) {
	if g == nil {
		return
	}
	return g.VideoQuality
}

// GetVideoChatStreamSegment invokes method getVideoChatStreamSegment#2e1847f4 returning error if any.
func (c *Client) GetVideoChatStreamSegment(ctx context.Context, request *GetVideoChatStreamSegmentRequest) (*Data, error) {
	var result Data

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
