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

// PremiumLimit represents TL type `premiumLimit#7ed372e6`.
type PremiumLimit struct {
	// The type of the limit
	Type PremiumLimitTypeClass
	// Default value of the limit
	DefaultValue int32
	// Value of the limit for Premium users
	PremiumValue int32
}

// PremiumLimitTypeID is TL type id of PremiumLimit.
const PremiumLimitTypeID = 0x7ed372e6

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

func (p *PremiumLimit) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Type == nil) {
		return false
	}
	if !(p.DefaultValue == 0) {
		return false
	}
	if !(p.PremiumValue == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PremiumLimit) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premiumLimit",
		ID:   PremiumLimitTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "DefaultValue",
			SchemaName: "default_value",
		},
		{
			Name:       "PremiumValue",
			SchemaName: "premium_value",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PremiumLimit) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumLimit#7ed372e6 as nil")
	}
	b.PutID(PremiumLimitTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumLimit) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumLimit#7ed372e6 as nil")
	}
	if p.Type == nil {
		return fmt.Errorf("unable to encode premiumLimit#7ed372e6: field type is nil")
	}
	if err := p.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode premiumLimit#7ed372e6: field type: %w", err)
	}
	b.PutInt32(p.DefaultValue)
	b.PutInt32(p.PremiumValue)
	return nil
}

// Decode implements bin.Decoder.
func (p *PremiumLimit) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumLimit#7ed372e6 to nil")
	}
	if err := b.ConsumeID(PremiumLimitTypeID); err != nil {
		return fmt.Errorf("unable to decode premiumLimit#7ed372e6: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PremiumLimit) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumLimit#7ed372e6 to nil")
	}
	{
		value, err := DecodePremiumLimitType(b)
		if err != nil {
			return fmt.Errorf("unable to decode premiumLimit#7ed372e6: field type: %w", err)
		}
		p.Type = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode premiumLimit#7ed372e6: field default_value: %w", err)
		}
		p.DefaultValue = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode premiumLimit#7ed372e6: field premium_value: %w", err)
		}
		p.PremiumValue = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PremiumLimit) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumLimit#7ed372e6 as nil")
	}
	b.ObjStart()
	b.PutID("premiumLimit")
	b.Comma()
	b.FieldStart("type")
	if p.Type == nil {
		return fmt.Errorf("unable to encode premiumLimit#7ed372e6: field type is nil")
	}
	if err := p.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode premiumLimit#7ed372e6: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("default_value")
	b.PutInt32(p.DefaultValue)
	b.Comma()
	b.FieldStart("premium_value")
	b.PutInt32(p.PremiumValue)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PremiumLimit) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumLimit#7ed372e6 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("premiumLimit"); err != nil {
				return fmt.Errorf("unable to decode premiumLimit#7ed372e6: %w", err)
			}
		case "type":
			value, err := DecodeTDLibJSONPremiumLimitType(b)
			if err != nil {
				return fmt.Errorf("unable to decode premiumLimit#7ed372e6: field type: %w", err)
			}
			p.Type = value
		case "default_value":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode premiumLimit#7ed372e6: field default_value: %w", err)
			}
			p.DefaultValue = value
		case "premium_value":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode premiumLimit#7ed372e6: field premium_value: %w", err)
			}
			p.PremiumValue = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (p *PremiumLimit) GetType() (value PremiumLimitTypeClass) {
	if p == nil {
		return
	}
	return p.Type
}

// GetDefaultValue returns value of DefaultValue field.
func (p *PremiumLimit) GetDefaultValue() (value int32) {
	if p == nil {
		return
	}
	return p.DefaultValue
}

// GetPremiumValue returns value of PremiumValue field.
func (p *PremiumLimit) GetPremiumValue() (value int32) {
	if p == nil {
		return
	}
	return p.PremiumValue
}
