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

// PaymentsGetSuggestedStarRefBotsRequest represents TL type `payments.getSuggestedStarRefBots#d6b48f7`.
// Obtain a list of suggested mini apps¹ with available affiliate programs²
// order_by_revenue and order_by_date are mutually exclusive: if neither is set, results
// are sorted by profitability.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//  2. https://core.telegram.org/api/bots/referrals
//
// See https://core.telegram.org/method/payments.getSuggestedStarRefBots for reference.
type PaymentsGetSuggestedStarRefBotsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, orders results by the expected revenue
	OrderByRevenue bool
	// If set, orders results by the creation date of the affiliate program
	OrderByDate bool
	// The peer that will become the affiliate: star commissions will be transferred to this
	// peer's star balance.
	Peer InputPeerClass
	// Offset for pagination, taken from payments.suggestedStarRefBots¹.next_offset,
	// initially empty.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/payments.suggestedStarRefBots
	Offset string
	// Maximum number of results to return, see pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Limit int
}

// PaymentsGetSuggestedStarRefBotsRequestTypeID is TL type id of PaymentsGetSuggestedStarRefBotsRequest.
const PaymentsGetSuggestedStarRefBotsRequestTypeID = 0xd6b48f7

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

func (g *PaymentsGetSuggestedStarRefBotsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.OrderByRevenue == false) {
		return false
	}
	if !(g.OrderByDate == false) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Offset == "") {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsGetSuggestedStarRefBotsRequest from given interface.
func (g *PaymentsGetSuggestedStarRefBotsRequest) FillFrom(from interface {
	GetOrderByRevenue() (value bool)
	GetOrderByDate() (value bool)
	GetPeer() (value InputPeerClass)
	GetOffset() (value string)
	GetLimit() (value int)
}) {
	g.OrderByRevenue = from.GetOrderByRevenue()
	g.OrderByDate = from.GetOrderByDate()
	g.Peer = from.GetPeer()
	g.Offset = from.GetOffset()
	g.Limit = from.GetLimit()
}

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

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

// TypeInfo returns info about TL type.
func (g *PaymentsGetSuggestedStarRefBotsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.getSuggestedStarRefBots",
		ID:   PaymentsGetSuggestedStarRefBotsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OrderByRevenue",
			SchemaName: "order_by_revenue",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "OrderByDate",
			SchemaName: "order_by_date",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *PaymentsGetSuggestedStarRefBotsRequest) SetFlags() {
	if !(g.OrderByRevenue == false) {
		g.Flags.Set(0)
	}
	if !(g.OrderByDate == false) {
		g.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (g *PaymentsGetSuggestedStarRefBotsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getSuggestedStarRefBots#d6b48f7 as nil")
	}
	b.PutID(PaymentsGetSuggestedStarRefBotsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *PaymentsGetSuggestedStarRefBotsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getSuggestedStarRefBots#d6b48f7 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getSuggestedStarRefBots#d6b48f7: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode payments.getSuggestedStarRefBots#d6b48f7: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getSuggestedStarRefBots#d6b48f7: field peer: %w", err)
	}
	b.PutString(g.Offset)
	b.PutInt(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *PaymentsGetSuggestedStarRefBotsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getSuggestedStarRefBots#d6b48f7 to nil")
	}
	if err := b.ConsumeID(PaymentsGetSuggestedStarRefBotsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.getSuggestedStarRefBots#d6b48f7: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetSuggestedStarRefBotsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getSuggestedStarRefBots#d6b48f7 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.getSuggestedStarRefBots#d6b48f7: field flags: %w", err)
		}
	}
	g.OrderByRevenue = g.Flags.Has(0)
	g.OrderByDate = g.Flags.Has(1)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getSuggestedStarRefBots#d6b48f7: field peer: %w", err)
		}
		g.Peer = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.getSuggestedStarRefBots#d6b48f7: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.getSuggestedStarRefBots#d6b48f7: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// SetOrderByRevenue sets value of OrderByRevenue conditional field.
func (g *PaymentsGetSuggestedStarRefBotsRequest) SetOrderByRevenue(value bool) {
	if value {
		g.Flags.Set(0)
		g.OrderByRevenue = true
	} else {
		g.Flags.Unset(0)
		g.OrderByRevenue = false
	}
}

// GetOrderByRevenue returns value of OrderByRevenue conditional field.
func (g *PaymentsGetSuggestedStarRefBotsRequest) GetOrderByRevenue() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

// SetOrderByDate sets value of OrderByDate conditional field.
func (g *PaymentsGetSuggestedStarRefBotsRequest) SetOrderByDate(value bool) {
	if value {
		g.Flags.Set(1)
		g.OrderByDate = true
	} else {
		g.Flags.Unset(1)
		g.OrderByDate = false
	}
}

// GetOrderByDate returns value of OrderByDate conditional field.
func (g *PaymentsGetSuggestedStarRefBotsRequest) GetOrderByDate() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(1)
}

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

// GetOffset returns value of Offset field.
func (g *PaymentsGetSuggestedStarRefBotsRequest) GetOffset() (value string) {
	if g == nil {
		return
	}
	return g.Offset
}

// GetLimit returns value of Limit field.
func (g *PaymentsGetSuggestedStarRefBotsRequest) GetLimit() (value int) {
	if g == nil {
		return
	}
	return g.Limit
}

// PaymentsGetSuggestedStarRefBots invokes method payments.getSuggestedStarRefBots#d6b48f7 returning error if any.
// Obtain a list of suggested mini apps¹ with available affiliate programs²
// order_by_revenue and order_by_date are mutually exclusive: if neither is set, results
// are sorted by profitability.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//  2. https://core.telegram.org/api/bots/referrals
//
// Possible errors:
//
//	403 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.getSuggestedStarRefBots for reference.
func (c *Client) PaymentsGetSuggestedStarRefBots(ctx context.Context, request *PaymentsGetSuggestedStarRefBotsRequest) (*PaymentsSuggestedStarRefBots, error) {
	var result PaymentsSuggestedStarRefBots

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
