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

// DecryptGroupCallDataRequest represents TL type `decryptGroupCallData#6a333de4`.
type DecryptGroupCallDataRequest struct {
	// Group call identifier. The call must not be a video chat
	GroupCallID int32
	// Identifier of the group call participant, which sent the data
	ParticipantID MessageSenderClass
	// Data channel for which data was encrypted; pass null if unknown
	DataChannel GroupCallDataChannelClass
	// Data to decrypt
	Data []byte
}

// DecryptGroupCallDataRequestTypeID is TL type id of DecryptGroupCallDataRequest.
const DecryptGroupCallDataRequestTypeID = 0x6a333de4

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

func (d *DecryptGroupCallDataRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.GroupCallID == 0) {
		return false
	}
	if !(d.ParticipantID == nil) {
		return false
	}
	if !(d.DataChannel == nil) {
		return false
	}
	if !(d.Data == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DecryptGroupCallDataRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "decryptGroupCallData",
		ID:   DecryptGroupCallDataRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GroupCallID",
			SchemaName: "group_call_id",
		},
		{
			Name:       "ParticipantID",
			SchemaName: "participant_id",
		},
		{
			Name:       "DataChannel",
			SchemaName: "data_channel",
		},
		{
			Name:       "Data",
			SchemaName: "data",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DecryptGroupCallDataRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode decryptGroupCallData#6a333de4 as nil")
	}
	b.PutID(DecryptGroupCallDataRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DecryptGroupCallDataRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode decryptGroupCallData#6a333de4 as nil")
	}
	b.PutInt32(d.GroupCallID)
	if d.ParticipantID == nil {
		return fmt.Errorf("unable to encode decryptGroupCallData#6a333de4: field participant_id is nil")
	}
	if err := d.ParticipantID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode decryptGroupCallData#6a333de4: field participant_id: %w", err)
	}
	if d.DataChannel == nil {
		return fmt.Errorf("unable to encode decryptGroupCallData#6a333de4: field data_channel is nil")
	}
	if err := d.DataChannel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode decryptGroupCallData#6a333de4: field data_channel: %w", err)
	}
	b.PutBytes(d.Data)
	return nil
}

// Decode implements bin.Decoder.
func (d *DecryptGroupCallDataRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode decryptGroupCallData#6a333de4 to nil")
	}
	if err := b.ConsumeID(DecryptGroupCallDataRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode decryptGroupCallData#6a333de4: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DecryptGroupCallDataRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode decryptGroupCallData#6a333de4 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode decryptGroupCallData#6a333de4: field group_call_id: %w", err)
		}
		d.GroupCallID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode decryptGroupCallData#6a333de4: field participant_id: %w", err)
		}
		d.ParticipantID = value
	}
	{
		value, err := DecodeGroupCallDataChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode decryptGroupCallData#6a333de4: field data_channel: %w", err)
		}
		d.DataChannel = value
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode decryptGroupCallData#6a333de4: field data: %w", err)
		}
		d.Data = value
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DecryptGroupCallDataRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode decryptGroupCallData#6a333de4 to nil")
	}

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

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

// GetParticipantID returns value of ParticipantID field.
func (d *DecryptGroupCallDataRequest) GetParticipantID() (value MessageSenderClass) {
	if d == nil {
		return
	}
	return d.ParticipantID
}

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

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

// DecryptGroupCallData invokes method decryptGroupCallData#6a333de4 returning error if any.
func (c *Client) DecryptGroupCallData(ctx context.Context, request *DecryptGroupCallDataRequest) (*Data, error) {
	var result Data

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