// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// InvokeWithBusinessConnectionRequest represents TL type `invokeWithBusinessConnection#dd289f8e`.
// Invoke a method using a Telegram Business Bot connection, see here » for more info,
// including a list of the methods that can be wrapped in this constructor¹.
// Make sure to always send queries wrapped in a invokeWithBusinessConnection to the
// datacenter ID, specified in the dc_id field of the botBusinessConnection¹ that is
// being used.
//
// Links:
//  1. https://core.telegram.org/api/bots/connected-business-bots
//  2. https://core.telegram.org/constructor/botBusinessConnection
//
// See https://core.telegram.org/constructor/invokeWithBusinessConnection for reference.
type InvokeWithBusinessConnectionRequest struct {
	// Business connection ID.
	ConnectionID string
	// The actual query.
	Query bin.Object
}

// InvokeWithBusinessConnectionRequestTypeID is TL type id of InvokeWithBusinessConnectionRequest.
const InvokeWithBusinessConnectionRequestTypeID = 0xdd289f8e

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

func (i *InvokeWithBusinessConnectionRequest) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.ConnectionID == "") {
		return false
	}
	if !(i.Query == nil) {
		return false
	}

	return true
}

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

// FillFrom fills InvokeWithBusinessConnectionRequest from given interface.
func (i *InvokeWithBusinessConnectionRequest) FillFrom(from interface {
	GetConnectionID() (value string)
	GetQuery() (value bin.Object)
}) {
	i.ConnectionID = from.GetConnectionID()
	i.Query = from.GetQuery()
}

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

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

// TypeInfo returns info about TL type.
func (i *InvokeWithBusinessConnectionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "invokeWithBusinessConnection",
		ID:   InvokeWithBusinessConnectionRequestTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ConnectionID",
			SchemaName: "connection_id",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InvokeWithBusinessConnectionRequest) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode invokeWithBusinessConnection#dd289f8e as nil")
	}
	b.PutID(InvokeWithBusinessConnectionRequestTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InvokeWithBusinessConnectionRequest) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode invokeWithBusinessConnection#dd289f8e as nil")
	}
	b.PutString(i.ConnectionID)
	if err := i.Query.Encode(b); err != nil {
		return fmt.Errorf("unable to encode invokeWithBusinessConnection#dd289f8e: field query: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InvokeWithBusinessConnectionRequest) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode invokeWithBusinessConnection#dd289f8e to nil")
	}
	if err := b.ConsumeID(InvokeWithBusinessConnectionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode invokeWithBusinessConnection#dd289f8e: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InvokeWithBusinessConnectionRequest) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode invokeWithBusinessConnection#dd289f8e to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode invokeWithBusinessConnection#dd289f8e: field connection_id: %w", err)
		}
		i.ConnectionID = value
	}
	{
		if err := i.Query.Decode(b); err != nil {
			return fmt.Errorf("unable to decode invokeWithBusinessConnection#dd289f8e: field query: %w", err)
		}
	}
	return nil
}

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

// GetQuery returns value of Query field.
func (i *InvokeWithBusinessConnectionRequest) GetQuery() (value bin.Object) {
	if i == nil {
		return
	}
	return i.Query
}
