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

// StoreTransactionAppStore represents TL type `storeTransactionAppStore#60e41d49`.
type StoreTransactionAppStore struct {
	// App Store receipt
	Receipt []byte
}

// StoreTransactionAppStoreTypeID is TL type id of StoreTransactionAppStore.
const StoreTransactionAppStoreTypeID = 0x60e41d49

// construct implements constructor of StoreTransactionClass.
func (s StoreTransactionAppStore) construct() StoreTransactionClass { return &s }

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

	_ StoreTransactionClass = &StoreTransactionAppStore{}
)

func (s *StoreTransactionAppStore) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Receipt == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoreTransactionAppStore) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storeTransactionAppStore",
		ID:   StoreTransactionAppStoreTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Receipt",
			SchemaName: "receipt",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoreTransactionAppStore) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storeTransactionAppStore#60e41d49 as nil")
	}
	b.PutID(StoreTransactionAppStoreTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoreTransactionAppStore) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storeTransactionAppStore#60e41d49 as nil")
	}
	b.PutBytes(s.Receipt)
	return nil
}

// Decode implements bin.Decoder.
func (s *StoreTransactionAppStore) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storeTransactionAppStore#60e41d49 to nil")
	}
	if err := b.ConsumeID(StoreTransactionAppStoreTypeID); err != nil {
		return fmt.Errorf("unable to decode storeTransactionAppStore#60e41d49: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StoreTransactionAppStore) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storeTransactionAppStore#60e41d49 to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode storeTransactionAppStore#60e41d49: field receipt: %w", err)
		}
		s.Receipt = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoreTransactionAppStore) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storeTransactionAppStore#60e41d49 as nil")
	}
	b.ObjStart()
	b.PutID("storeTransactionAppStore")
	b.Comma()
	b.FieldStart("receipt")
	b.PutBytes(s.Receipt)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StoreTransactionAppStore) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode storeTransactionAppStore#60e41d49 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storeTransactionAppStore"); err != nil {
				return fmt.Errorf("unable to decode storeTransactionAppStore#60e41d49: %w", err)
			}
		case "receipt":
			value, err := b.Bytes()
			if err != nil {
				return fmt.Errorf("unable to decode storeTransactionAppStore#60e41d49: field receipt: %w", err)
			}
			s.Receipt = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetReceipt returns value of Receipt field.
func (s *StoreTransactionAppStore) GetReceipt() (value []byte) {
	if s == nil {
		return
	}
	return s.Receipt
}

// StoreTransactionGooglePlay represents TL type `storeTransactionGooglePlay#41356639`.
type StoreTransactionGooglePlay struct {
	// Application package name
	PackageName string
	// Identifier of the purchased store product
	StoreProductID string
	// Google Play purchase token
	PurchaseToken string
}

// StoreTransactionGooglePlayTypeID is TL type id of StoreTransactionGooglePlay.
const StoreTransactionGooglePlayTypeID = 0x41356639

// construct implements constructor of StoreTransactionClass.
func (s StoreTransactionGooglePlay) construct() StoreTransactionClass { return &s }

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

	_ StoreTransactionClass = &StoreTransactionGooglePlay{}
)

func (s *StoreTransactionGooglePlay) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.PackageName == "") {
		return false
	}
	if !(s.StoreProductID == "") {
		return false
	}
	if !(s.PurchaseToken == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoreTransactionGooglePlay) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storeTransactionGooglePlay",
		ID:   StoreTransactionGooglePlayTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PackageName",
			SchemaName: "package_name",
		},
		{
			Name:       "StoreProductID",
			SchemaName: "store_product_id",
		},
		{
			Name:       "PurchaseToken",
			SchemaName: "purchase_token",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoreTransactionGooglePlay) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storeTransactionGooglePlay#41356639 as nil")
	}
	b.PutID(StoreTransactionGooglePlayTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoreTransactionGooglePlay) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storeTransactionGooglePlay#41356639 as nil")
	}
	b.PutString(s.PackageName)
	b.PutString(s.StoreProductID)
	b.PutString(s.PurchaseToken)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StoreTransactionGooglePlay) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storeTransactionGooglePlay#41356639 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode storeTransactionGooglePlay#41356639: field package_name: %w", err)
		}
		s.PackageName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode storeTransactionGooglePlay#41356639: field store_product_id: %w", err)
		}
		s.StoreProductID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode storeTransactionGooglePlay#41356639: field purchase_token: %w", err)
		}
		s.PurchaseToken = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoreTransactionGooglePlay) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storeTransactionGooglePlay#41356639 as nil")
	}
	b.ObjStart()
	b.PutID("storeTransactionGooglePlay")
	b.Comma()
	b.FieldStart("package_name")
	b.PutString(s.PackageName)
	b.Comma()
	b.FieldStart("store_product_id")
	b.PutString(s.StoreProductID)
	b.Comma()
	b.FieldStart("purchase_token")
	b.PutString(s.PurchaseToken)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("storeTransactionGooglePlay"); err != nil {
				return fmt.Errorf("unable to decode storeTransactionGooglePlay#41356639: %w", err)
			}
		case "package_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode storeTransactionGooglePlay#41356639: field package_name: %w", err)
			}
			s.PackageName = value
		case "store_product_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode storeTransactionGooglePlay#41356639: field store_product_id: %w", err)
			}
			s.StoreProductID = value
		case "purchase_token":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode storeTransactionGooglePlay#41356639: field purchase_token: %w", err)
			}
			s.PurchaseToken = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPackageName returns value of PackageName field.
func (s *StoreTransactionGooglePlay) GetPackageName() (value string) {
	if s == nil {
		return
	}
	return s.PackageName
}

// GetStoreProductID returns value of StoreProductID field.
func (s *StoreTransactionGooglePlay) GetStoreProductID() (value string) {
	if s == nil {
		return
	}
	return s.StoreProductID
}

// GetPurchaseToken returns value of PurchaseToken field.
func (s *StoreTransactionGooglePlay) GetPurchaseToken() (value string) {
	if s == nil {
		return
	}
	return s.PurchaseToken
}

// StoreTransactionClassName is schema name of StoreTransactionClass.
const StoreTransactionClassName = "StoreTransaction"

// StoreTransactionClass represents StoreTransaction generic type.
//
// Example:
//
//	g, err := tdapi.DecodeStoreTransaction(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.StoreTransactionAppStore: // storeTransactionAppStore#60e41d49
//	case *tdapi.StoreTransactionGooglePlay: // storeTransactionGooglePlay#41356639
//	default: panic(v)
//	}
type StoreTransactionClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() StoreTransactionClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeStoreTransaction implements binary de-serialization for StoreTransactionClass.
func DecodeStoreTransaction(buf *bin.Buffer) (StoreTransactionClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case StoreTransactionAppStoreTypeID:
		// Decoding storeTransactionAppStore#60e41d49.
		v := StoreTransactionAppStore{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoreTransactionClass: %w", err)
		}
		return &v, nil
	case StoreTransactionGooglePlayTypeID:
		// Decoding storeTransactionGooglePlay#41356639.
		v := StoreTransactionGooglePlay{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoreTransactionClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode StoreTransactionClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONStoreTransaction implements binary de-serialization for StoreTransactionClass.
func DecodeTDLibJSONStoreTransaction(buf tdjson.Decoder) (StoreTransactionClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "storeTransactionAppStore":
		// Decoding storeTransactionAppStore#60e41d49.
		v := StoreTransactionAppStore{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoreTransactionClass: %w", err)
		}
		return &v, nil
	case "storeTransactionGooglePlay":
		// Decoding storeTransactionGooglePlay#41356639.
		v := StoreTransactionGooglePlay{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StoreTransactionClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode StoreTransactionClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// StoreTransaction boxes the StoreTransactionClass providing a helper.
type StoreTransactionBox struct {
	StoreTransaction StoreTransactionClass
}

// Decode implements bin.Decoder for StoreTransactionBox.
func (b *StoreTransactionBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode StoreTransactionBox to nil")
	}
	v, err := DecodeStoreTransaction(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.StoreTransaction = v
	return nil
}

// Encode implements bin.Encode for StoreTransactionBox.
func (b *StoreTransactionBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.StoreTransaction == nil {
		return fmt.Errorf("unable to encode StoreTransactionClass as nil")
	}
	return b.StoreTransaction.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for StoreTransactionBox.
func (b *StoreTransactionBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode StoreTransactionBox to nil")
	}
	v, err := DecodeTDLibJSONStoreTransaction(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.StoreTransaction = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for StoreTransactionBox.
func (b *StoreTransactionBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.StoreTransaction == nil {
		return fmt.Errorf("unable to encode StoreTransactionClass as nil")
	}
	return b.StoreTransaction.EncodeTDLibJSON(buf)
}
