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

// InputBusinessRecipients represents TL type `inputBusinessRecipients#6f8b32aa`.
// 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/inputBusinessRecipients for reference.
type InputBusinessRecipients 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 []InputUserClass
}

// InputBusinessRecipientsTypeID is TL type id of InputBusinessRecipients.
const InputBusinessRecipientsTypeID = 0x6f8b32aa

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

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

	return true
}

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

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

}

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

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

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

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

// Encode implements bin.Encoder.
func (i *InputBusinessRecipients) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBusinessRecipients#6f8b32aa as nil")
	}
	b.PutID(InputBusinessRecipientsTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputBusinessRecipients) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBusinessRecipients#6f8b32aa as nil")
	}
	i.SetFlags()
	if err := i.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputBusinessRecipients#6f8b32aa: field flags: %w", err)
	}
	if i.Flags.Has(4) {
		b.PutVectorHeader(len(i.Users))
		for idx, v := range i.Users {
			if v == nil {
				return fmt.Errorf("unable to encode inputBusinessRecipients#6f8b32aa: field users element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode inputBusinessRecipients#6f8b32aa: field users element with index %d: %w", idx, err)
			}
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InputBusinessRecipients) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBusinessRecipients#6f8b32aa to nil")
	}
	if err := b.ConsumeID(InputBusinessRecipientsTypeID); err != nil {
		return fmt.Errorf("unable to decode inputBusinessRecipients#6f8b32aa: %w", err)
	}
	return i.DecodeBare(b)
}

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

		if headerLen > 0 {
			i.Users = make([]InputUserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputBusinessRecipients#6f8b32aa: field users: %w", err)
			}
			i.Users = append(i.Users, value)
		}
	}
	return nil
}

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

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

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

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

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

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

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

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

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

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

// SetUsers sets value of Users conditional field.
func (i *InputBusinessRecipients) SetUsers(value []InputUserClass) {
	i.Flags.Set(4)
	i.Users = value
}

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

// MapUsers returns field Users wrapped in InputUserClassArray helper.
func (i *InputBusinessRecipients) MapUsers() (value InputUserClassArray, ok bool) {
	if !i.Flags.Has(4) {
		return value, false
	}
	return InputUserClassArray(i.Users), true
}
