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

// MessagesToggleBotInAttachMenuRequest represents TL type `messages.toggleBotInAttachMenu#69f59d69`.
// Enable or disable web bot attachment menu »¹
//
// Links:
//  1. https://core.telegram.org/api/bots/attach
//
// See https://core.telegram.org/method/messages.toggleBotInAttachMenu for reference.
type MessagesToggleBotInAttachMenuRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether the user authorizes the bot to write messages to them, if requested by
	// attachMenuBot¹.request_write_access
	//
	// Links:
	//  1) https://core.telegram.org/constructor/attachMenuBot
	WriteAllowed bool
	// Bot ID
	Bot InputUserClass
	// Toggle
	Enabled bool
}

// MessagesToggleBotInAttachMenuRequestTypeID is TL type id of MessagesToggleBotInAttachMenuRequest.
const MessagesToggleBotInAttachMenuRequestTypeID = 0x69f59d69

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

func (t *MessagesToggleBotInAttachMenuRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Flags.Zero()) {
		return false
	}
	if !(t.WriteAllowed == false) {
		return false
	}
	if !(t.Bot == nil) {
		return false
	}
	if !(t.Enabled == false) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesToggleBotInAttachMenuRequest from given interface.
func (t *MessagesToggleBotInAttachMenuRequest) FillFrom(from interface {
	GetWriteAllowed() (value bool)
	GetBot() (value InputUserClass)
	GetEnabled() (value bool)
}) {
	t.WriteAllowed = from.GetWriteAllowed()
	t.Bot = from.GetBot()
	t.Enabled = from.GetEnabled()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesToggleBotInAttachMenuRequest) TypeName() string {
	return "messages.toggleBotInAttachMenu"
}

// TypeInfo returns info about TL type.
func (t *MessagesToggleBotInAttachMenuRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.toggleBotInAttachMenu",
		ID:   MessagesToggleBotInAttachMenuRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "WriteAllowed",
			SchemaName: "write_allowed",
			Null:       !t.Flags.Has(0),
		},
		{
			Name:       "Bot",
			SchemaName: "bot",
		},
		{
			Name:       "Enabled",
			SchemaName: "enabled",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (t *MessagesToggleBotInAttachMenuRequest) SetFlags() {
	if !(t.WriteAllowed == false) {
		t.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (t *MessagesToggleBotInAttachMenuRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode messages.toggleBotInAttachMenu#69f59d69 as nil")
	}
	b.PutID(MessagesToggleBotInAttachMenuRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *MessagesToggleBotInAttachMenuRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode messages.toggleBotInAttachMenu#69f59d69 as nil")
	}
	t.SetFlags()
	if err := t.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.toggleBotInAttachMenu#69f59d69: field flags: %w", err)
	}
	if t.Bot == nil {
		return fmt.Errorf("unable to encode messages.toggleBotInAttachMenu#69f59d69: field bot is nil")
	}
	if err := t.Bot.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.toggleBotInAttachMenu#69f59d69: field bot: %w", err)
	}
	b.PutBool(t.Enabled)
	return nil
}

// Decode implements bin.Decoder.
func (t *MessagesToggleBotInAttachMenuRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode messages.toggleBotInAttachMenu#69f59d69 to nil")
	}
	if err := b.ConsumeID(MessagesToggleBotInAttachMenuRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.toggleBotInAttachMenu#69f59d69: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *MessagesToggleBotInAttachMenuRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode messages.toggleBotInAttachMenu#69f59d69 to nil")
	}
	{
		if err := t.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.toggleBotInAttachMenu#69f59d69: field flags: %w", err)
		}
	}
	t.WriteAllowed = t.Flags.Has(0)
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.toggleBotInAttachMenu#69f59d69: field bot: %w", err)
		}
		t.Bot = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messages.toggleBotInAttachMenu#69f59d69: field enabled: %w", err)
		}
		t.Enabled = value
	}
	return nil
}

// SetWriteAllowed sets value of WriteAllowed conditional field.
func (t *MessagesToggleBotInAttachMenuRequest) SetWriteAllowed(value bool) {
	if value {
		t.Flags.Set(0)
		t.WriteAllowed = true
	} else {
		t.Flags.Unset(0)
		t.WriteAllowed = false
	}
}

// GetWriteAllowed returns value of WriteAllowed conditional field.
func (t *MessagesToggleBotInAttachMenuRequest) GetWriteAllowed() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(0)
}

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

// GetEnabled returns value of Enabled field.
func (t *MessagesToggleBotInAttachMenuRequest) GetEnabled() (value bool) {
	if t == nil {
		return
	}
	return t.Enabled
}

// MessagesToggleBotInAttachMenu invokes method messages.toggleBotInAttachMenu#69f59d69 returning error if any.
// Enable or disable web bot attachment menu »¹
//
// Links:
//  1. https://core.telegram.org/api/bots/attach
//
// Possible errors:
//
//	400 BOT_INVALID: This is not a valid bot.
//
// See https://core.telegram.org/method/messages.toggleBotInAttachMenu for reference.
func (c *Client) MessagesToggleBotInAttachMenu(ctx context.Context, request *MessagesToggleBotInAttachMenuRequest) (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
}
