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

// PaymentsGetPremiumGiftCodeOptionsRequest represents TL type `payments.getPremiumGiftCodeOptions#2757ba54`.
// Obtain a list of Telegram Premium giveaway/gift code »¹ options.
//
// Links:
//  1. https://core.telegram.org/api/giveaways
//
// See https://core.telegram.org/method/payments.getPremiumGiftCodeOptions for reference.
type PaymentsGetPremiumGiftCodeOptionsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The channel that will start the giveaway
	//
	// Use SetBoostPeer and GetBoostPeer helpers.
	BoostPeer InputPeerClass
}

// PaymentsGetPremiumGiftCodeOptionsRequestTypeID is TL type id of PaymentsGetPremiumGiftCodeOptionsRequest.
const PaymentsGetPremiumGiftCodeOptionsRequestTypeID = 0x2757ba54

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

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

	return true
}

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

// FillFrom fills PaymentsGetPremiumGiftCodeOptionsRequest from given interface.
func (g *PaymentsGetPremiumGiftCodeOptionsRequest) FillFrom(from interface {
	GetBoostPeer() (value InputPeerClass, ok bool)
}) {
	if val, ok := from.GetBoostPeer(); ok {
		g.BoostPeer = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (g *PaymentsGetPremiumGiftCodeOptionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.getPremiumGiftCodeOptions",
		ID:   PaymentsGetPremiumGiftCodeOptionsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BoostPeer",
			SchemaName: "boost_peer",
			Null:       !g.Flags.Has(0),
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (g *PaymentsGetPremiumGiftCodeOptionsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getPremiumGiftCodeOptions#2757ba54 as nil")
	}
	b.PutID(PaymentsGetPremiumGiftCodeOptionsRequestTypeID)
	return g.EncodeBare(b)
}

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

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

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetPremiumGiftCodeOptionsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getPremiumGiftCodeOptions#2757ba54 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.getPremiumGiftCodeOptions#2757ba54: field flags: %w", err)
		}
	}
	if g.Flags.Has(0) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getPremiumGiftCodeOptions#2757ba54: field boost_peer: %w", err)
		}
		g.BoostPeer = value
	}
	return nil
}

// SetBoostPeer sets value of BoostPeer conditional field.
func (g *PaymentsGetPremiumGiftCodeOptionsRequest) SetBoostPeer(value InputPeerClass) {
	g.Flags.Set(0)
	g.BoostPeer = value
}

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

// PaymentsGetPremiumGiftCodeOptions invokes method payments.getPremiumGiftCodeOptions#2757ba54 returning error if any.
// Obtain a list of Telegram Premium giveaway/gift code »¹ options.
//
// Links:
//  1. https://core.telegram.org/api/giveaways
//
// See https://core.telegram.org/method/payments.getPremiumGiftCodeOptions for reference.
func (c *Client) PaymentsGetPremiumGiftCodeOptions(ctx context.Context, request *PaymentsGetPremiumGiftCodeOptionsRequest) ([]PremiumGiftCodeOption, error) {
	var result PremiumGiftCodeOptionVector

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