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

// EncryptGroupCallDataRequest represents TL type `encryptGroupCallData#e9783686`.
type EncryptGroupCallDataRequest struct {
	// Group call identifier. The call must not be a video chat
	GroupCallID int32
	// Data channel for which data is encrypted
	DataChannel GroupCallDataChannelClass
	// Data to encrypt
	Data []byte
	// Size of data prefix that must be kept unencrypted
	UnencryptedPrefixSize int32
}

// EncryptGroupCallDataRequestTypeID is TL type id of EncryptGroupCallDataRequest.
const EncryptGroupCallDataRequestTypeID = 0xe9783686

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

func (e *EncryptGroupCallDataRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.GroupCallID == 0) {
		return false
	}
	if !(e.DataChannel == nil) {
		return false
	}
	if !(e.Data == nil) {
		return false
	}
	if !(e.UnencryptedPrefixSize == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EncryptGroupCallDataRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "encryptGroupCallData",
		ID:   EncryptGroupCallDataRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GroupCallID",
			SchemaName: "group_call_id",
		},
		{
			Name:       "DataChannel",
			SchemaName: "data_channel",
		},
		{
			Name:       "Data",
			SchemaName: "data",
		},
		{
			Name:       "UnencryptedPrefixSize",
			SchemaName: "unencrypted_prefix_size",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EncryptGroupCallDataRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode encryptGroupCallData#e9783686 as nil")
	}
	b.PutID(EncryptGroupCallDataRequestTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EncryptGroupCallDataRequest) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode encryptGroupCallData#e9783686 as nil")
	}
	b.PutInt32(e.GroupCallID)
	if e.DataChannel == nil {
		return fmt.Errorf("unable to encode encryptGroupCallData#e9783686: field data_channel is nil")
	}
	if err := e.DataChannel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode encryptGroupCallData#e9783686: field data_channel: %w", err)
	}
	b.PutBytes(e.Data)
	b.PutInt32(e.UnencryptedPrefixSize)
	return nil
}

// Decode implements bin.Decoder.
func (e *EncryptGroupCallDataRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode encryptGroupCallData#e9783686 to nil")
	}
	if err := b.ConsumeID(EncryptGroupCallDataRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EncryptGroupCallDataRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode encryptGroupCallData#e9783686 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: field group_call_id: %w", err)
		}
		e.GroupCallID = value
	}
	{
		value, err := DecodeGroupCallDataChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: field data_channel: %w", err)
		}
		e.DataChannel = value
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: field data: %w", err)
		}
		e.Data = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: field unencrypted_prefix_size: %w", err)
		}
		e.UnencryptedPrefixSize = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EncryptGroupCallDataRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode encryptGroupCallData#e9783686 as nil")
	}
	b.ObjStart()
	b.PutID("encryptGroupCallData")
	b.Comma()
	b.FieldStart("group_call_id")
	b.PutInt32(e.GroupCallID)
	b.Comma()
	b.FieldStart("data_channel")
	if e.DataChannel == nil {
		return fmt.Errorf("unable to encode encryptGroupCallData#e9783686: field data_channel is nil")
	}
	if err := e.DataChannel.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode encryptGroupCallData#e9783686: field data_channel: %w", err)
	}
	b.Comma()
	b.FieldStart("data")
	b.PutBytes(e.Data)
	b.Comma()
	b.FieldStart("unencrypted_prefix_size")
	b.PutInt32(e.UnencryptedPrefixSize)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EncryptGroupCallDataRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode encryptGroupCallData#e9783686 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("encryptGroupCallData"); err != nil {
				return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: %w", err)
			}
		case "group_call_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: field group_call_id: %w", err)
			}
			e.GroupCallID = value
		case "data_channel":
			value, err := DecodeTDLibJSONGroupCallDataChannel(b)
			if err != nil {
				return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: field data_channel: %w", err)
			}
			e.DataChannel = value
		case "data":
			value, err := b.Bytes()
			if err != nil {
				return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: field data: %w", err)
			}
			e.Data = value
		case "unencrypted_prefix_size":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode encryptGroupCallData#e9783686: field unencrypted_prefix_size: %w", err)
			}
			e.UnencryptedPrefixSize = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetDataChannel returns value of DataChannel field.
func (e *EncryptGroupCallDataRequest) GetDataChannel() (value GroupCallDataChannelClass) {
	if e == nil {
		return
	}
	return e.DataChannel
}

// GetData returns value of Data field.
func (e *EncryptGroupCallDataRequest) GetData() (value []byte) {
	if e == nil {
		return
	}
	return e.Data
}

// GetUnencryptedPrefixSize returns value of UnencryptedPrefixSize field.
func (e *EncryptGroupCallDataRequest) GetUnencryptedPrefixSize() (value int32) {
	if e == nil {
		return
	}
	return e.UnencryptedPrefixSize
}

// EncryptGroupCallData invokes method encryptGroupCallData#e9783686 returning error if any.
func (c *Client) EncryptGroupCallData(ctx context.Context, request *EncryptGroupCallDataRequest) (*Data, error) {
	var result Data

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