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

// JoinGroupCallRequest represents TL type `joinGroupCall#873c7953`.
type JoinGroupCallRequest struct {
	// The group call to join
	InputGroupCall InputGroupCallClass
	// Parameters to join the call
	JoinParameters GroupCallJoinParameters
}

// JoinGroupCallRequestTypeID is TL type id of JoinGroupCallRequest.
const JoinGroupCallRequestTypeID = 0x873c7953

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

func (j *JoinGroupCallRequest) Zero() bool {
	if j == nil {
		return true
	}
	if !(j.InputGroupCall == nil) {
		return false
	}
	if !(j.JoinParameters.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (j *JoinGroupCallRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "joinGroupCall",
		ID:   JoinGroupCallRequestTypeID,
	}
	if j == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "InputGroupCall",
			SchemaName: "input_group_call",
		},
		{
			Name:       "JoinParameters",
			SchemaName: "join_parameters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (j *JoinGroupCallRequest) Encode(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't encode joinGroupCall#873c7953 as nil")
	}
	b.PutID(JoinGroupCallRequestTypeID)
	return j.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (j *JoinGroupCallRequest) EncodeBare(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't encode joinGroupCall#873c7953 as nil")
	}
	if j.InputGroupCall == nil {
		return fmt.Errorf("unable to encode joinGroupCall#873c7953: field input_group_call is nil")
	}
	if err := j.InputGroupCall.Encode(b); err != nil {
		return fmt.Errorf("unable to encode joinGroupCall#873c7953: field input_group_call: %w", err)
	}
	if err := j.JoinParameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode joinGroupCall#873c7953: field join_parameters: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (j *JoinGroupCallRequest) Decode(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't decode joinGroupCall#873c7953 to nil")
	}
	if err := b.ConsumeID(JoinGroupCallRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode joinGroupCall#873c7953: %w", err)
	}
	return j.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (j *JoinGroupCallRequest) DecodeBare(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't decode joinGroupCall#873c7953 to nil")
	}
	{
		value, err := DecodeInputGroupCall(b)
		if err != nil {
			return fmt.Errorf("unable to decode joinGroupCall#873c7953: field input_group_call: %w", err)
		}
		j.InputGroupCall = value
	}
	{
		if err := j.JoinParameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode joinGroupCall#873c7953: field join_parameters: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (j *JoinGroupCallRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if j == nil {
		return fmt.Errorf("can't encode joinGroupCall#873c7953 as nil")
	}
	b.ObjStart()
	b.PutID("joinGroupCall")
	b.Comma()
	b.FieldStart("input_group_call")
	if j.InputGroupCall == nil {
		return fmt.Errorf("unable to encode joinGroupCall#873c7953: field input_group_call is nil")
	}
	if err := j.InputGroupCall.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode joinGroupCall#873c7953: field input_group_call: %w", err)
	}
	b.Comma()
	b.FieldStart("join_parameters")
	if err := j.JoinParameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode joinGroupCall#873c7953: field join_parameters: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (j *JoinGroupCallRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if j == nil {
		return fmt.Errorf("can't decode joinGroupCall#873c7953 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("joinGroupCall"); err != nil {
				return fmt.Errorf("unable to decode joinGroupCall#873c7953: %w", err)
			}
		case "input_group_call":
			value, err := DecodeTDLibJSONInputGroupCall(b)
			if err != nil {
				return fmt.Errorf("unable to decode joinGroupCall#873c7953: field input_group_call: %w", err)
			}
			j.InputGroupCall = value
		case "join_parameters":
			if err := j.JoinParameters.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode joinGroupCall#873c7953: field join_parameters: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetInputGroupCall returns value of InputGroupCall field.
func (j *JoinGroupCallRequest) GetInputGroupCall() (value InputGroupCallClass) {
	if j == nil {
		return
	}
	return j.InputGroupCall
}

// GetJoinParameters returns value of JoinParameters field.
func (j *JoinGroupCallRequest) GetJoinParameters() (value GroupCallJoinParameters) {
	if j == nil {
		return
	}
	return j.JoinParameters
}

// JoinGroupCall invokes method joinGroupCall#873c7953 returning error if any.
func (c *Client) JoinGroupCall(ctx context.Context, request *JoinGroupCallRequest) (*GroupCallInfo, error) {
	var result GroupCallInfo

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