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

// AccountPaidMessagesRevenue represents TL type `account.paidMessagesRevenue#1e109708`.
// Total number of non-refunded Telegram Stars¹ a user has spent on sending us messages
// either directly or through a channel, see here »² for more info on paid messages.
//
// Links:
//  1. https://core.telegram.org/api/stars
//  2. https://core.telegram.org/api/paid-messages
//
// See https://core.telegram.org/constructor/account.paidMessagesRevenue for reference.
type AccountPaidMessagesRevenue struct {
	// Amount in Stars¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/stars
	StarsAmount int64
}

// AccountPaidMessagesRevenueTypeID is TL type id of AccountPaidMessagesRevenue.
const AccountPaidMessagesRevenueTypeID = 0x1e109708

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

func (p *AccountPaidMessagesRevenue) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.StarsAmount == 0) {
		return false
	}

	return true
}

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

// FillFrom fills AccountPaidMessagesRevenue from given interface.
func (p *AccountPaidMessagesRevenue) FillFrom(from interface {
	GetStarsAmount() (value int64)
}) {
	p.StarsAmount = from.GetStarsAmount()
}

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

// TypeName returns name of type in TL schema.
func (*AccountPaidMessagesRevenue) TypeName() string {
	return "account.paidMessagesRevenue"
}

// TypeInfo returns info about TL type.
func (p *AccountPaidMessagesRevenue) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.paidMessagesRevenue",
		ID:   AccountPaidMessagesRevenueTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StarsAmount",
			SchemaName: "stars_amount",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *AccountPaidMessagesRevenue) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode account.paidMessagesRevenue#1e109708 as nil")
	}
	b.PutID(AccountPaidMessagesRevenueTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *AccountPaidMessagesRevenue) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode account.paidMessagesRevenue#1e109708 as nil")
	}
	b.PutLong(p.StarsAmount)
	return nil
}

// Decode implements bin.Decoder.
func (p *AccountPaidMessagesRevenue) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode account.paidMessagesRevenue#1e109708 to nil")
	}
	if err := b.ConsumeID(AccountPaidMessagesRevenueTypeID); err != nil {
		return fmt.Errorf("unable to decode account.paidMessagesRevenue#1e109708: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *AccountPaidMessagesRevenue) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode account.paidMessagesRevenue#1e109708 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode account.paidMessagesRevenue#1e109708: field stars_amount: %w", err)
		}
		p.StarsAmount = value
	}
	return nil
}

// GetStarsAmount returns value of StarsAmount field.
func (p *AccountPaidMessagesRevenue) GetStarsAmount() (value int64) {
	if p == nil {
		return
	}
	return p.StarsAmount
}
