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

// AffiliateInfo represents TL type `affiliateInfo#b1c1dcfa`.
type AffiliateInfo struct {
	// The number of Telegram Stars received by the affiliate for each 1000 Telegram Stars
	// received by the program owner
	CommissionPerMille int32
	// Identifier of the chat which received the commission
	AffiliateChatID int64
	// The amount of Telegram Stars that were received by the affiliate; can be negative for
	// refunds
	StarAmount StarAmount
}

// AffiliateInfoTypeID is TL type id of AffiliateInfo.
const AffiliateInfoTypeID = 0xb1c1dcfa

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

func (a *AffiliateInfo) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.CommissionPerMille == 0) {
		return false
	}
	if !(a.AffiliateChatID == 0) {
		return false
	}
	if !(a.StarAmount.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AffiliateInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "affiliateInfo",
		ID:   AffiliateInfoTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CommissionPerMille",
			SchemaName: "commission_per_mille",
		},
		{
			Name:       "AffiliateChatID",
			SchemaName: "affiliate_chat_id",
		},
		{
			Name:       "StarAmount",
			SchemaName: "star_amount",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AffiliateInfo) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateInfo#b1c1dcfa as nil")
	}
	b.PutID(AffiliateInfoTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AffiliateInfo) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateInfo#b1c1dcfa as nil")
	}
	b.PutInt32(a.CommissionPerMille)
	b.PutInt53(a.AffiliateChatID)
	if err := a.StarAmount.Encode(b); err != nil {
		return fmt.Errorf("unable to encode affiliateInfo#b1c1dcfa: field star_amount: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AffiliateInfo) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateInfo#b1c1dcfa to nil")
	}
	if err := b.ConsumeID(AffiliateInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode affiliateInfo#b1c1dcfa: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AffiliateInfo) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateInfo#b1c1dcfa to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode affiliateInfo#b1c1dcfa: field commission_per_mille: %w", err)
		}
		a.CommissionPerMille = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode affiliateInfo#b1c1dcfa: field affiliate_chat_id: %w", err)
		}
		a.AffiliateChatID = value
	}
	{
		if err := a.StarAmount.Decode(b); err != nil {
			return fmt.Errorf("unable to decode affiliateInfo#b1c1dcfa: field star_amount: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AffiliateInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateInfo#b1c1dcfa as nil")
	}
	b.ObjStart()
	b.PutID("affiliateInfo")
	b.Comma()
	b.FieldStart("commission_per_mille")
	b.PutInt32(a.CommissionPerMille)
	b.Comma()
	b.FieldStart("affiliate_chat_id")
	b.PutInt53(a.AffiliateChatID)
	b.Comma()
	b.FieldStart("star_amount")
	if err := a.StarAmount.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode affiliateInfo#b1c1dcfa: field star_amount: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AffiliateInfo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateInfo#b1c1dcfa to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("affiliateInfo"); err != nil {
				return fmt.Errorf("unable to decode affiliateInfo#b1c1dcfa: %w", err)
			}
		case "commission_per_mille":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode affiliateInfo#b1c1dcfa: field commission_per_mille: %w", err)
			}
			a.CommissionPerMille = value
		case "affiliate_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode affiliateInfo#b1c1dcfa: field affiliate_chat_id: %w", err)
			}
			a.AffiliateChatID = value
		case "star_amount":
			if err := a.StarAmount.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode affiliateInfo#b1c1dcfa: field star_amount: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCommissionPerMille returns value of CommissionPerMille field.
func (a *AffiliateInfo) GetCommissionPerMille() (value int32) {
	if a == nil {
		return
	}
	return a.CommissionPerMille
}

// GetAffiliateChatID returns value of AffiliateChatID field.
func (a *AffiliateInfo) GetAffiliateChatID() (value int64) {
	if a == nil {
		return
	}
	return a.AffiliateChatID
}

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