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

// PaymentsChangeStarsSubscriptionRequest represents TL type `payments.changeStarsSubscription#c7770878`.
// Activate or deactivate a Telegram Star subscription »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#paid-invite-links
//
// See https://core.telegram.org/method/payments.changeStarsSubscription for reference.
type PaymentsChangeStarsSubscriptionRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Always pass inputPeerSelf¹.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/inputPeerSelf
	Peer InputPeerClass
	// ID of the subscription.
	SubscriptionID string
	// Whether to cancel or reactivate the subscription.
	//
	// Use SetCanceled and GetCanceled helpers.
	Canceled bool
}

// PaymentsChangeStarsSubscriptionRequestTypeID is TL type id of PaymentsChangeStarsSubscriptionRequest.
const PaymentsChangeStarsSubscriptionRequestTypeID = 0xc7770878

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

func (c *PaymentsChangeStarsSubscriptionRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.Peer == nil) {
		return false
	}
	if !(c.SubscriptionID == "") {
		return false
	}
	if !(c.Canceled == false) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsChangeStarsSubscriptionRequest from given interface.
func (c *PaymentsChangeStarsSubscriptionRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetSubscriptionID() (value string)
	GetCanceled() (value bool, ok bool)
}) {
	c.Peer = from.GetPeer()
	c.SubscriptionID = from.GetSubscriptionID()
	if val, ok := from.GetCanceled(); ok {
		c.Canceled = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*PaymentsChangeStarsSubscriptionRequest) TypeName() string {
	return "payments.changeStarsSubscription"
}

// TypeInfo returns info about TL type.
func (c *PaymentsChangeStarsSubscriptionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.changeStarsSubscription",
		ID:   PaymentsChangeStarsSubscriptionRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "SubscriptionID",
			SchemaName: "subscription_id",
		},
		{
			Name:       "Canceled",
			SchemaName: "canceled",
			Null:       !c.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *PaymentsChangeStarsSubscriptionRequest) SetFlags() {
	if !(c.Canceled == false) {
		c.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (c *PaymentsChangeStarsSubscriptionRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.changeStarsSubscription#c7770878 as nil")
	}
	b.PutID(PaymentsChangeStarsSubscriptionRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *PaymentsChangeStarsSubscriptionRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.changeStarsSubscription#c7770878 as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.changeStarsSubscription#c7770878: field flags: %w", err)
	}
	if c.Peer == nil {
		return fmt.Errorf("unable to encode payments.changeStarsSubscription#c7770878: field peer is nil")
	}
	if err := c.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.changeStarsSubscription#c7770878: field peer: %w", err)
	}
	b.PutString(c.SubscriptionID)
	if c.Flags.Has(0) {
		b.PutBool(c.Canceled)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *PaymentsChangeStarsSubscriptionRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.changeStarsSubscription#c7770878 to nil")
	}
	if err := b.ConsumeID(PaymentsChangeStarsSubscriptionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.changeStarsSubscription#c7770878: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *PaymentsChangeStarsSubscriptionRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.changeStarsSubscription#c7770878 to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.changeStarsSubscription#c7770878: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.changeStarsSubscription#c7770878: field peer: %w", err)
		}
		c.Peer = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.changeStarsSubscription#c7770878: field subscription_id: %w", err)
		}
		c.SubscriptionID = value
	}
	if c.Flags.Has(0) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode payments.changeStarsSubscription#c7770878: field canceled: %w", err)
		}
		c.Canceled = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (c *PaymentsChangeStarsSubscriptionRequest) GetPeer() (value InputPeerClass) {
	if c == nil {
		return
	}
	return c.Peer
}

// GetSubscriptionID returns value of SubscriptionID field.
func (c *PaymentsChangeStarsSubscriptionRequest) GetSubscriptionID() (value string) {
	if c == nil {
		return
	}
	return c.SubscriptionID
}

// SetCanceled sets value of Canceled conditional field.
func (c *PaymentsChangeStarsSubscriptionRequest) SetCanceled(value bool) {
	c.Flags.Set(0)
	c.Canceled = value
}

// GetCanceled returns value of Canceled conditional field and
// boolean which is true if field was set.
func (c *PaymentsChangeStarsSubscriptionRequest) GetCanceled() (value bool, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(0) {
		return value, false
	}
	return c.Canceled, true
}

// PaymentsChangeStarsSubscription invokes method payments.changeStarsSubscription#c7770878 returning error if any.
// Activate or deactivate a Telegram Star subscription »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#paid-invite-links
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.changeStarsSubscription for reference.
func (c *Client) PaymentsChangeStarsSubscription(ctx context.Context, request *PaymentsChangeStarsSubscriptionRequest) (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
}
