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

// BotsEditAccessSettingsRequest represents TL type `bots.editAccessSettings#31813cd8`.
//
// See https://core.telegram.org/method/bots.editAccessSettings for reference.
type BotsEditAccessSettingsRequest struct {
	// Flags field of BotsEditAccessSettingsRequest.
	Flags bin.Fields
	// Restricted field of BotsEditAccessSettingsRequest.
	Restricted bool
	// Bot field of BotsEditAccessSettingsRequest.
	Bot InputUserClass
	// AddUsers field of BotsEditAccessSettingsRequest.
	//
	// Use SetAddUsers and GetAddUsers helpers.
	AddUsers []InputUserClass
}

// BotsEditAccessSettingsRequestTypeID is TL type id of BotsEditAccessSettingsRequest.
const BotsEditAccessSettingsRequestTypeID = 0x31813cd8

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

func (e *BotsEditAccessSettingsRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.Flags.Zero()) {
		return false
	}
	if !(e.Restricted == false) {
		return false
	}
	if !(e.Bot == nil) {
		return false
	}
	if !(e.AddUsers == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BotsEditAccessSettingsRequest from given interface.
func (e *BotsEditAccessSettingsRequest) FillFrom(from interface {
	GetRestricted() (value bool)
	GetBot() (value InputUserClass)
	GetAddUsers() (value []InputUserClass, ok bool)
}) {
	e.Restricted = from.GetRestricted()
	e.Bot = from.GetBot()
	if val, ok := from.GetAddUsers(); ok {
		e.AddUsers = val
	}

}

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

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

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

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

// Encode implements bin.Encoder.
func (e *BotsEditAccessSettingsRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode bots.editAccessSettings#31813cd8 as nil")
	}
	b.PutID(BotsEditAccessSettingsRequestTypeID)
	return e.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (e *BotsEditAccessSettingsRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode bots.editAccessSettings#31813cd8 to nil")
	}
	if err := b.ConsumeID(BotsEditAccessSettingsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.editAccessSettings#31813cd8: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *BotsEditAccessSettingsRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode bots.editAccessSettings#31813cd8 to nil")
	}
	{
		if err := e.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode bots.editAccessSettings#31813cd8: field flags: %w", err)
		}
	}
	e.Restricted = e.Flags.Has(0)
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.editAccessSettings#31813cd8: field bot: %w", err)
		}
		e.Bot = value
	}
	if e.Flags.Has(1) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode bots.editAccessSettings#31813cd8: field add_users: %w", err)
		}

		if headerLen > 0 {
			e.AddUsers = 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 bots.editAccessSettings#31813cd8: field add_users: %w", err)
			}
			e.AddUsers = append(e.AddUsers, value)
		}
	}
	return nil
}

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

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

// GetBot returns value of Bot field.
func (e *BotsEditAccessSettingsRequest) GetBot() (value InputUserClass) {
	if e == nil {
		return
	}
	return e.Bot
}

// SetAddUsers sets value of AddUsers conditional field.
func (e *BotsEditAccessSettingsRequest) SetAddUsers(value []InputUserClass) {
	e.Flags.Set(1)
	e.AddUsers = value
}

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

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

// BotsEditAccessSettings invokes method bots.editAccessSettings#31813cd8 returning error if any.
//
// See https://core.telegram.org/method/bots.editAccessSettings for reference.
func (c *Client) BotsEditAccessSettings(ctx context.Context, request *BotsEditAccessSettingsRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
