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

// GetBusinessConnectionRequest represents TL type `getBusinessConnection#81f42420`.
type GetBusinessConnectionRequest struct {
	// Identifier of the business connection to return
	ConnectionID string
}

// GetBusinessConnectionRequestTypeID is TL type id of GetBusinessConnectionRequest.
const GetBusinessConnectionRequestTypeID = 0x81f42420

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

func (g *GetBusinessConnectionRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ConnectionID == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetBusinessConnectionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getBusinessConnection",
		ID:   GetBusinessConnectionRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ConnectionID",
			SchemaName: "connection_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetBusinessConnectionRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessConnection#81f42420 as nil")
	}
	b.PutID(GetBusinessConnectionRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetBusinessConnectionRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessConnection#81f42420 as nil")
	}
	b.PutString(g.ConnectionID)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetBusinessConnectionRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBusinessConnection#81f42420 to nil")
	}
	if err := b.ConsumeID(GetBusinessConnectionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getBusinessConnection#81f42420: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetBusinessConnectionRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBusinessConnection#81f42420 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getBusinessConnection#81f42420: field connection_id: %w", err)
		}
		g.ConnectionID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetBusinessConnectionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessConnection#81f42420 as nil")
	}
	b.ObjStart()
	b.PutID("getBusinessConnection")
	b.Comma()
	b.FieldStart("connection_id")
	b.PutString(g.ConnectionID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetBusinessConnectionRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getBusinessConnection#81f42420 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getBusinessConnection"); err != nil {
				return fmt.Errorf("unable to decode getBusinessConnection#81f42420: %w", err)
			}
		case "connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getBusinessConnection#81f42420: field connection_id: %w", err)
			}
			g.ConnectionID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetConnectionID returns value of ConnectionID field.
func (g *GetBusinessConnectionRequest) GetConnectionID() (value string) {
	if g == nil {
		return
	}
	return g.ConnectionID
}

// GetBusinessConnection invokes method getBusinessConnection#81f42420 returning error if any.
func (c *Client) GetBusinessConnection(ctx context.Context, connectionid string) (*BusinessConnection, error) {
	var result BusinessConnection

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