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

// AccountUpdateConnectedBotRequest represents TL type `account.updateConnectedBot#66a08c7e`.
// Connect a business bot »¹ to the current account, or to change the current
// connection settings.
//
// Links:
//  1. https://core.telegram.org/api/bots/connected-business-bots
//
// See https://core.telegram.org/method/account.updateConnectedBot for reference.
type AccountUpdateConnectedBotRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to fully disconnect the bot from the current account.
	Deleted bool
	// Business bot rights.
	//
	// Use SetRights and GetRights helpers.
	Rights BusinessBotRights
	// The bot to connect or disconnect
	Bot InputUserClass
	// Configuration for the business connection
	Recipients InputBusinessBotRecipients
}

// AccountUpdateConnectedBotRequestTypeID is TL type id of AccountUpdateConnectedBotRequest.
const AccountUpdateConnectedBotRequestTypeID = 0x66a08c7e

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

func (u *AccountUpdateConnectedBotRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.Deleted == false) {
		return false
	}
	if !(u.Rights.Zero()) {
		return false
	}
	if !(u.Bot == nil) {
		return false
	}
	if !(u.Recipients.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills AccountUpdateConnectedBotRequest from given interface.
func (u *AccountUpdateConnectedBotRequest) FillFrom(from interface {
	GetDeleted() (value bool)
	GetRights() (value BusinessBotRights, ok bool)
	GetBot() (value InputUserClass)
	GetRecipients() (value InputBusinessBotRecipients)
}) {
	u.Deleted = from.GetDeleted()
	if val, ok := from.GetRights(); ok {
		u.Rights = val
	}

	u.Bot = from.GetBot()
	u.Recipients = from.GetRecipients()
}

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

// TypeName returns name of type in TL schema.
func (*AccountUpdateConnectedBotRequest) TypeName() string {
	return "account.updateConnectedBot"
}

// TypeInfo returns info about TL type.
func (u *AccountUpdateConnectedBotRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.updateConnectedBot",
		ID:   AccountUpdateConnectedBotRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Deleted",
			SchemaName: "deleted",
			Null:       !u.Flags.Has(1),
		},
		{
			Name:       "Rights",
			SchemaName: "rights",
			Null:       !u.Flags.Has(0),
		},
		{
			Name:       "Bot",
			SchemaName: "bot",
		},
		{
			Name:       "Recipients",
			SchemaName: "recipients",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *AccountUpdateConnectedBotRequest) SetFlags() {
	if !(u.Deleted == false) {
		u.Flags.Set(1)
	}
	if !(u.Rights.Zero()) {
		u.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (u *AccountUpdateConnectedBotRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateConnectedBot#66a08c7e as nil")
	}
	b.PutID(AccountUpdateConnectedBotRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *AccountUpdateConnectedBotRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateConnectedBot#66a08c7e as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.updateConnectedBot#66a08c7e: field flags: %w", err)
	}
	if u.Flags.Has(0) {
		if err := u.Rights.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.updateConnectedBot#66a08c7e: field rights: %w", err)
		}
	}
	if u.Bot == nil {
		return fmt.Errorf("unable to encode account.updateConnectedBot#66a08c7e: field bot is nil")
	}
	if err := u.Bot.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.updateConnectedBot#66a08c7e: field bot: %w", err)
	}
	if err := u.Recipients.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.updateConnectedBot#66a08c7e: field recipients: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *AccountUpdateConnectedBotRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode account.updateConnectedBot#66a08c7e to nil")
	}
	if err := b.ConsumeID(AccountUpdateConnectedBotRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.updateConnectedBot#66a08c7e: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *AccountUpdateConnectedBotRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode account.updateConnectedBot#66a08c7e to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.updateConnectedBot#66a08c7e: field flags: %w", err)
		}
	}
	u.Deleted = u.Flags.Has(1)
	if u.Flags.Has(0) {
		if err := u.Rights.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.updateConnectedBot#66a08c7e: field rights: %w", err)
		}
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.updateConnectedBot#66a08c7e: field bot: %w", err)
		}
		u.Bot = value
	}
	{
		if err := u.Recipients.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.updateConnectedBot#66a08c7e: field recipients: %w", err)
		}
	}
	return nil
}

// SetDeleted sets value of Deleted conditional field.
func (u *AccountUpdateConnectedBotRequest) SetDeleted(value bool) {
	if value {
		u.Flags.Set(1)
		u.Deleted = true
	} else {
		u.Flags.Unset(1)
		u.Deleted = false
	}
}

// GetDeleted returns value of Deleted conditional field.
func (u *AccountUpdateConnectedBotRequest) GetDeleted() (value bool) {
	if u == nil {
		return
	}
	return u.Flags.Has(1)
}

// SetRights sets value of Rights conditional field.
func (u *AccountUpdateConnectedBotRequest) SetRights(value BusinessBotRights) {
	u.Flags.Set(0)
	u.Rights = value
}

// GetRights returns value of Rights conditional field and
// boolean which is true if field was set.
func (u *AccountUpdateConnectedBotRequest) GetRights() (value BusinessBotRights, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(0) {
		return value, false
	}
	return u.Rights, true
}

// GetBot returns value of Bot field.
func (u *AccountUpdateConnectedBotRequest) GetBot() (value InputUserClass) {
	if u == nil {
		return
	}
	return u.Bot
}

// GetRecipients returns value of Recipients field.
func (u *AccountUpdateConnectedBotRequest) GetRecipients() (value InputBusinessBotRecipients) {
	if u == nil {
		return
	}
	return u.Recipients
}

// AccountUpdateConnectedBot invokes method account.updateConnectedBot#66a08c7e returning error if any.
// Connect a business bot »¹ to the current account, or to change the current
// connection settings.
//
// Links:
//  1. https://core.telegram.org/api/bots/connected-business-bots
//
// Possible errors:
//
//	400 BOT_BUSINESS_MISSING: The specified bot is not a business bot (the user.bot_business flag is not set).
//	400 BUSINESS_RECIPIENTS_EMPTY: You didn't set any flag in inputBusinessBotRecipients, thus the bot cannot work with any peer.
//	403 PREMIUM_ACCOUNT_REQUIRED: A premium account is required to execute this action.
//
// See https://core.telegram.org/method/account.updateConnectedBot for reference.
func (c *Client) AccountUpdateConnectedBot(ctx context.Context, request *AccountUpdateConnectedBotRequest) (UpdatesClass, error) {
	var result UpdatesBox

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