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

// BusinessChatLink represents TL type `businessChatLink#b4ae666f`.
// Contains info about a business chat deep link »¹ created by the current account.
//
// Links:
//  1. https://core.telegram.org/api/business#business-chat-links
//
// See https://core.telegram.org/constructor/businessChatLink for reference.
type BusinessChatLink struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Business chat deep link¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/links#business-chat-links
	Link string
	// Message to pre-fill in the message input field.
	Message string
	// Message entities for styled text¹
	//
	// Links:
	//  1) https://core.telegram.org/api/entities
	//
	// Use SetEntities and GetEntities helpers.
	Entities []MessageEntityClass
	// Human-readable name of the link, to simplify management in the UI (only visible to the
	// creator of the link).
	//
	// Use SetTitle and GetTitle helpers.
	Title string
	// Number of times the link was resolved (clicked/scanned/etc...).
	Views int
}

// BusinessChatLinkTypeID is TL type id of BusinessChatLink.
const BusinessChatLinkTypeID = 0xb4ae666f

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

func (b *BusinessChatLink) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.Link == "") {
		return false
	}
	if !(b.Message == "") {
		return false
	}
	if !(b.Entities == nil) {
		return false
	}
	if !(b.Title == "") {
		return false
	}
	if !(b.Views == 0) {
		return false
	}

	return true
}

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

// FillFrom fills BusinessChatLink from given interface.
func (b *BusinessChatLink) FillFrom(from interface {
	GetLink() (value string)
	GetMessage() (value string)
	GetEntities() (value []MessageEntityClass, ok bool)
	GetTitle() (value string, ok bool)
	GetViews() (value int)
}) {
	b.Link = from.GetLink()
	b.Message = from.GetMessage()
	if val, ok := from.GetEntities(); ok {
		b.Entities = val
	}

	if val, ok := from.GetTitle(); ok {
		b.Title = val
	}

	b.Views = from.GetViews()
}

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

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

// TypeInfo returns info about TL type.
func (b *BusinessChatLink) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessChatLink",
		ID:   BusinessChatLinkTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Link",
			SchemaName: "link",
		},
		{
			Name:       "Message",
			SchemaName: "message",
		},
		{
			Name:       "Entities",
			SchemaName: "entities",
			Null:       !b.Flags.Has(0),
		},
		{
			Name:       "Title",
			SchemaName: "title",
			Null:       !b.Flags.Has(1),
		},
		{
			Name:       "Views",
			SchemaName: "views",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *BusinessChatLink) SetFlags() {
	if !(b.Entities == nil) {
		b.Flags.Set(0)
	}
	if !(b.Title == "") {
		b.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (b *BusinessChatLink) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessChatLink#b4ae666f as nil")
	}
	buf.PutID(BusinessChatLinkTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessChatLink) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessChatLink#b4ae666f as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessChatLink#b4ae666f: field flags: %w", err)
	}
	buf.PutString(b.Link)
	buf.PutString(b.Message)
	if b.Flags.Has(0) {
		buf.PutVectorHeader(len(b.Entities))
		for idx, v := range b.Entities {
			if v == nil {
				return fmt.Errorf("unable to encode businessChatLink#b4ae666f: field entities element with index %d is nil", idx)
			}
			if err := v.Encode(buf); err != nil {
				return fmt.Errorf("unable to encode businessChatLink#b4ae666f: field entities element with index %d: %w", idx, err)
			}
		}
	}
	if b.Flags.Has(1) {
		buf.PutString(b.Title)
	}
	buf.PutInt(b.Views)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (b *BusinessChatLink) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessChatLink#b4ae666f to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessChatLink#b4ae666f: field flags: %w", err)
		}
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode businessChatLink#b4ae666f: field link: %w", err)
		}
		b.Link = value
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode businessChatLink#b4ae666f: field message: %w", err)
		}
		b.Message = value
	}
	if b.Flags.Has(0) {
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode businessChatLink#b4ae666f: field entities: %w", err)
		}

		if headerLen > 0 {
			b.Entities = make([]MessageEntityClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeMessageEntity(buf)
			if err != nil {
				return fmt.Errorf("unable to decode businessChatLink#b4ae666f: field entities: %w", err)
			}
			b.Entities = append(b.Entities, value)
		}
	}
	if b.Flags.Has(1) {
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode businessChatLink#b4ae666f: field title: %w", err)
		}
		b.Title = value
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode businessChatLink#b4ae666f: field views: %w", err)
		}
		b.Views = value
	}
	return nil
}

// GetLink returns value of Link field.
func (b *BusinessChatLink) GetLink() (value string) {
	if b == nil {
		return
	}
	return b.Link
}

// GetMessage returns value of Message field.
func (b *BusinessChatLink) GetMessage() (value string) {
	if b == nil {
		return
	}
	return b.Message
}

// SetEntities sets value of Entities conditional field.
func (b *BusinessChatLink) SetEntities(value []MessageEntityClass) {
	b.Flags.Set(0)
	b.Entities = value
}

// GetEntities returns value of Entities conditional field and
// boolean which is true if field was set.
func (b *BusinessChatLink) GetEntities() (value []MessageEntityClass, ok bool) {
	if b == nil {
		return
	}
	if !b.Flags.Has(0) {
		return value, false
	}
	return b.Entities, true
}

// SetTitle sets value of Title conditional field.
func (b *BusinessChatLink) SetTitle(value string) {
	b.Flags.Set(1)
	b.Title = value
}

// GetTitle returns value of Title conditional field and
// boolean which is true if field was set.
func (b *BusinessChatLink) GetTitle() (value string, ok bool) {
	if b == nil {
		return
	}
	if !b.Flags.Has(1) {
		return value, false
	}
	return b.Title, true
}

// GetViews returns value of Views field.
func (b *BusinessChatLink) GetViews() (value int) {
	if b == nil {
		return
	}
	return b.Views
}

// MapEntities returns field Entities wrapped in MessageEntityClassArray helper.
func (b *BusinessChatLink) MapEntities() (value MessageEntityClassArray, ok bool) {
	if !b.Flags.Has(0) {
		return value, false
	}
	return MessageEntityClassArray(b.Entities), true
}
