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

// BotsAccessSettings represents TL type `bots.accessSettings#dd1fbf93`.
//
// See https://core.telegram.org/constructor/bots.accessSettings for reference.
type BotsAccessSettings struct {
	// Flags field of BotsAccessSettings.
	Flags bin.Fields
	// Restricted field of BotsAccessSettings.
	Restricted bool
	// AddUsers field of BotsAccessSettings.
	//
	// Use SetAddUsers and GetAddUsers helpers.
	AddUsers []UserClass
}

// BotsAccessSettingsTypeID is TL type id of BotsAccessSettings.
const BotsAccessSettingsTypeID = 0xdd1fbf93

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

func (a *BotsAccessSettings) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Flags.Zero()) {
		return false
	}
	if !(a.Restricted == false) {
		return false
	}
	if !(a.AddUsers == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BotsAccessSettings from given interface.
func (a *BotsAccessSettings) FillFrom(from interface {
	GetRestricted() (value bool)
	GetAddUsers() (value []UserClass, ok bool)
}) {
	a.Restricted = from.GetRestricted()
	if val, ok := from.GetAddUsers(); ok {
		a.AddUsers = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*BotsAccessSettings) TypeName() string {
	return "bots.accessSettings"
}

// TypeInfo returns info about TL type.
func (a *BotsAccessSettings) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.accessSettings",
		ID:   BotsAccessSettingsTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Restricted",
			SchemaName: "restricted",
			Null:       !a.Flags.Has(0),
		},
		{
			Name:       "AddUsers",
			SchemaName: "add_users",
			Null:       !a.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (a *BotsAccessSettings) SetFlags() {
	if !(a.Restricted == false) {
		a.Flags.Set(0)
	}
	if !(a.AddUsers == nil) {
		a.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (a *BotsAccessSettings) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode bots.accessSettings#dd1fbf93 as nil")
	}
	b.PutID(BotsAccessSettingsTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *BotsAccessSettings) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode bots.accessSettings#dd1fbf93 as nil")
	}
	a.SetFlags()
	if err := a.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.accessSettings#dd1fbf93: field flags: %w", err)
	}
	if a.Flags.Has(1) {
		b.PutVectorHeader(len(a.AddUsers))
		for idx, v := range a.AddUsers {
			if v == nil {
				return fmt.Errorf("unable to encode bots.accessSettings#dd1fbf93: field add_users element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode bots.accessSettings#dd1fbf93: field add_users element with index %d: %w", idx, err)
			}
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *BotsAccessSettings) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode bots.accessSettings#dd1fbf93 to nil")
	}
	if err := b.ConsumeID(BotsAccessSettingsTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.accessSettings#dd1fbf93: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *BotsAccessSettings) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode bots.accessSettings#dd1fbf93 to nil")
	}
	{
		if err := a.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode bots.accessSettings#dd1fbf93: field flags: %w", err)
		}
	}
	a.Restricted = a.Flags.Has(0)
	if a.Flags.Has(1) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode bots.accessSettings#dd1fbf93: field add_users: %w", err)
		}

		if headerLen > 0 {
			a.AddUsers = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode bots.accessSettings#dd1fbf93: field add_users: %w", err)
			}
			a.AddUsers = append(a.AddUsers, value)
		}
	}
	return nil
}

// SetRestricted sets value of Restricted conditional field.
func (a *BotsAccessSettings) SetRestricted(value bool) {
	if value {
		a.Flags.Set(0)
		a.Restricted = true
	} else {
		a.Flags.Unset(0)
		a.Restricted = false
	}
}

// GetRestricted returns value of Restricted conditional field.
func (a *BotsAccessSettings) GetRestricted() (value bool) {
	if a == nil {
		return
	}
	return a.Flags.Has(0)
}

// SetAddUsers sets value of AddUsers conditional field.
func (a *BotsAccessSettings) SetAddUsers(value []UserClass) {
	a.Flags.Set(1)
	a.AddUsers = value
}

// GetAddUsers returns value of AddUsers conditional field and
// boolean which is true if field was set.
func (a *BotsAccessSettings) GetAddUsers() (value []UserClass, ok bool) {
	if a == nil {
		return
	}
	if !a.Flags.Has(1) {
		return value, false
	}
	return a.AddUsers, true
}

// MapAddUsers returns field AddUsers wrapped in UserClassArray helper.
func (a *BotsAccessSettings) MapAddUsers() (value UserClassArray, ok bool) {
	if !a.Flags.Has(1) {
		return value, false
	}
	return UserClassArray(a.AddUsers), true
}
