// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// StarsRating represents TL type `starsRating#1b0e4f07`.
// Represents the profile's star rating, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/stars#star-rating
//
// See https://core.telegram.org/constructor/starsRating for reference.
type StarsRating struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The current level, may be negative.
	Level int
	// The numerical value of the rating required for the current level.
	CurrentLevelStars int64
	// Numerical value of the current rating.
	Stars int64
	// The numerical value of the rating required for the next level.
	//
	// Use SetNextLevelStars and GetNextLevelStars helpers.
	NextLevelStars int64
}

// StarsRatingTypeID is TL type id of StarsRating.
const StarsRatingTypeID = 0x1b0e4f07

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

func (s *StarsRating) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Level == 0) {
		return false
	}
	if !(s.CurrentLevelStars == 0) {
		return false
	}
	if !(s.Stars == 0) {
		return false
	}
	if !(s.NextLevelStars == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StarsRating from given interface.
func (s *StarsRating) FillFrom(from interface {
	GetLevel() (value int)
	GetCurrentLevelStars() (value int64)
	GetStars() (value int64)
	GetNextLevelStars() (value int64, ok bool)
}) {
	s.Level = from.GetLevel()
	s.CurrentLevelStars = from.GetCurrentLevelStars()
	s.Stars = from.GetStars()
	if val, ok := from.GetNextLevelStars(); ok {
		s.NextLevelStars = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *StarsRating) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starsRating",
		ID:   StarsRatingTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Level",
			SchemaName: "level",
		},
		{
			Name:       "CurrentLevelStars",
			SchemaName: "current_level_stars",
		},
		{
			Name:       "Stars",
			SchemaName: "stars",
		},
		{
			Name:       "NextLevelStars",
			SchemaName: "next_level_stars",
			Null:       !s.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StarsRating) SetFlags() {
	if !(s.NextLevelStars == 0) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *StarsRating) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsRating#1b0e4f07 as nil")
	}
	b.PutID(StarsRatingTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarsRating) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starsRating#1b0e4f07 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starsRating#1b0e4f07: field flags: %w", err)
	}
	b.PutInt(s.Level)
	b.PutLong(s.CurrentLevelStars)
	b.PutLong(s.Stars)
	if s.Flags.Has(0) {
		b.PutLong(s.NextLevelStars)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StarsRating) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starsRating#1b0e4f07 to nil")
	}
	if err := b.ConsumeID(StarsRatingTypeID); err != nil {
		return fmt.Errorf("unable to decode starsRating#1b0e4f07: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StarsRating) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starsRating#1b0e4f07 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starsRating#1b0e4f07: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starsRating#1b0e4f07: field level: %w", err)
		}
		s.Level = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starsRating#1b0e4f07: field current_level_stars: %w", err)
		}
		s.CurrentLevelStars = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starsRating#1b0e4f07: field stars: %w", err)
		}
		s.Stars = value
	}
	if s.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starsRating#1b0e4f07: field next_level_stars: %w", err)
		}
		s.NextLevelStars = value
	}
	return nil
}

// GetLevel returns value of Level field.
func (s *StarsRating) GetLevel() (value int) {
	if s == nil {
		return
	}
	return s.Level
}

// GetCurrentLevelStars returns value of CurrentLevelStars field.
func (s *StarsRating) GetCurrentLevelStars() (value int64) {
	if s == nil {
		return
	}
	return s.CurrentLevelStars
}

// GetStars returns value of Stars field.
func (s *StarsRating) GetStars() (value int64) {
	if s == nil {
		return
	}
	return s.Stars
}

// SetNextLevelStars sets value of NextLevelStars conditional field.
func (s *StarsRating) SetNextLevelStars(value int64) {
	s.Flags.Set(0)
	s.NextLevelStars = value
}

// GetNextLevelStars returns value of NextLevelStars conditional field and
// boolean which is true if field was set.
func (s *StarsRating) GetNextLevelStars() (value int64, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.NextLevelStars, true
}
