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

// PaymentsSendStarGiftOfferRequest represents TL type `payments.sendStarGiftOffer#8fb86b41`.
//
// See https://core.telegram.org/method/payments.sendStarGiftOffer for reference.
type PaymentsSendStarGiftOfferRequest struct {
	// Flags field of PaymentsSendStarGiftOfferRequest.
	Flags bin.Fields
	// Peer field of PaymentsSendStarGiftOfferRequest.
	Peer InputPeerClass
	// Slug field of PaymentsSendStarGiftOfferRequest.
	Slug string
	// Price field of PaymentsSendStarGiftOfferRequest.
	Price StarsAmountClass
	// Duration field of PaymentsSendStarGiftOfferRequest.
	Duration int
	// RandomID field of PaymentsSendStarGiftOfferRequest.
	RandomID int64
	// AllowPaidStars field of PaymentsSendStarGiftOfferRequest.
	//
	// Use SetAllowPaidStars and GetAllowPaidStars helpers.
	AllowPaidStars int64
}

// PaymentsSendStarGiftOfferRequestTypeID is TL type id of PaymentsSendStarGiftOfferRequest.
const PaymentsSendStarGiftOfferRequestTypeID = 0x8fb86b41

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

func (s *PaymentsSendStarGiftOfferRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.Slug == "") {
		return false
	}
	if !(s.Price == nil) {
		return false
	}
	if !(s.Duration == 0) {
		return false
	}
	if !(s.RandomID == 0) {
		return false
	}
	if !(s.AllowPaidStars == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsSendStarGiftOfferRequest from given interface.
func (s *PaymentsSendStarGiftOfferRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetSlug() (value string)
	GetPrice() (value StarsAmountClass)
	GetDuration() (value int)
	GetRandomID() (value int64)
	GetAllowPaidStars() (value int64, ok bool)
}) {
	s.Peer = from.GetPeer()
	s.Slug = from.GetSlug()
	s.Price = from.GetPrice()
	s.Duration = from.GetDuration()
	s.RandomID = from.GetRandomID()
	if val, ok := from.GetAllowPaidStars(); ok {
		s.AllowPaidStars = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *PaymentsSendStarGiftOfferRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.sendStarGiftOffer",
		ID:   PaymentsSendStarGiftOfferRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Slug",
			SchemaName: "slug",
		},
		{
			Name:       "Price",
			SchemaName: "price",
		},
		{
			Name:       "Duration",
			SchemaName: "duration",
		},
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
		{
			Name:       "AllowPaidStars",
			SchemaName: "allow_paid_stars",
			Null:       !s.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *PaymentsSendStarGiftOfferRequest) SetFlags() {
	if !(s.AllowPaidStars == 0) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *PaymentsSendStarGiftOfferRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.sendStarGiftOffer#8fb86b41 as nil")
	}
	b.PutID(PaymentsSendStarGiftOfferRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsSendStarGiftOfferRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.sendStarGiftOffer#8fb86b41 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.sendStarGiftOffer#8fb86b41: field flags: %w", err)
	}
	if s.Peer == nil {
		return fmt.Errorf("unable to encode payments.sendStarGiftOffer#8fb86b41: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.sendStarGiftOffer#8fb86b41: field peer: %w", err)
	}
	b.PutString(s.Slug)
	if s.Price == nil {
		return fmt.Errorf("unable to encode payments.sendStarGiftOffer#8fb86b41: field price is nil")
	}
	if err := s.Price.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.sendStarGiftOffer#8fb86b41: field price: %w", err)
	}
	b.PutInt(s.Duration)
	b.PutLong(s.RandomID)
	if s.Flags.Has(0) {
		b.PutLong(s.AllowPaidStars)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *PaymentsSendStarGiftOfferRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.sendStarGiftOffer#8fb86b41 to nil")
	}
	if err := b.ConsumeID(PaymentsSendStarGiftOfferRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.sendStarGiftOffer#8fb86b41: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsSendStarGiftOfferRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.sendStarGiftOffer#8fb86b41 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.sendStarGiftOffer#8fb86b41: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.sendStarGiftOffer#8fb86b41: field peer: %w", err)
		}
		s.Peer = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.sendStarGiftOffer#8fb86b41: field slug: %w", err)
		}
		s.Slug = value
	}
	{
		value, err := DecodeStarsAmount(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.sendStarGiftOffer#8fb86b41: field price: %w", err)
		}
		s.Price = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.sendStarGiftOffer#8fb86b41: field duration: %w", err)
		}
		s.Duration = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode payments.sendStarGiftOffer#8fb86b41: field random_id: %w", err)
		}
		s.RandomID = value
	}
	if s.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode payments.sendStarGiftOffer#8fb86b41: field allow_paid_stars: %w", err)
		}
		s.AllowPaidStars = value
	}
	return nil
}

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

// GetSlug returns value of Slug field.
func (s *PaymentsSendStarGiftOfferRequest) GetSlug() (value string) {
	if s == nil {
		return
	}
	return s.Slug
}

// GetPrice returns value of Price field.
func (s *PaymentsSendStarGiftOfferRequest) GetPrice() (value StarsAmountClass) {
	if s == nil {
		return
	}
	return s.Price
}

// GetDuration returns value of Duration field.
func (s *PaymentsSendStarGiftOfferRequest) GetDuration() (value int) {
	if s == nil {
		return
	}
	return s.Duration
}

// GetRandomID returns value of RandomID field.
func (s *PaymentsSendStarGiftOfferRequest) GetRandomID() (value int64) {
	if s == nil {
		return
	}
	return s.RandomID
}

// SetAllowPaidStars sets value of AllowPaidStars conditional field.
func (s *PaymentsSendStarGiftOfferRequest) SetAllowPaidStars(value int64) {
	s.Flags.Set(0)
	s.AllowPaidStars = value
}

// GetAllowPaidStars returns value of AllowPaidStars conditional field and
// boolean which is true if field was set.
func (s *PaymentsSendStarGiftOfferRequest) GetAllowPaidStars() (value int64, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.AllowPaidStars, true
}

// PaymentsSendStarGiftOffer invokes method payments.sendStarGiftOffer#8fb86b41 returning error if any.
//
// See https://core.telegram.org/method/payments.sendStarGiftOffer for reference.
func (c *Client) PaymentsSendStarGiftOffer(ctx context.Context, request *PaymentsSendStarGiftOfferRequest) (UpdatesClass, error) {
	var result UpdatesBox

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