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

// CreateGroupCallRequest represents TL type `createGroupCall#730a82c0`.
type CreateGroupCallRequest struct {
	// Parameters to join the call; pass null to only create call link without joining the
	// call
	JoinParameters GroupCallJoinParameters
}

// CreateGroupCallRequestTypeID is TL type id of CreateGroupCallRequest.
const CreateGroupCallRequestTypeID = 0x730a82c0

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

func (c *CreateGroupCallRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.JoinParameters.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreateGroupCallRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createGroupCall",
		ID:   CreateGroupCallRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "JoinParameters",
			SchemaName: "join_parameters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CreateGroupCallRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createGroupCall#730a82c0 as nil")
	}
	b.PutID(CreateGroupCallRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreateGroupCallRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createGroupCall#730a82c0 as nil")
	}
	if err := c.JoinParameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode createGroupCall#730a82c0: field join_parameters: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *CreateGroupCallRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createGroupCall#730a82c0 to nil")
	}
	if err := b.ConsumeID(CreateGroupCallRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode createGroupCall#730a82c0: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CreateGroupCallRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createGroupCall#730a82c0 to nil")
	}
	{
		if err := c.JoinParameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode createGroupCall#730a82c0: field join_parameters: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CreateGroupCallRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode createGroupCall#730a82c0 as nil")
	}
	b.ObjStart()
	b.PutID("createGroupCall")
	b.Comma()
	b.FieldStart("join_parameters")
	if err := c.JoinParameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode createGroupCall#730a82c0: field join_parameters: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CreateGroupCallRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode createGroupCall#730a82c0 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("createGroupCall"); err != nil {
				return fmt.Errorf("unable to decode createGroupCall#730a82c0: %w", err)
			}
		case "join_parameters":
			if err := c.JoinParameters.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode createGroupCall#730a82c0: field join_parameters: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// CreateGroupCall invokes method createGroupCall#730a82c0 returning error if any.
func (c *Client) CreateGroupCall(ctx context.Context, joinparameters GroupCallJoinParameters) (*GroupCallInfo, error) {
	var result GroupCallInfo

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