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

// LabeledPricePart represents TL type `labeledPricePart#20f2e726`.
type LabeledPricePart struct {
	// Label for this portion of the product price
	Label string
	// Currency amount in the smallest units of the currency
	Amount int64
}

// LabeledPricePartTypeID is TL type id of LabeledPricePart.
const LabeledPricePartTypeID = 0x20f2e726

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

func (l *LabeledPricePart) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.Label == "") {
		return false
	}
	if !(l.Amount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LabeledPricePart) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "labeledPricePart",
		ID:   LabeledPricePartTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Label",
			SchemaName: "label",
		},
		{
			Name:       "Amount",
			SchemaName: "amount",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LabeledPricePart) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode labeledPricePart#20f2e726 as nil")
	}
	b.PutID(LabeledPricePartTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LabeledPricePart) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode labeledPricePart#20f2e726 as nil")
	}
	b.PutString(l.Label)
	b.PutInt53(l.Amount)
	return nil
}

// Decode implements bin.Decoder.
func (l *LabeledPricePart) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode labeledPricePart#20f2e726 to nil")
	}
	if err := b.ConsumeID(LabeledPricePartTypeID); err != nil {
		return fmt.Errorf("unable to decode labeledPricePart#20f2e726: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LabeledPricePart) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode labeledPricePart#20f2e726 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode labeledPricePart#20f2e726: field label: %w", err)
		}
		l.Label = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode labeledPricePart#20f2e726: field amount: %w", err)
		}
		l.Amount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LabeledPricePart) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode labeledPricePart#20f2e726 as nil")
	}
	b.ObjStart()
	b.PutID("labeledPricePart")
	b.Comma()
	b.FieldStart("label")
	b.PutString(l.Label)
	b.Comma()
	b.FieldStart("amount")
	b.PutInt53(l.Amount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LabeledPricePart) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode labeledPricePart#20f2e726 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("labeledPricePart"); err != nil {
				return fmt.Errorf("unable to decode labeledPricePart#20f2e726: %w", err)
			}
		case "label":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode labeledPricePart#20f2e726: field label: %w", err)
			}
			l.Label = value
		case "amount":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode labeledPricePart#20f2e726: field amount: %w", err)
			}
			l.Amount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetLabel returns value of Label field.
func (l *LabeledPricePart) GetLabel() (value string) {
	if l == nil {
		return
	}
	return l.Label
}

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