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

// PaymentsGetStarsGiftOptionsRequest represents TL type `payments.getStarsGiftOptions#d3c96bc8`.
// Obtain a list of Telegram Stars gift options »¹ as starsGiftOption² constructors.
//
// Links:
//  1. https://core.telegram.org/api/stars#buying-or-gifting-stars
//  2. https://core.telegram.org/constructor/starsGiftOption
//
// See https://core.telegram.org/method/payments.getStarsGiftOptions for reference.
type PaymentsGetStarsGiftOptionsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Receiver of the gift (optional).
	//
	// Use SetUserID and GetUserID helpers.
	UserID InputUserClass
}

// PaymentsGetStarsGiftOptionsRequestTypeID is TL type id of PaymentsGetStarsGiftOptionsRequest.
const PaymentsGetStarsGiftOptionsRequestTypeID = 0xd3c96bc8

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

func (g *PaymentsGetStarsGiftOptionsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.UserID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsGetStarsGiftOptionsRequest from given interface.
func (g *PaymentsGetStarsGiftOptionsRequest) FillFrom(from interface {
	GetUserID() (value InputUserClass, ok bool)
}) {
	if val, ok := from.GetUserID(); ok {
		g.UserID = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (g *PaymentsGetStarsGiftOptionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.getStarsGiftOptions",
		ID:   PaymentsGetStarsGiftOptionsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
			Null:       !g.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *PaymentsGetStarsGiftOptionsRequest) SetFlags() {
	if !(g.UserID == nil) {
		g.Flags.Set(0)
	}
}

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

// EncodeBare implements bin.BareEncoder.
func (g *PaymentsGetStarsGiftOptionsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarsGiftOptions#d3c96bc8 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarsGiftOptions#d3c96bc8: field flags: %w", err)
	}
	if g.Flags.Has(0) {
		if g.UserID == nil {
			return fmt.Errorf("unable to encode payments.getStarsGiftOptions#d3c96bc8: field user_id is nil")
		}
		if err := g.UserID.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.getStarsGiftOptions#d3c96bc8: field user_id: %w", err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetStarsGiftOptionsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getStarsGiftOptions#d3c96bc8 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.getStarsGiftOptions#d3c96bc8: field flags: %w", err)
		}
	}
	if g.Flags.Has(0) {
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarsGiftOptions#d3c96bc8: field user_id: %w", err)
		}
		g.UserID = value
	}
	return nil
}

// SetUserID sets value of UserID conditional field.
func (g *PaymentsGetStarsGiftOptionsRequest) SetUserID(value InputUserClass) {
	g.Flags.Set(0)
	g.UserID = value
}

// GetUserID returns value of UserID conditional field and
// boolean which is true if field was set.
func (g *PaymentsGetStarsGiftOptionsRequest) GetUserID() (value InputUserClass, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(0) {
		return value, false
	}
	return g.UserID, true
}

// PaymentsGetStarsGiftOptions invokes method payments.getStarsGiftOptions#d3c96bc8 returning error if any.
// Obtain a list of Telegram Stars gift options »¹ as starsGiftOption² constructors.
//
// Links:
//  1. https://core.telegram.org/api/stars#buying-or-gifting-stars
//  2. https://core.telegram.org/constructor/starsGiftOption
//
// Possible errors:
//
//	400 INPUT_USER_DEACTIVATED: The specified user was deleted.
//	400 USER_GIFT_UNAVAILABLE: Gifts are not available in the current region (stars_gifts_enabled is equal to false).
//	400 USER_ID_INVALID: The provided user ID is invalid.
//
// See https://core.telegram.org/method/payments.getStarsGiftOptions for reference.
func (c *Client) PaymentsGetStarsGiftOptions(ctx context.Context, request *PaymentsGetStarsGiftOptionsRequest) ([]StarsGiftOption, error) {
	var result StarsGiftOptionVector

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