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

// PaymentsSuggestedStarRefBots represents TL type `payments.suggestedStarRefBots#b4d5d859`.
// A list of suggested mini apps¹ with available affiliate programs²
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//  2. https://core.telegram.org/api/bots/referrals
//
// See https://core.telegram.org/constructor/payments.suggestedStarRefBots for reference.
type PaymentsSuggestedStarRefBots struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Total number of results (for pagination)
	Count int
	// Suggested affiliate programs (full or partial list to be fetched using pagination)
	SuggestedBots []StarRefProgram
	// Peers mentioned in suggested_bots
	Users []UserClass
	// Next offset for pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
}

// PaymentsSuggestedStarRefBotsTypeID is TL type id of PaymentsSuggestedStarRefBots.
const PaymentsSuggestedStarRefBotsTypeID = 0xb4d5d859

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

func (s *PaymentsSuggestedStarRefBots) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Count == 0) {
		return false
	}
	if !(s.SuggestedBots == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}
	if !(s.NextOffset == "") {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsSuggestedStarRefBots from given interface.
func (s *PaymentsSuggestedStarRefBots) FillFrom(from interface {
	GetCount() (value int)
	GetSuggestedBots() (value []StarRefProgram)
	GetUsers() (value []UserClass)
	GetNextOffset() (value string, ok bool)
}) {
	s.Count = from.GetCount()
	s.SuggestedBots = from.GetSuggestedBots()
	s.Users = from.GetUsers()
	if val, ok := from.GetNextOffset(); ok {
		s.NextOffset = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*PaymentsSuggestedStarRefBots) TypeName() string {
	return "payments.suggestedStarRefBots"
}

// TypeInfo returns info about TL type.
func (s *PaymentsSuggestedStarRefBots) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.suggestedStarRefBots",
		ID:   PaymentsSuggestedStarRefBotsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "SuggestedBots",
			SchemaName: "suggested_bots",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !s.Flags.Has(0),
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (s *PaymentsSuggestedStarRefBots) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.suggestedStarRefBots#b4d5d859 as nil")
	}
	b.PutID(PaymentsSuggestedStarRefBotsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsSuggestedStarRefBots) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.suggestedStarRefBots#b4d5d859 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.suggestedStarRefBots#b4d5d859: field flags: %w", err)
	}
	b.PutInt(s.Count)
	b.PutVectorHeader(len(s.SuggestedBots))
	for idx, v := range s.SuggestedBots {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.suggestedStarRefBots#b4d5d859: field suggested_bots element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Users))
	for idx, v := range s.Users {
		if v == nil {
			return fmt.Errorf("unable to encode payments.suggestedStarRefBots#b4d5d859: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.suggestedStarRefBots#b4d5d859: field users element with index %d: %w", idx, err)
		}
	}
	if s.Flags.Has(0) {
		b.PutString(s.NextOffset)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsSuggestedStarRefBots) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.suggestedStarRefBots#b4d5d859 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.suggestedStarRefBots#b4d5d859: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.suggestedStarRefBots#b4d5d859: field count: %w", err)
		}
		s.Count = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.suggestedStarRefBots#b4d5d859: field suggested_bots: %w", err)
		}

		if headerLen > 0 {
			s.SuggestedBots = make([]StarRefProgram, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarRefProgram
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.suggestedStarRefBots#b4d5d859: field suggested_bots: %w", err)
			}
			s.SuggestedBots = append(s.SuggestedBots, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.suggestedStarRefBots#b4d5d859: field users: %w", err)
		}

		if headerLen > 0 {
			s.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.suggestedStarRefBots#b4d5d859: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.suggestedStarRefBots#b4d5d859: field next_offset: %w", err)
		}
		s.NextOffset = value
	}
	return nil
}

// GetCount returns value of Count field.
func (s *PaymentsSuggestedStarRefBots) GetCount() (value int) {
	if s == nil {
		return
	}
	return s.Count
}

// GetSuggestedBots returns value of SuggestedBots field.
func (s *PaymentsSuggestedStarRefBots) GetSuggestedBots() (value []StarRefProgram) {
	if s == nil {
		return
	}
	return s.SuggestedBots
}

// GetUsers returns value of Users field.
func (s *PaymentsSuggestedStarRefBots) GetUsers() (value []UserClass) {
	if s == nil {
		return
	}
	return s.Users
}

// SetNextOffset sets value of NextOffset conditional field.
func (s *PaymentsSuggestedStarRefBots) SetNextOffset(value string) {
	s.Flags.Set(0)
	s.NextOffset = value
}

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

// MapUsers returns field Users wrapped in UserClassArray helper.
func (s *PaymentsSuggestedStarRefBots) MapUsers() (value UserClassArray) {
	return UserClassArray(s.Users)
}
