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

// BotBusinessConnection represents TL type `botBusinessConnection#8f34b2f5`.
// Contains info about a bot business connection¹.
//
// Links:
//  1. https://core.telegram.org/api/bots/connected-business-bots
//
// See https://core.telegram.org/constructor/botBusinessConnection for reference.
type BotBusinessConnection struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether this business connection is currently disabled
	Disabled bool
	// Business connection ID, used to identify messages coming from the connection and to
	// reply to them as specified here »¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/bots/connected-business-bots
	ConnectionID string
	// ID of the user that the bot is connected to via this connection.
	UserID int64
	// ID of the datacenter where to send queries wrapped in a invokeWithBusinessConnection¹
	// as specified here »².
	//
	// Links:
	//  1) https://core.telegram.org/method/invokeWithBusinessConnection
	//  2) https://core.telegram.org/api/bots/connected-business-bots
	DCID int
	// When was the connection created.
	Date int
	// Business bot rights.
	//
	// Use SetRights and GetRights helpers.
	Rights BusinessBotRights
}

// BotBusinessConnectionTypeID is TL type id of BotBusinessConnection.
const BotBusinessConnectionTypeID = 0x8f34b2f5

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

func (b *BotBusinessConnection) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.Disabled == false) {
		return false
	}
	if !(b.ConnectionID == "") {
		return false
	}
	if !(b.UserID == 0) {
		return false
	}
	if !(b.DCID == 0) {
		return false
	}
	if !(b.Date == 0) {
		return false
	}
	if !(b.Rights.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills BotBusinessConnection from given interface.
func (b *BotBusinessConnection) FillFrom(from interface {
	GetDisabled() (value bool)
	GetConnectionID() (value string)
	GetUserID() (value int64)
	GetDCID() (value int)
	GetDate() (value int)
	GetRights() (value BusinessBotRights, ok bool)
}) {
	b.Disabled = from.GetDisabled()
	b.ConnectionID = from.GetConnectionID()
	b.UserID = from.GetUserID()
	b.DCID = from.GetDCID()
	b.Date = from.GetDate()
	if val, ok := from.GetRights(); ok {
		b.Rights = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (b *BotBusinessConnection) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botBusinessConnection",
		ID:   BotBusinessConnectionTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Disabled",
			SchemaName: "disabled",
			Null:       !b.Flags.Has(1),
		},
		{
			Name:       "ConnectionID",
			SchemaName: "connection_id",
		},
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "DCID",
			SchemaName: "dc_id",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Rights",
			SchemaName: "rights",
			Null:       !b.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *BotBusinessConnection) SetFlags() {
	if !(b.Disabled == false) {
		b.Flags.Set(1)
	}
	if !(b.Rights.Zero()) {
		b.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (b *BotBusinessConnection) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botBusinessConnection#8f34b2f5 as nil")
	}
	buf.PutID(BotBusinessConnectionTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotBusinessConnection) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botBusinessConnection#8f34b2f5 as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode botBusinessConnection#8f34b2f5: field flags: %w", err)
	}
	buf.PutString(b.ConnectionID)
	buf.PutLong(b.UserID)
	buf.PutInt(b.DCID)
	buf.PutInt(b.Date)
	if b.Flags.Has(2) {
		if err := b.Rights.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode botBusinessConnection#8f34b2f5: field rights: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BotBusinessConnection) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botBusinessConnection#8f34b2f5 to nil")
	}
	if err := buf.ConsumeID(BotBusinessConnectionTypeID); err != nil {
		return fmt.Errorf("unable to decode botBusinessConnection#8f34b2f5: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BotBusinessConnection) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botBusinessConnection#8f34b2f5 to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode botBusinessConnection#8f34b2f5: field flags: %w", err)
		}
	}
	b.Disabled = b.Flags.Has(1)
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode botBusinessConnection#8f34b2f5: field connection_id: %w", err)
		}
		b.ConnectionID = value
	}
	{
		value, err := buf.Long()
		if err != nil {
			return fmt.Errorf("unable to decode botBusinessConnection#8f34b2f5: field user_id: %w", err)
		}
		b.UserID = value
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode botBusinessConnection#8f34b2f5: field dc_id: %w", err)
		}
		b.DCID = value
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode botBusinessConnection#8f34b2f5: field date: %w", err)
		}
		b.Date = value
	}
	if b.Flags.Has(2) {
		if err := b.Rights.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode botBusinessConnection#8f34b2f5: field rights: %w", err)
		}
	}
	return nil
}

// SetDisabled sets value of Disabled conditional field.
func (b *BotBusinessConnection) SetDisabled(value bool) {
	if value {
		b.Flags.Set(1)
		b.Disabled = true
	} else {
		b.Flags.Unset(1)
		b.Disabled = false
	}
}

// GetDisabled returns value of Disabled conditional field.
func (b *BotBusinessConnection) GetDisabled() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(1)
}

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

// GetUserID returns value of UserID field.
func (b *BotBusinessConnection) GetUserID() (value int64) {
	if b == nil {
		return
	}
	return b.UserID
}

// GetDCID returns value of DCID field.
func (b *BotBusinessConnection) GetDCID() (value int) {
	if b == nil {
		return
	}
	return b.DCID
}

// GetDate returns value of Date field.
func (b *BotBusinessConnection) GetDate() (value int) {
	if b == nil {
		return
	}
	return b.Date
}

// SetRights sets value of Rights conditional field.
func (b *BotBusinessConnection) SetRights(value BusinessBotRights) {
	b.Flags.Set(2)
	b.Rights = value
}

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