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

// StatisticalValue represents TL type `statisticalValue#626d6a76`.
type StatisticalValue struct {
	// The current value
	Value float64
	// The value for the previous day
	PreviousValue float64
	// The growth rate of the value, as a percentage
	GrowthRatePercentage float64
}

// StatisticalValueTypeID is TL type id of StatisticalValue.
const StatisticalValueTypeID = 0x626d6a76

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

func (s *StatisticalValue) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Value == 0) {
		return false
	}
	if !(s.PreviousValue == 0) {
		return false
	}
	if !(s.GrowthRatePercentage == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StatisticalValue) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "statisticalValue",
		ID:   StatisticalValueTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Value",
			SchemaName: "value",
		},
		{
			Name:       "PreviousValue",
			SchemaName: "previous_value",
		},
		{
			Name:       "GrowthRatePercentage",
			SchemaName: "growth_rate_percentage",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StatisticalValue) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode statisticalValue#626d6a76 as nil")
	}
	b.PutID(StatisticalValueTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StatisticalValue) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode statisticalValue#626d6a76 as nil")
	}
	b.PutDouble(s.Value)
	b.PutDouble(s.PreviousValue)
	b.PutDouble(s.GrowthRatePercentage)
	return nil
}

// Decode implements bin.Decoder.
func (s *StatisticalValue) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode statisticalValue#626d6a76 to nil")
	}
	if err := b.ConsumeID(StatisticalValueTypeID); err != nil {
		return fmt.Errorf("unable to decode statisticalValue#626d6a76: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StatisticalValue) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode statisticalValue#626d6a76 to nil")
	}
	{
		value, err := b.Double()
		if err != nil {
			return fmt.Errorf("unable to decode statisticalValue#626d6a76: field value: %w", err)
		}
		s.Value = value
	}
	{
		value, err := b.Double()
		if err != nil {
			return fmt.Errorf("unable to decode statisticalValue#626d6a76: field previous_value: %w", err)
		}
		s.PreviousValue = value
	}
	{
		value, err := b.Double()
		if err != nil {
			return fmt.Errorf("unable to decode statisticalValue#626d6a76: field growth_rate_percentage: %w", err)
		}
		s.GrowthRatePercentage = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StatisticalValue) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode statisticalValue#626d6a76 as nil")
	}
	b.ObjStart()
	b.PutID("statisticalValue")
	b.Comma()
	b.FieldStart("value")
	b.PutDouble(s.Value)
	b.Comma()
	b.FieldStart("previous_value")
	b.PutDouble(s.PreviousValue)
	b.Comma()
	b.FieldStart("growth_rate_percentage")
	b.PutDouble(s.GrowthRatePercentage)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StatisticalValue) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode statisticalValue#626d6a76 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("statisticalValue"); err != nil {
				return fmt.Errorf("unable to decode statisticalValue#626d6a76: %w", err)
			}
		case "value":
			value, err := b.Double()
			if err != nil {
				return fmt.Errorf("unable to decode statisticalValue#626d6a76: field value: %w", err)
			}
			s.Value = value
		case "previous_value":
			value, err := b.Double()
			if err != nil {
				return fmt.Errorf("unable to decode statisticalValue#626d6a76: field previous_value: %w", err)
			}
			s.PreviousValue = value
		case "growth_rate_percentage":
			value, err := b.Double()
			if err != nil {
				return fmt.Errorf("unable to decode statisticalValue#626d6a76: field growth_rate_percentage: %w", err)
			}
			s.GrowthRatePercentage = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetValue returns value of Value field.
func (s *StatisticalValue) GetValue() (value float64) {
	if s == nil {
		return
	}
	return s.Value
}

// GetPreviousValue returns value of PreviousValue field.
func (s *StatisticalValue) GetPreviousValue() (value float64) {
	if s == nil {
		return
	}
	return s.PreviousValue
}

// GetGrowthRatePercentage returns value of GrowthRatePercentage field.
func (s *StatisticalValue) GetGrowthRatePercentage() (value float64) {
	if s == nil {
		return
	}
	return s.GrowthRatePercentage
}
