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

// AccountToggleSponsoredMessagesRequest represents TL type `account.toggleSponsoredMessages#b9d9a38d`.
// Disable or re-enable Telegram ads for the current Premium¹ account.
// Useful for business owners that may want to launch and view their own Telegram ads via
// the Telegram ad platform »¹.
//
// Links:
//  1. https://core.telegram.org/api/premium
//  2. https://ads.telegram.org
//
// See https://core.telegram.org/method/account.toggleSponsoredMessages for reference.
type AccountToggleSponsoredMessagesRequest struct {
	// Enable or disable ads.
	Enabled bool
}

// AccountToggleSponsoredMessagesRequestTypeID is TL type id of AccountToggleSponsoredMessagesRequest.
const AccountToggleSponsoredMessagesRequestTypeID = 0xb9d9a38d

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

func (t *AccountToggleSponsoredMessagesRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Enabled == false) {
		return false
	}

	return true
}

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

// FillFrom fills AccountToggleSponsoredMessagesRequest from given interface.
func (t *AccountToggleSponsoredMessagesRequest) FillFrom(from interface {
	GetEnabled() (value bool)
}) {
	t.Enabled = from.GetEnabled()
}

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

// TypeName returns name of type in TL schema.
func (*AccountToggleSponsoredMessagesRequest) TypeName() string {
	return "account.toggleSponsoredMessages"
}

// TypeInfo returns info about TL type.
func (t *AccountToggleSponsoredMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.toggleSponsoredMessages",
		ID:   AccountToggleSponsoredMessagesRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Enabled",
			SchemaName: "enabled",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *AccountToggleSponsoredMessagesRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode account.toggleSponsoredMessages#b9d9a38d as nil")
	}
	b.PutID(AccountToggleSponsoredMessagesRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *AccountToggleSponsoredMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode account.toggleSponsoredMessages#b9d9a38d as nil")
	}
	b.PutBool(t.Enabled)
	return nil
}

// Decode implements bin.Decoder.
func (t *AccountToggleSponsoredMessagesRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode account.toggleSponsoredMessages#b9d9a38d to nil")
	}
	if err := b.ConsumeID(AccountToggleSponsoredMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.toggleSponsoredMessages#b9d9a38d: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *AccountToggleSponsoredMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode account.toggleSponsoredMessages#b9d9a38d to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode account.toggleSponsoredMessages#b9d9a38d: field enabled: %w", err)
		}
		t.Enabled = value
	}
	return nil
}

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

// AccountToggleSponsoredMessages invokes method account.toggleSponsoredMessages#b9d9a38d returning error if any.
// Disable or re-enable Telegram ads for the current Premium¹ account.
// Useful for business owners that may want to launch and view their own Telegram ads via
// the Telegram ad platform »¹.
//
// Links:
//  1. https://core.telegram.org/api/premium
//  2. https://ads.telegram.org
//
// See https://core.telegram.org/method/account.toggleSponsoredMessages for reference.
func (c *Client) AccountToggleSponsoredMessages(ctx context.Context, enabled bool) (bool, error) {
	var result BoolBox

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