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

// AccountToggleNoPaidMessagesExceptionRequest represents TL type `account.toggleNoPaidMessagesException#fe2eda76`.
// Allow a user to send us messages without paying if paid messages »¹ are enabled.
//
// Links:
//  1. https://core.telegram.org/api/paid-messages
//
// See https://core.telegram.org/method/account.toggleNoPaidMessagesException for reference.
type AccountToggleNoPaidMessagesExceptionRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set and require_payment is not set, refunds the amounts the user has already paid
	// us to send us messages (directly or via a monoforum).
	RefundCharged bool
	// If set, requires the user to pay in order to send us messages. Can only be set by
	// monoforums, not users, i.e. parent_peer must be set if this flag is set; users must
	// instead use the inputPrivacyKeyNoPaidMessages¹ privacy setting to remove a previously
	// added exemption. If not set, allows the user to send us messages without paying (can
	// be unset by both monoforums and users).
	//
	// Links:
	//  1) https://core.telegram.org/constructor/inputPrivacyKeyNoPaidMessages
	RequirePayment bool
	// If set, applies the setting within the monoforum aka direct messages »¹ (pass the ID
	// of the monoforum, not the ID of the associated channel).
	//
	// Links:
	//  1) https://core.telegram.org/api/monoforum
	//
	// Use SetParentPeer and GetParentPeer helpers.
	ParentPeer InputPeerClass
	// The user to exempt or unexempt.
	UserID InputUserClass
}

// AccountToggleNoPaidMessagesExceptionRequestTypeID is TL type id of AccountToggleNoPaidMessagesExceptionRequest.
const AccountToggleNoPaidMessagesExceptionRequestTypeID = 0xfe2eda76

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

func (t *AccountToggleNoPaidMessagesExceptionRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Flags.Zero()) {
		return false
	}
	if !(t.RefundCharged == false) {
		return false
	}
	if !(t.RequirePayment == false) {
		return false
	}
	if !(t.ParentPeer == nil) {
		return false
	}
	if !(t.UserID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountToggleNoPaidMessagesExceptionRequest from given interface.
func (t *AccountToggleNoPaidMessagesExceptionRequest) FillFrom(from interface {
	GetRefundCharged() (value bool)
	GetRequirePayment() (value bool)
	GetParentPeer() (value InputPeerClass, ok bool)
	GetUserID() (value InputUserClass)
}) {
	t.RefundCharged = from.GetRefundCharged()
	t.RequirePayment = from.GetRequirePayment()
	if val, ok := from.GetParentPeer(); ok {
		t.ParentPeer = val
	}

	t.UserID = from.GetUserID()
}

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

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

// TypeInfo returns info about TL type.
func (t *AccountToggleNoPaidMessagesExceptionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.toggleNoPaidMessagesException",
		ID:   AccountToggleNoPaidMessagesExceptionRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "RefundCharged",
			SchemaName: "refund_charged",
			Null:       !t.Flags.Has(0),
		},
		{
			Name:       "RequirePayment",
			SchemaName: "require_payment",
			Null:       !t.Flags.Has(2),
		},
		{
			Name:       "ParentPeer",
			SchemaName: "parent_peer",
			Null:       !t.Flags.Has(1),
		},
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (t *AccountToggleNoPaidMessagesExceptionRequest) SetFlags() {
	if !(t.RefundCharged == false) {
		t.Flags.Set(0)
	}
	if !(t.RequirePayment == false) {
		t.Flags.Set(2)
	}
	if !(t.ParentPeer == nil) {
		t.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (t *AccountToggleNoPaidMessagesExceptionRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode account.toggleNoPaidMessagesException#fe2eda76 as nil")
	}
	b.PutID(AccountToggleNoPaidMessagesExceptionRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *AccountToggleNoPaidMessagesExceptionRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode account.toggleNoPaidMessagesException#fe2eda76 as nil")
	}
	t.SetFlags()
	if err := t.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.toggleNoPaidMessagesException#fe2eda76: field flags: %w", err)
	}
	if t.Flags.Has(1) {
		if t.ParentPeer == nil {
			return fmt.Errorf("unable to encode account.toggleNoPaidMessagesException#fe2eda76: field parent_peer is nil")
		}
		if err := t.ParentPeer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.toggleNoPaidMessagesException#fe2eda76: field parent_peer: %w", err)
		}
	}
	if t.UserID == nil {
		return fmt.Errorf("unable to encode account.toggleNoPaidMessagesException#fe2eda76: field user_id is nil")
	}
	if err := t.UserID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.toggleNoPaidMessagesException#fe2eda76: field user_id: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (t *AccountToggleNoPaidMessagesExceptionRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode account.toggleNoPaidMessagesException#fe2eda76 to nil")
	}
	{
		if err := t.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.toggleNoPaidMessagesException#fe2eda76: field flags: %w", err)
		}
	}
	t.RefundCharged = t.Flags.Has(0)
	t.RequirePayment = t.Flags.Has(2)
	if t.Flags.Has(1) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.toggleNoPaidMessagesException#fe2eda76: field parent_peer: %w", err)
		}
		t.ParentPeer = value
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.toggleNoPaidMessagesException#fe2eda76: field user_id: %w", err)
		}
		t.UserID = value
	}
	return nil
}

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

// GetRefundCharged returns value of RefundCharged conditional field.
func (t *AccountToggleNoPaidMessagesExceptionRequest) GetRefundCharged() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(0)
}

// SetRequirePayment sets value of RequirePayment conditional field.
func (t *AccountToggleNoPaidMessagesExceptionRequest) SetRequirePayment(value bool) {
	if value {
		t.Flags.Set(2)
		t.RequirePayment = true
	} else {
		t.Flags.Unset(2)
		t.RequirePayment = false
	}
}

// GetRequirePayment returns value of RequirePayment conditional field.
func (t *AccountToggleNoPaidMessagesExceptionRequest) GetRequirePayment() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(2)
}

// SetParentPeer sets value of ParentPeer conditional field.
func (t *AccountToggleNoPaidMessagesExceptionRequest) SetParentPeer(value InputPeerClass) {
	t.Flags.Set(1)
	t.ParentPeer = value
}

// GetParentPeer returns value of ParentPeer conditional field and
// boolean which is true if field was set.
func (t *AccountToggleNoPaidMessagesExceptionRequest) GetParentPeer() (value InputPeerClass, ok bool) {
	if t == nil {
		return
	}
	if !t.Flags.Has(1) {
		return value, false
	}
	return t.ParentPeer, true
}

// GetUserID returns value of UserID field.
func (t *AccountToggleNoPaidMessagesExceptionRequest) GetUserID() (value InputUserClass) {
	if t == nil {
		return
	}
	return t.UserID
}

// AccountToggleNoPaidMessagesException invokes method account.toggleNoPaidMessagesException#fe2eda76 returning error if any.
// Allow a user to send us messages without paying if paid messages »¹ are enabled.
//
// Links:
//  1. https://core.telegram.org/api/paid-messages
//
// Possible errors:
//
//	400 PARENT_PEER_INVALID: The specified parent_peer is invalid.
//	400 UNSUPPORTED: require_payment cannot be set by users, only by monoforums: users must instead use the inputPrivacyKeyNoPaidMessages privacy setting to remove a previously added exemption.
//	400 USER_ID_INVALID: The provided user ID is invalid.
//
// See https://core.telegram.org/method/account.toggleNoPaidMessagesException for reference.
func (c *Client) AccountToggleNoPaidMessagesException(ctx context.Context, request *AccountToggleNoPaidMessagesExceptionRequest) (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
}
