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

// PaymentsSavedStarGifts represents TL type `payments.savedStarGifts#95f389b1`.
// Represents a list of gifts¹.
//
// Links:
//  1. https://core.telegram.org/api/gifts
//
// See https://core.telegram.org/constructor/payments.savedStarGifts for reference.
type PaymentsSavedStarGifts struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Total number of results (can be less than the returned gifts, in which case
	// next_offset will be set).
	Count int
	// Ternary value: can be not set, set&true, set&false. Can only be set for channels we
	// own: the value indicates whether we enabled gift notifications¹ for this channel.
	//
	// Links:
	//  1) https://core.telegram.org/api/gifts#notifications-for-received-channel-gifts
	//
	// Use SetChatNotificationsEnabled and GetChatNotificationsEnabled helpers.
	ChatNotificationsEnabled bool
	// Gifts
	Gifts []SavedStarGift
	// Offset to pass to payments.getSavedStarGifts¹ to fetch the next page of results.
	//
	// Links:
	//  1) https://core.telegram.org/method/payments.getSavedStarGifts
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
	// Channels mentioned in gifts
	Chats []ChatClass
	// Users mentioned in gifts
	Users []UserClass
}

// PaymentsSavedStarGiftsTypeID is TL type id of PaymentsSavedStarGifts.
const PaymentsSavedStarGiftsTypeID = 0x95f389b1

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

func (s *PaymentsSavedStarGifts) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Count == 0) {
		return false
	}
	if !(s.ChatNotificationsEnabled == false) {
		return false
	}
	if !(s.Gifts == nil) {
		return false
	}
	if !(s.NextOffset == "") {
		return false
	}
	if !(s.Chats == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsSavedStarGifts from given interface.
func (s *PaymentsSavedStarGifts) FillFrom(from interface {
	GetCount() (value int)
	GetChatNotificationsEnabled() (value bool, ok bool)
	GetGifts() (value []SavedStarGift)
	GetNextOffset() (value string, ok bool)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	s.Count = from.GetCount()
	if val, ok := from.GetChatNotificationsEnabled(); ok {
		s.ChatNotificationsEnabled = val
	}

	s.Gifts = from.GetGifts()
	if val, ok := from.GetNextOffset(); ok {
		s.NextOffset = val
	}

	s.Chats = from.GetChats()
	s.Users = from.GetUsers()
}

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

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

// TypeInfo returns info about TL type.
func (s *PaymentsSavedStarGifts) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.savedStarGifts",
		ID:   PaymentsSavedStarGiftsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "ChatNotificationsEnabled",
			SchemaName: "chat_notifications_enabled",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "Gifts",
			SchemaName: "gifts",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

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

// Encode implements bin.Encoder.
func (s *PaymentsSavedStarGifts) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.savedStarGifts#95f389b1 as nil")
	}
	b.PutID(PaymentsSavedStarGiftsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsSavedStarGifts) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.savedStarGifts#95f389b1 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.savedStarGifts#95f389b1: field flags: %w", err)
	}
	b.PutInt(s.Count)
	if s.Flags.Has(1) {
		b.PutBool(s.ChatNotificationsEnabled)
	}
	b.PutVectorHeader(len(s.Gifts))
	for idx, v := range s.Gifts {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.savedStarGifts#95f389b1: field gifts element with index %d: %w", idx, err)
		}
	}
	if s.Flags.Has(0) {
		b.PutString(s.NextOffset)
	}
	b.PutVectorHeader(len(s.Chats))
	for idx, v := range s.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode payments.savedStarGifts#95f389b1: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.savedStarGifts#95f389b1: field chats 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.savedStarGifts#95f389b1: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.savedStarGifts#95f389b1: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsSavedStarGifts) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.savedStarGifts#95f389b1 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: field count: %w", err)
		}
		s.Count = value
	}
	if s.Flags.Has(1) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: field chat_notifications_enabled: %w", err)
		}
		s.ChatNotificationsEnabled = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: field gifts: %w", err)
		}

		if headerLen > 0 {
			s.Gifts = make([]SavedStarGift, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value SavedStarGift
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: field gifts: %w", err)
			}
			s.Gifts = append(s.Gifts, value)
		}
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: field next_offset: %w", err)
		}
		s.NextOffset = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: field chats: %w", err)
		}

		if headerLen > 0 {
			s.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: field chats: %w", err)
			}
			s.Chats = append(s.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.savedStarGifts#95f389b1: 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.savedStarGifts#95f389b1: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	return nil
}

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

// SetChatNotificationsEnabled sets value of ChatNotificationsEnabled conditional field.
func (s *PaymentsSavedStarGifts) SetChatNotificationsEnabled(value bool) {
	s.Flags.Set(1)
	s.ChatNotificationsEnabled = value
}

// GetChatNotificationsEnabled returns value of ChatNotificationsEnabled conditional field and
// boolean which is true if field was set.
func (s *PaymentsSavedStarGifts) GetChatNotificationsEnabled() (value bool, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.ChatNotificationsEnabled, true
}

// GetGifts returns value of Gifts field.
func (s *PaymentsSavedStarGifts) GetGifts() (value []SavedStarGift) {
	if s == nil {
		return
	}
	return s.Gifts
}

// SetNextOffset sets value of NextOffset conditional field.
func (s *PaymentsSavedStarGifts) 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 *PaymentsSavedStarGifts) GetNextOffset() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.NextOffset, true
}

// GetChats returns value of Chats field.
func (s *PaymentsSavedStarGifts) GetChats() (value []ChatClass) {
	if s == nil {
		return
	}
	return s.Chats
}

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

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (s *PaymentsSavedStarGifts) MapChats() (value ChatClassArray) {
	return ChatClassArray(s.Chats)
}

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