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

// BusinessRecipients represents TL type `businessRecipients#21108ff7`.
// Specifies the chats that can receive Telegram Business away »¹ and greeting »²
// messages.
// If exclude_selected is set, specifies all chats that cannot receive Telegram Business
// away »¹ and greeting »² messages.
//
// Links:
//  1. https://core.telegram.org/api/business#away-messages
//  2. https://core.telegram.org/api/business#greeting-messages
//  3. https://core.telegram.org/api/business#away-messages
//  4. https://core.telegram.org/api/business#greeting-messages
//
// See https://core.telegram.org/constructor/businessRecipients for reference.
type BusinessRecipients struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// All existing private chats.
	ExistingChats bool
	// All new private chats.
	NewChats bool
	// All private chats with contacts.
	Contacts bool
	// All private chats with non-contacts.
	NonContacts bool
	// If set, inverts the selection.
	ExcludeSelected bool
	// Only private chats with the specified users.
	//
	// Use SetUsers and GetUsers helpers.
	Users []int64
}

// BusinessRecipientsTypeID is TL type id of BusinessRecipients.
const BusinessRecipientsTypeID = 0x21108ff7

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

func (b *BusinessRecipients) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.ExistingChats == false) {
		return false
	}
	if !(b.NewChats == false) {
		return false
	}
	if !(b.Contacts == false) {
		return false
	}
	if !(b.NonContacts == false) {
		return false
	}
	if !(b.ExcludeSelected == false) {
		return false
	}
	if !(b.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BusinessRecipients from given interface.
func (b *BusinessRecipients) FillFrom(from interface {
	GetExistingChats() (value bool)
	GetNewChats() (value bool)
	GetContacts() (value bool)
	GetNonContacts() (value bool)
	GetExcludeSelected() (value bool)
	GetUsers() (value []int64, ok bool)
}) {
	b.ExistingChats = from.GetExistingChats()
	b.NewChats = from.GetNewChats()
	b.Contacts = from.GetContacts()
	b.NonContacts = from.GetNonContacts()
	b.ExcludeSelected = from.GetExcludeSelected()
	if val, ok := from.GetUsers(); ok {
		b.Users = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (b *BusinessRecipients) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessRecipients",
		ID:   BusinessRecipientsTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ExistingChats",
			SchemaName: "existing_chats",
			Null:       !b.Flags.Has(0),
		},
		{
			Name:       "NewChats",
			SchemaName: "new_chats",
			Null:       !b.Flags.Has(1),
		},
		{
			Name:       "Contacts",
			SchemaName: "contacts",
			Null:       !b.Flags.Has(2),
		},
		{
			Name:       "NonContacts",
			SchemaName: "non_contacts",
			Null:       !b.Flags.Has(3),
		},
		{
			Name:       "ExcludeSelected",
			SchemaName: "exclude_selected",
			Null:       !b.Flags.Has(5),
		},
		{
			Name:       "Users",
			SchemaName: "users",
			Null:       !b.Flags.Has(4),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *BusinessRecipients) SetFlags() {
	if !(b.ExistingChats == false) {
		b.Flags.Set(0)
	}
	if !(b.NewChats == false) {
		b.Flags.Set(1)
	}
	if !(b.Contacts == false) {
		b.Flags.Set(2)
	}
	if !(b.NonContacts == false) {
		b.Flags.Set(3)
	}
	if !(b.ExcludeSelected == false) {
		b.Flags.Set(5)
	}
	if !(b.Users == nil) {
		b.Flags.Set(4)
	}
}

// Encode implements bin.Encoder.
func (b *BusinessRecipients) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessRecipients#21108ff7 as nil")
	}
	buf.PutID(BusinessRecipientsTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessRecipients) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessRecipients#21108ff7 as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessRecipients#21108ff7: field flags: %w", err)
	}
	if b.Flags.Has(4) {
		buf.PutVectorHeader(len(b.Users))
		for _, v := range b.Users {
			buf.PutLong(v)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (b *BusinessRecipients) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessRecipients#21108ff7 to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessRecipients#21108ff7: field flags: %w", err)
		}
	}
	b.ExistingChats = b.Flags.Has(0)
	b.NewChats = b.Flags.Has(1)
	b.Contacts = b.Flags.Has(2)
	b.NonContacts = b.Flags.Has(3)
	b.ExcludeSelected = b.Flags.Has(5)
	if b.Flags.Has(4) {
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode businessRecipients#21108ff7: field users: %w", err)
		}

		if headerLen > 0 {
			b.Users = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := buf.Long()
			if err != nil {
				return fmt.Errorf("unable to decode businessRecipients#21108ff7: field users: %w", err)
			}
			b.Users = append(b.Users, value)
		}
	}
	return nil
}

// SetExistingChats sets value of ExistingChats conditional field.
func (b *BusinessRecipients) SetExistingChats(value bool) {
	if value {
		b.Flags.Set(0)
		b.ExistingChats = true
	} else {
		b.Flags.Unset(0)
		b.ExistingChats = false
	}
}

// GetExistingChats returns value of ExistingChats conditional field.
func (b *BusinessRecipients) GetExistingChats() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(0)
}

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

// GetNewChats returns value of NewChats conditional field.
func (b *BusinessRecipients) GetNewChats() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(1)
}

// SetContacts sets value of Contacts conditional field.
func (b *BusinessRecipients) SetContacts(value bool) {
	if value {
		b.Flags.Set(2)
		b.Contacts = true
	} else {
		b.Flags.Unset(2)
		b.Contacts = false
	}
}

// GetContacts returns value of Contacts conditional field.
func (b *BusinessRecipients) GetContacts() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(2)
}

// SetNonContacts sets value of NonContacts conditional field.
func (b *BusinessRecipients) SetNonContacts(value bool) {
	if value {
		b.Flags.Set(3)
		b.NonContacts = true
	} else {
		b.Flags.Unset(3)
		b.NonContacts = false
	}
}

// GetNonContacts returns value of NonContacts conditional field.
func (b *BusinessRecipients) GetNonContacts() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(3)
}

// SetExcludeSelected sets value of ExcludeSelected conditional field.
func (b *BusinessRecipients) SetExcludeSelected(value bool) {
	if value {
		b.Flags.Set(5)
		b.ExcludeSelected = true
	} else {
		b.Flags.Unset(5)
		b.ExcludeSelected = false
	}
}

// GetExcludeSelected returns value of ExcludeSelected conditional field.
func (b *BusinessRecipients) GetExcludeSelected() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(5)
}

// SetUsers sets value of Users conditional field.
func (b *BusinessRecipients) SetUsers(value []int64) {
	b.Flags.Set(4)
	b.Users = value
}

// GetUsers returns value of Users conditional field and
// boolean which is true if field was set.
func (b *BusinessRecipients) GetUsers() (value []int64, ok bool) {
	if b == nil {
		return
	}
	if !b.Flags.Has(4) {
		return value, false
	}
	return b.Users, true
}
