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

// LeaveGroupCallRequest represents TL type `leaveGroupCall#3a6befa9`.
type LeaveGroupCallRequest struct {
	// Group call identifier
	GroupCallID int32
}

// LeaveGroupCallRequestTypeID is TL type id of LeaveGroupCallRequest.
const LeaveGroupCallRequestTypeID = 0x3a6befa9

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

func (l *LeaveGroupCallRequest) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.GroupCallID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LeaveGroupCallRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "leaveGroupCall",
		ID:   LeaveGroupCallRequestTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GroupCallID",
			SchemaName: "group_call_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LeaveGroupCallRequest) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode leaveGroupCall#3a6befa9 as nil")
	}
	b.PutID(LeaveGroupCallRequestTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LeaveGroupCallRequest) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode leaveGroupCall#3a6befa9 as nil")
	}
	b.PutInt32(l.GroupCallID)
	return nil
}

// Decode implements bin.Decoder.
func (l *LeaveGroupCallRequest) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode leaveGroupCall#3a6befa9 to nil")
	}
	if err := b.ConsumeID(LeaveGroupCallRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode leaveGroupCall#3a6befa9: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LeaveGroupCallRequest) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode leaveGroupCall#3a6befa9 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode leaveGroupCall#3a6befa9: field group_call_id: %w", err)
		}
		l.GroupCallID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LeaveGroupCallRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode leaveGroupCall#3a6befa9 as nil")
	}
	b.ObjStart()
	b.PutID("leaveGroupCall")
	b.Comma()
	b.FieldStart("group_call_id")
	b.PutInt32(l.GroupCallID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LeaveGroupCallRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode leaveGroupCall#3a6befa9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("leaveGroupCall"); err != nil {
				return fmt.Errorf("unable to decode leaveGroupCall#3a6befa9: %w", err)
			}
		case "group_call_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode leaveGroupCall#3a6befa9: field group_call_id: %w", err)
			}
			l.GroupCallID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// LeaveGroupCall invokes method leaveGroupCall#3a6befa9 returning error if any.
func (c *Client) LeaveGroupCall(ctx context.Context, groupcallid int32) error {
	var ok Ok

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