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

// StarsSubscriptionPricing represents TL type `starsSubscriptionPricing#5416d58`.
// Pricing of a Telegram Star subscription »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#paid-invite-links
//
// See https://core.telegram.org/constructor/starsSubscriptionPricing for reference.
type StarsSubscriptionPricing struct {
	// The user should pay amount stars every period seconds to gain and maintain access to
	// the channel. Currently the only allowed subscription period is 30*24*60*60, i.e. the
	// user will be debited amount stars every month.
	Period int
	// Price of the subscription in Telegram Stars.
	Amount int64
}

// StarsSubscriptionPricingTypeID is TL type id of StarsSubscriptionPricing.
const StarsSubscriptionPricingTypeID = 0x5416d58

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

func (s *StarsSubscriptionPricing) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Period == 0) {
		return false
	}
	if !(s.Amount == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StarsSubscriptionPricing from given interface.
func (s *StarsSubscriptionPricing) FillFrom(from interface {
	GetPeriod() (value int)
	GetAmount() (value int64)
}) {
	s.Period = from.GetPeriod()
	s.Amount = from.GetAmount()
}

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

// TypeName returns name of type in TL schema.
func (*StarsSubscriptionPricing) TypeName() string {
	return "starsSubscriptionPricing"
}

// TypeInfo returns info about TL type.
func (s *StarsSubscriptionPricing) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starsSubscriptionPricing",
		ID:   StarsSubscriptionPricingTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Period",
			SchemaName: "period",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StarsSubscriptionPricing) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsSubscriptionPricing#5416d58 as nil")
	}
	b.PutID(StarsSubscriptionPricingTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarsSubscriptionPricing) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsSubscriptionPricing#5416d58 as nil")
	}
	b.PutInt(s.Period)
	b.PutLong(s.Amount)
	return nil
}

// Decode implements bin.Decoder.
func (s *StarsSubscriptionPricing) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starsSubscriptionPricing#5416d58 to nil")
	}
	if err := b.ConsumeID(StarsSubscriptionPricingTypeID); err != nil {
		return fmt.Errorf("unable to decode starsSubscriptionPricing#5416d58: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StarsSubscriptionPricing) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starsSubscriptionPricing#5416d58 to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starsSubscriptionPricing#5416d58: field period: %w", err)
		}
		s.Period = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starsSubscriptionPricing#5416d58: field amount: %w", err)
		}
		s.Amount = value
	}
	return nil
}

// GetPeriod returns value of Period field.
func (s *StarsSubscriptionPricing) GetPeriod() (value int) {
	if s == nil {
		return
	}
	return s.Period
}

// GetAmount returns value of Amount field.
func (s *StarsSubscriptionPricing) GetAmount() (value int64) {
	if s == nil {
		return
	}
	return s.Amount
}
