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

// AffiliateProgramParameters represents TL type `affiliateProgramParameters#61e90c54`.
type AffiliateProgramParameters struct {
	// The number of Telegram Stars received by the affiliate for each 1000 Telegram Stars
	// received by the program owner;
	CommissionPerMille int32
	// Number of months the program will be active; 0-36. If 0, then the program is eternal
	MonthCount int32
}

// AffiliateProgramParametersTypeID is TL type id of AffiliateProgramParameters.
const AffiliateProgramParametersTypeID = 0x61e90c54

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

func (a *AffiliateProgramParameters) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.CommissionPerMille == 0) {
		return false
	}
	if !(a.MonthCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AffiliateProgramParameters) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "affiliateProgramParameters",
		ID:   AffiliateProgramParametersTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CommissionPerMille",
			SchemaName: "commission_per_mille",
		},
		{
			Name:       "MonthCount",
			SchemaName: "month_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AffiliateProgramParameters) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramParameters#61e90c54 as nil")
	}
	b.PutID(AffiliateProgramParametersTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AffiliateProgramParameters) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramParameters#61e90c54 as nil")
	}
	b.PutInt32(a.CommissionPerMille)
	b.PutInt32(a.MonthCount)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (a *AffiliateProgramParameters) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramParameters#61e90c54 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode affiliateProgramParameters#61e90c54: field commission_per_mille: %w", err)
		}
		a.CommissionPerMille = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode affiliateProgramParameters#61e90c54: field month_count: %w", err)
		}
		a.MonthCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AffiliateProgramParameters) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramParameters#61e90c54 as nil")
	}
	b.ObjStart()
	b.PutID("affiliateProgramParameters")
	b.Comma()
	b.FieldStart("commission_per_mille")
	b.PutInt32(a.CommissionPerMille)
	b.Comma()
	b.FieldStart("month_count")
	b.PutInt32(a.MonthCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("affiliateProgramParameters"); err != nil {
				return fmt.Errorf("unable to decode affiliateProgramParameters#61e90c54: %w", err)
			}
		case "commission_per_mille":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode affiliateProgramParameters#61e90c54: field commission_per_mille: %w", err)
			}
			a.CommissionPerMille = value
		case "month_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode affiliateProgramParameters#61e90c54: field month_count: %w", err)
			}
			a.MonthCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetMonthCount returns value of MonthCount field.
func (a *AffiliateProgramParameters) GetMonthCount() (value int32) {
	if a == nil {
		return
	}
	return a.MonthCount
}
