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

// PremiumFeatures represents TL type `premiumFeatures#4cf10e82`.
type PremiumFeatures struct {
	// The list of available features
	Features []PremiumFeatureClass
	// The list of limits, increased for Premium users
	Limits []PremiumLimit
	// An internal link to be opened to pay for Telegram Premium if store payment isn't
	// possible; may be null if direct payment isn't available
	PaymentLink InternalLinkTypeClass
}

// PremiumFeaturesTypeID is TL type id of PremiumFeatures.
const PremiumFeaturesTypeID = 0x4cf10e82

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

func (p *PremiumFeatures) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Features == nil) {
		return false
	}
	if !(p.Limits == nil) {
		return false
	}
	if !(p.PaymentLink == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PremiumFeatures) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premiumFeatures",
		ID:   PremiumFeaturesTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Features",
			SchemaName: "features",
		},
		{
			Name:       "Limits",
			SchemaName: "limits",
		},
		{
			Name:       "PaymentLink",
			SchemaName: "payment_link",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PremiumFeatures) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumFeatures#4cf10e82 as nil")
	}
	b.PutID(PremiumFeaturesTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumFeatures) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumFeatures#4cf10e82 as nil")
	}
	b.PutInt(len(p.Features))
	for idx, v := range p.Features {
		if v == nil {
			return fmt.Errorf("unable to encode premiumFeatures#4cf10e82: field features element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare premiumFeatures#4cf10e82: field features element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(p.Limits))
	for idx, v := range p.Limits {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare premiumFeatures#4cf10e82: field limits element with index %d: %w", idx, err)
		}
	}
	if p.PaymentLink == nil {
		return fmt.Errorf("unable to encode premiumFeatures#4cf10e82: field payment_link is nil")
	}
	if err := p.PaymentLink.Encode(b); err != nil {
		return fmt.Errorf("unable to encode premiumFeatures#4cf10e82: field payment_link: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PremiumFeatures) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumFeatures#4cf10e82 to nil")
	}
	if err := b.ConsumeID(PremiumFeaturesTypeID); err != nil {
		return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PremiumFeatures) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumFeatures#4cf10e82 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field features: %w", err)
		}

		if headerLen > 0 {
			p.Features = make([]PremiumFeatureClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodePremiumFeature(b)
			if err != nil {
				return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field features: %w", err)
			}
			p.Features = append(p.Features, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field limits: %w", err)
		}

		if headerLen > 0 {
			p.Limits = make([]PremiumLimit, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value PremiumLimit
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare premiumFeatures#4cf10e82: field limits: %w", err)
			}
			p.Limits = append(p.Limits, value)
		}
	}
	{
		value, err := DecodeInternalLinkType(b)
		if err != nil {
			return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field payment_link: %w", err)
		}
		p.PaymentLink = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PremiumFeatures) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumFeatures#4cf10e82 as nil")
	}
	b.ObjStart()
	b.PutID("premiumFeatures")
	b.Comma()
	b.FieldStart("features")
	b.ArrStart()
	for idx, v := range p.Features {
		if v == nil {
			return fmt.Errorf("unable to encode premiumFeatures#4cf10e82: field features element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode premiumFeatures#4cf10e82: field features element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("limits")
	b.ArrStart()
	for idx, v := range p.Limits {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode premiumFeatures#4cf10e82: field limits element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("payment_link")
	if p.PaymentLink == nil {
		return fmt.Errorf("unable to encode premiumFeatures#4cf10e82: field payment_link is nil")
	}
	if err := p.PaymentLink.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode premiumFeatures#4cf10e82: field payment_link: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PremiumFeatures) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumFeatures#4cf10e82 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("premiumFeatures"); err != nil {
				return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: %w", err)
			}
		case "features":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONPremiumFeature(b)
				if err != nil {
					return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field features: %w", err)
				}
				p.Features = append(p.Features, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field features: %w", err)
			}
		case "limits":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value PremiumLimit
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field limits: %w", err)
				}
				p.Limits = append(p.Limits, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field limits: %w", err)
			}
		case "payment_link":
			value, err := DecodeTDLibJSONInternalLinkType(b)
			if err != nil {
				return fmt.Errorf("unable to decode premiumFeatures#4cf10e82: field payment_link: %w", err)
			}
			p.PaymentLink = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetFeatures returns value of Features field.
func (p *PremiumFeatures) GetFeatures() (value []PremiumFeatureClass) {
	if p == nil {
		return
	}
	return p.Features
}

// GetLimits returns value of Limits field.
func (p *PremiumFeatures) GetLimits() (value []PremiumLimit) {
	if p == nil {
		return
	}
	return p.Limits
}

// GetPaymentLink returns value of PaymentLink field.
func (p *PremiumFeatures) GetPaymentLink() (value InternalLinkTypeClass) {
	if p == nil {
		return
	}
	return p.PaymentLink
}
