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

// StarSubscriptions represents TL type `starSubscriptions#a2f27c8d`.
type StarSubscriptions struct {
	// The amount of owned Telegram Stars
	StarAmount StarAmount
	// List of subscriptions for Telegram Stars
	Subscriptions []StarSubscription
	// The number of Telegram Stars required to buy to extend subscriptions expiring soon
	RequiredStarCount int64
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// StarSubscriptionsTypeID is TL type id of StarSubscriptions.
const StarSubscriptionsTypeID = 0xa2f27c8d

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

func (s *StarSubscriptions) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.StarAmount.Zero()) {
		return false
	}
	if !(s.Subscriptions == nil) {
		return false
	}
	if !(s.RequiredStarCount == 0) {
		return false
	}
	if !(s.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StarSubscriptions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starSubscriptions",
		ID:   StarSubscriptionsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StarAmount",
			SchemaName: "star_amount",
		},
		{
			Name:       "Subscriptions",
			SchemaName: "subscriptions",
		},
		{
			Name:       "RequiredStarCount",
			SchemaName: "required_star_count",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StarSubscriptions) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptions#a2f27c8d as nil")
	}
	b.PutID(StarSubscriptionsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarSubscriptions) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptions#a2f27c8d as nil")
	}
	if err := s.StarAmount.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starSubscriptions#a2f27c8d: field star_amount: %w", err)
	}
	b.PutInt(len(s.Subscriptions))
	for idx, v := range s.Subscriptions {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare starSubscriptions#a2f27c8d: field subscriptions element with index %d: %w", idx, err)
		}
	}
	b.PutInt53(s.RequiredStarCount)
	b.PutString(s.NextOffset)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StarSubscriptions) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starSubscriptions#a2f27c8d to nil")
	}
	{
		if err := s.StarAmount.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field star_amount: %w", err)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field subscriptions: %w", err)
		}

		if headerLen > 0 {
			s.Subscriptions = make([]StarSubscription, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarSubscription
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare starSubscriptions#a2f27c8d: field subscriptions: %w", err)
			}
			s.Subscriptions = append(s.Subscriptions, value)
		}
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field required_star_count: %w", err)
		}
		s.RequiredStarCount = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field next_offset: %w", err)
		}
		s.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StarSubscriptions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptions#a2f27c8d as nil")
	}
	b.ObjStart()
	b.PutID("starSubscriptions")
	b.Comma()
	b.FieldStart("star_amount")
	if err := s.StarAmount.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode starSubscriptions#a2f27c8d: field star_amount: %w", err)
	}
	b.Comma()
	b.FieldStart("subscriptions")
	b.ArrStart()
	for idx, v := range s.Subscriptions {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode starSubscriptions#a2f27c8d: field subscriptions element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("required_star_count")
	b.PutInt53(s.RequiredStarCount)
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(s.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("starSubscriptions"); err != nil {
				return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: %w", err)
			}
		case "star_amount":
			if err := s.StarAmount.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field star_amount: %w", err)
			}
		case "subscriptions":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value StarSubscription
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field subscriptions: %w", err)
				}
				s.Subscriptions = append(s.Subscriptions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field subscriptions: %w", err)
			}
		case "required_star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field required_star_count: %w", err)
			}
			s.RequiredStarCount = value
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode starSubscriptions#a2f27c8d: field next_offset: %w", err)
			}
			s.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStarAmount returns value of StarAmount field.
func (s *StarSubscriptions) GetStarAmount() (value StarAmount) {
	if s == nil {
		return
	}
	return s.StarAmount
}

// GetSubscriptions returns value of Subscriptions field.
func (s *StarSubscriptions) GetSubscriptions() (value []StarSubscription) {
	if s == nil {
		return
	}
	return s.Subscriptions
}

// GetRequiredStarCount returns value of RequiredStarCount field.
func (s *StarSubscriptions) GetRequiredStarCount() (value int64) {
	if s == nil {
		return
	}
	return s.RequiredStarCount
}

// GetNextOffset returns value of NextOffset field.
func (s *StarSubscriptions) GetNextOffset() (value string) {
	if s == nil {
		return
	}
	return s.NextOffset
}
