// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// ChatRevenueTransaction represents TL type `chatRevenueTransaction#4c7a4ff`.
type ChatRevenueTransaction struct {
	// Cryptocurrency in which revenue is calculated
	Cryptocurrency string
	// The withdrawn amount, in the smallest units of the cryptocurrency
	CryptocurrencyAmount int64
	// Type of the transaction
	Type ChatRevenueTransactionTypeClass
}

// ChatRevenueTransactionTypeID is TL type id of ChatRevenueTransaction.
const ChatRevenueTransactionTypeID = 0x4c7a4ff

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

func (c *ChatRevenueTransaction) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Cryptocurrency == "") {
		return false
	}
	if !(c.CryptocurrencyAmount == 0) {
		return false
	}
	if !(c.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatRevenueTransaction) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatRevenueTransaction",
		ID:   ChatRevenueTransactionTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Cryptocurrency",
			SchemaName: "cryptocurrency",
		},
		{
			Name:       "CryptocurrencyAmount",
			SchemaName: "cryptocurrency_amount",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatRevenueTransaction) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatRevenueTransaction#4c7a4ff as nil")
	}
	b.PutID(ChatRevenueTransactionTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatRevenueTransaction) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatRevenueTransaction#4c7a4ff as nil")
	}
	b.PutString(c.Cryptocurrency)
	b.PutLong(c.CryptocurrencyAmount)
	if c.Type == nil {
		return fmt.Errorf("unable to encode chatRevenueTransaction#4c7a4ff: field type is nil")
	}
	if err := c.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatRevenueTransaction#4c7a4ff: field type: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatRevenueTransaction) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatRevenueTransaction#4c7a4ff to nil")
	}
	if err := b.ConsumeID(ChatRevenueTransactionTypeID); err != nil {
		return fmt.Errorf("unable to decode chatRevenueTransaction#4c7a4ff: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatRevenueTransaction) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatRevenueTransaction#4c7a4ff to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode chatRevenueTransaction#4c7a4ff: field cryptocurrency: %w", err)
		}
		c.Cryptocurrency = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode chatRevenueTransaction#4c7a4ff: field cryptocurrency_amount: %w", err)
		}
		c.CryptocurrencyAmount = value
	}
	{
		value, err := DecodeChatRevenueTransactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatRevenueTransaction#4c7a4ff: field type: %w", err)
		}
		c.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatRevenueTransaction) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatRevenueTransaction#4c7a4ff as nil")
	}
	b.ObjStart()
	b.PutID("chatRevenueTransaction")
	b.Comma()
	b.FieldStart("cryptocurrency")
	b.PutString(c.Cryptocurrency)
	b.Comma()
	b.FieldStart("cryptocurrency_amount")
	b.PutLong(c.CryptocurrencyAmount)
	b.Comma()
	b.FieldStart("type")
	if c.Type == nil {
		return fmt.Errorf("unable to encode chatRevenueTransaction#4c7a4ff: field type is nil")
	}
	if err := c.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatRevenueTransaction#4c7a4ff: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatRevenueTransaction) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatRevenueTransaction#4c7a4ff to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatRevenueTransaction"); err != nil {
				return fmt.Errorf("unable to decode chatRevenueTransaction#4c7a4ff: %w", err)
			}
		case "cryptocurrency":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode chatRevenueTransaction#4c7a4ff: field cryptocurrency: %w", err)
			}
			c.Cryptocurrency = value
		case "cryptocurrency_amount":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode chatRevenueTransaction#4c7a4ff: field cryptocurrency_amount: %w", err)
			}
			c.CryptocurrencyAmount = value
		case "type":
			value, err := DecodeTDLibJSONChatRevenueTransactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatRevenueTransaction#4c7a4ff: field type: %w", err)
			}
			c.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCryptocurrency returns value of Cryptocurrency field.
func (c *ChatRevenueTransaction) GetCryptocurrency() (value string) {
	if c == nil {
		return
	}
	return c.Cryptocurrency
}

// GetCryptocurrencyAmount returns value of CryptocurrencyAmount field.
func (c *ChatRevenueTransaction) GetCryptocurrencyAmount() (value int64) {
	if c == nil {
		return
	}
	return c.CryptocurrencyAmount
}

// GetType returns value of Type field.
func (c *ChatRevenueTransaction) GetType() (value ChatRevenueTransactionTypeClass) {
	if c == nil {
		return
	}
	return c.Type
}
