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

// BusinessConnection represents TL type `businessConnection#c4a6c383`.
type BusinessConnection struct {
	// Unique identifier of the connection
	ID string
	// Identifier of the business user that created the connection
	UserID int64
	// Chat identifier of the private chat with the user
	UserChatID int64
	// Point in time (Unix timestamp) when the connection was established
	Date int32
	// Rights of the bot; may be null if the connection was disabled
	Rights BusinessBotRights
	// True, if the connection is enabled; false otherwise
	IsEnabled bool
}

// BusinessConnectionTypeID is TL type id of BusinessConnection.
const BusinessConnectionTypeID = 0xc4a6c383

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

func (b *BusinessConnection) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.ID == "") {
		return false
	}
	if !(b.UserID == 0) {
		return false
	}
	if !(b.UserChatID == 0) {
		return false
	}
	if !(b.Date == 0) {
		return false
	}
	if !(b.Rights.Zero()) {
		return false
	}
	if !(b.IsEnabled == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BusinessConnection) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessConnection",
		ID:   BusinessConnectionTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "UserChatID",
			SchemaName: "user_chat_id",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Rights",
			SchemaName: "rights",
		},
		{
			Name:       "IsEnabled",
			SchemaName: "is_enabled",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BusinessConnection) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessConnection#c4a6c383 as nil")
	}
	buf.PutID(BusinessConnectionTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessConnection) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessConnection#c4a6c383 as nil")
	}
	buf.PutString(b.ID)
	buf.PutInt53(b.UserID)
	buf.PutInt53(b.UserChatID)
	buf.PutInt32(b.Date)
	if err := b.Rights.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessConnection#c4a6c383: field rights: %w", err)
	}
	buf.PutBool(b.IsEnabled)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (b *BusinessConnection) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessConnection#c4a6c383 to nil")
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode businessConnection#c4a6c383: field id: %w", err)
		}
		b.ID = value
	}
	{
		value, err := buf.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode businessConnection#c4a6c383: field user_id: %w", err)
		}
		b.UserID = value
	}
	{
		value, err := buf.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode businessConnection#c4a6c383: field user_chat_id: %w", err)
		}
		b.UserChatID = value
	}
	{
		value, err := buf.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode businessConnection#c4a6c383: field date: %w", err)
		}
		b.Date = value
	}
	{
		if err := b.Rights.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessConnection#c4a6c383: field rights: %w", err)
		}
	}
	{
		value, err := buf.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode businessConnection#c4a6c383: field is_enabled: %w", err)
		}
		b.IsEnabled = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BusinessConnection) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode businessConnection#c4a6c383 as nil")
	}
	buf.ObjStart()
	buf.PutID("businessConnection")
	buf.Comma()
	buf.FieldStart("id")
	buf.PutString(b.ID)
	buf.Comma()
	buf.FieldStart("user_id")
	buf.PutInt53(b.UserID)
	buf.Comma()
	buf.FieldStart("user_chat_id")
	buf.PutInt53(b.UserChatID)
	buf.Comma()
	buf.FieldStart("date")
	buf.PutInt32(b.Date)
	buf.Comma()
	buf.FieldStart("rights")
	if err := b.Rights.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode businessConnection#c4a6c383: field rights: %w", err)
	}
	buf.Comma()
	buf.FieldStart("is_enabled")
	buf.PutBool(b.IsEnabled)
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BusinessConnection) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode businessConnection#c4a6c383 to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("businessConnection"); err != nil {
				return fmt.Errorf("unable to decode businessConnection#c4a6c383: %w", err)
			}
		case "id":
			value, err := buf.String()
			if err != nil {
				return fmt.Errorf("unable to decode businessConnection#c4a6c383: field id: %w", err)
			}
			b.ID = value
		case "user_id":
			value, err := buf.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode businessConnection#c4a6c383: field user_id: %w", err)
			}
			b.UserID = value
		case "user_chat_id":
			value, err := buf.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode businessConnection#c4a6c383: field user_chat_id: %w", err)
			}
			b.UserChatID = value
		case "date":
			value, err := buf.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode businessConnection#c4a6c383: field date: %w", err)
			}
			b.Date = value
		case "rights":
			if err := b.Rights.DecodeTDLibJSON(buf); err != nil {
				return fmt.Errorf("unable to decode businessConnection#c4a6c383: field rights: %w", err)
			}
		case "is_enabled":
			value, err := buf.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode businessConnection#c4a6c383: field is_enabled: %w", err)
			}
			b.IsEnabled = value
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (b *BusinessConnection) GetID() (value string) {
	if b == nil {
		return
	}
	return b.ID
}

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

// GetUserChatID returns value of UserChatID field.
func (b *BusinessConnection) GetUserChatID() (value int64) {
	if b == nil {
		return
	}
	return b.UserChatID
}

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

// GetRights returns value of Rights field.
func (b *BusinessConnection) GetRights() (value BusinessBotRights) {
	if b == nil {
		return
	}
	return b.Rights
}

// GetIsEnabled returns value of IsEnabled field.
func (b *BusinessConnection) GetIsEnabled() (value bool) {
	if b == nil {
		return
	}
	return b.IsEnabled
}
