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

// StarTransaction represents TL type `starTransaction#7f820a90`.
type StarTransaction struct {
	// Unique identifier of the transaction
	ID string
	// The amount of added owned Telegram Stars; negative for outgoing transactions
	StarAmount StarAmount
	// True, if the transaction is a refund of a previous transaction
	IsRefund bool
	// Point in time (Unix timestamp) when the transaction was completed
	Date int32
	// Type of the transaction
	Type StarTransactionTypeClass
}

// StarTransactionTypeID is TL type id of StarTransaction.
const StarTransactionTypeID = 0x7f820a90

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

func (s *StarTransaction) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ID == "") {
		return false
	}
	if !(s.StarAmount.Zero()) {
		return false
	}
	if !(s.IsRefund == false) {
		return false
	}
	if !(s.Date == 0) {
		return false
	}
	if !(s.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StarTransaction) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starTransaction",
		ID:   StarTransactionTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "StarAmount",
			SchemaName: "star_amount",
		},
		{
			Name:       "IsRefund",
			SchemaName: "is_refund",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StarTransaction) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starTransaction#7f820a90 as nil")
	}
	b.PutID(StarTransactionTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarTransaction) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starTransaction#7f820a90 as nil")
	}
	b.PutString(s.ID)
	if err := s.StarAmount.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starTransaction#7f820a90: field star_amount: %w", err)
	}
	b.PutBool(s.IsRefund)
	b.PutInt32(s.Date)
	if s.Type == nil {
		return fmt.Errorf("unable to encode starTransaction#7f820a90: field type is nil")
	}
	if err := s.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starTransaction#7f820a90: field type: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StarTransaction) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starTransaction#7f820a90 to nil")
	}
	if err := b.ConsumeID(StarTransactionTypeID); err != nil {
		return fmt.Errorf("unable to decode starTransaction#7f820a90: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StarTransaction) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starTransaction#7f820a90 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starTransaction#7f820a90: field id: %w", err)
		}
		s.ID = value
	}
	{
		if err := s.StarAmount.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starTransaction#7f820a90: field star_amount: %w", err)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode starTransaction#7f820a90: field is_refund: %w", err)
		}
		s.IsRefund = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode starTransaction#7f820a90: field date: %w", err)
		}
		s.Date = value
	}
	{
		value, err := DecodeStarTransactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode starTransaction#7f820a90: field type: %w", err)
		}
		s.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StarTransaction) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode starTransaction#7f820a90 as nil")
	}
	b.ObjStart()
	b.PutID("starTransaction")
	b.Comma()
	b.FieldStart("id")
	b.PutString(s.ID)
	b.Comma()
	b.FieldStart("star_amount")
	if err := s.StarAmount.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode starTransaction#7f820a90: field star_amount: %w", err)
	}
	b.Comma()
	b.FieldStart("is_refund")
	b.PutBool(s.IsRefund)
	b.Comma()
	b.FieldStart("date")
	b.PutInt32(s.Date)
	b.Comma()
	b.FieldStart("type")
	if s.Type == nil {
		return fmt.Errorf("unable to encode starTransaction#7f820a90: field type is nil")
	}
	if err := s.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode starTransaction#7f820a90: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StarTransaction) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode starTransaction#7f820a90 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("starTransaction"); err != nil {
				return fmt.Errorf("unable to decode starTransaction#7f820a90: %w", err)
			}
		case "id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode starTransaction#7f820a90: field id: %w", err)
			}
			s.ID = value
		case "star_amount":
			if err := s.StarAmount.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode starTransaction#7f820a90: field star_amount: %w", err)
			}
		case "is_refund":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode starTransaction#7f820a90: field is_refund: %w", err)
			}
			s.IsRefund = value
		case "date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode starTransaction#7f820a90: field date: %w", err)
			}
			s.Date = value
		case "type":
			value, err := DecodeTDLibJSONStarTransactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode starTransaction#7f820a90: field type: %w", err)
			}
			s.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (s *StarTransaction) GetID() (value string) {
	if s == nil {
		return
	}
	return s.ID
}

// GetStarAmount returns value of StarAmount field.
func (s *StarTransaction) GetStarAmount() (value StarAmount) {
	if s == nil {
		return
	}
	return s.StarAmount
}

// GetIsRefund returns value of IsRefund field.
func (s *StarTransaction) GetIsRefund() (value bool) {
	if s == nil {
		return
	}
	return s.IsRefund
}

// GetDate returns value of Date field.
func (s *StarTransaction) GetDate() (value int32) {
	if s == nil {
		return
	}
	return s.Date
}

// GetType returns value of Type field.
func (s *StarTransaction) GetType() (value StarTransactionTypeClass) {
	if s == nil {
		return
	}
	return s.Type
}
