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

// PaymentsUpdateStarGiftCollectionRequest represents TL type `payments.updateStarGiftCollection#4fddbee7`.
// Add or remove gifts from a star gift collection »¹, or rename the collection.
//
// Links:
//  1. https://core.telegram.org/api/gifts#gift-collections
//
// See https://core.telegram.org/method/payments.updateStarGiftCollection for reference.
type PaymentsUpdateStarGiftCollectionRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Peer that owns the collection.
	Peer InputPeerClass
	// Collection ID.
	CollectionID int
	// Title of the collection, to rename the collection.
	//
	// Use SetTitle and GetTitle helpers.
	Title string
	// Can contain a list of gifts to remove from the collection.
	//
	// Use SetDeleteStargift and GetDeleteStargift helpers.
	DeleteStargift []InputSavedStarGiftClass
	// Can contain a list of gifts to add to the collection.
	//
	// Use SetAddStargift and GetAddStargift helpers.
	AddStargift []InputSavedStarGiftClass
	// Can contain the new gift order.
	//
	// Use SetOrder and GetOrder helpers.
	Order []InputSavedStarGiftClass
}

// PaymentsUpdateStarGiftCollectionRequestTypeID is TL type id of PaymentsUpdateStarGiftCollectionRequest.
const PaymentsUpdateStarGiftCollectionRequestTypeID = 0x4fddbee7

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

func (u *PaymentsUpdateStarGiftCollectionRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.Peer == nil) {
		return false
	}
	if !(u.CollectionID == 0) {
		return false
	}
	if !(u.Title == "") {
		return false
	}
	if !(u.DeleteStargift == nil) {
		return false
	}
	if !(u.AddStargift == nil) {
		return false
	}
	if !(u.Order == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsUpdateStarGiftCollectionRequest from given interface.
func (u *PaymentsUpdateStarGiftCollectionRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetCollectionID() (value int)
	GetTitle() (value string, ok bool)
	GetDeleteStargift() (value []InputSavedStarGiftClass, ok bool)
	GetAddStargift() (value []InputSavedStarGiftClass, ok bool)
	GetOrder() (value []InputSavedStarGiftClass, ok bool)
}) {
	u.Peer = from.GetPeer()
	u.CollectionID = from.GetCollectionID()
	if val, ok := from.GetTitle(); ok {
		u.Title = val
	}

	if val, ok := from.GetDeleteStargift(); ok {
		u.DeleteStargift = val
	}

	if val, ok := from.GetAddStargift(); ok {
		u.AddStargift = val
	}

	if val, ok := from.GetOrder(); ok {
		u.Order = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (u *PaymentsUpdateStarGiftCollectionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.updateStarGiftCollection",
		ID:   PaymentsUpdateStarGiftCollectionRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "CollectionID",
			SchemaName: "collection_id",
		},
		{
			Name:       "Title",
			SchemaName: "title",
			Null:       !u.Flags.Has(0),
		},
		{
			Name:       "DeleteStargift",
			SchemaName: "delete_stargift",
			Null:       !u.Flags.Has(1),
		},
		{
			Name:       "AddStargift",
			SchemaName: "add_stargift",
			Null:       !u.Flags.Has(2),
		},
		{
			Name:       "Order",
			SchemaName: "order",
			Null:       !u.Flags.Has(3),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *PaymentsUpdateStarGiftCollectionRequest) SetFlags() {
	if !(u.Title == "") {
		u.Flags.Set(0)
	}
	if !(u.DeleteStargift == nil) {
		u.Flags.Set(1)
	}
	if !(u.AddStargift == nil) {
		u.Flags.Set(2)
	}
	if !(u.Order == nil) {
		u.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (u *PaymentsUpdateStarGiftCollectionRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode payments.updateStarGiftCollection#4fddbee7 as nil")
	}
	b.PutID(PaymentsUpdateStarGiftCollectionRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *PaymentsUpdateStarGiftCollectionRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode payments.updateStarGiftCollection#4fddbee7 as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field flags: %w", err)
	}
	if u.Peer == nil {
		return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field peer is nil")
	}
	if err := u.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field peer: %w", err)
	}
	b.PutInt(u.CollectionID)
	if u.Flags.Has(0) {
		b.PutString(u.Title)
	}
	if u.Flags.Has(1) {
		b.PutVectorHeader(len(u.DeleteStargift))
		for idx, v := range u.DeleteStargift {
			if v == nil {
				return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field delete_stargift element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field delete_stargift element with index %d: %w", idx, err)
			}
		}
	}
	if u.Flags.Has(2) {
		b.PutVectorHeader(len(u.AddStargift))
		for idx, v := range u.AddStargift {
			if v == nil {
				return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field add_stargift element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field add_stargift element with index %d: %w", idx, err)
			}
		}
	}
	if u.Flags.Has(3) {
		b.PutVectorHeader(len(u.Order))
		for idx, v := range u.Order {
			if v == nil {
				return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field order element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode payments.updateStarGiftCollection#4fddbee7: field order element with index %d: %w", idx, err)
			}
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *PaymentsUpdateStarGiftCollectionRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode payments.updateStarGiftCollection#4fddbee7 to nil")
	}
	if err := b.ConsumeID(PaymentsUpdateStarGiftCollectionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *PaymentsUpdateStarGiftCollectionRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode payments.updateStarGiftCollection#4fddbee7 to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field peer: %w", err)
		}
		u.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field collection_id: %w", err)
		}
		u.CollectionID = value
	}
	if u.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field title: %w", err)
		}
		u.Title = value
	}
	if u.Flags.Has(1) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field delete_stargift: %w", err)
		}

		if headerLen > 0 {
			u.DeleteStargift = make([]InputSavedStarGiftClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputSavedStarGift(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field delete_stargift: %w", err)
			}
			u.DeleteStargift = append(u.DeleteStargift, value)
		}
	}
	if u.Flags.Has(2) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field add_stargift: %w", err)
		}

		if headerLen > 0 {
			u.AddStargift = make([]InputSavedStarGiftClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputSavedStarGift(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field add_stargift: %w", err)
			}
			u.AddStargift = append(u.AddStargift, value)
		}
	}
	if u.Flags.Has(3) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field order: %w", err)
		}

		if headerLen > 0 {
			u.Order = make([]InputSavedStarGiftClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputSavedStarGift(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.updateStarGiftCollection#4fddbee7: field order: %w", err)
			}
			u.Order = append(u.Order, value)
		}
	}
	return nil
}

// GetPeer returns value of Peer field.
func (u *PaymentsUpdateStarGiftCollectionRequest) GetPeer() (value InputPeerClass) {
	if u == nil {
		return
	}
	return u.Peer
}

// GetCollectionID returns value of CollectionID field.
func (u *PaymentsUpdateStarGiftCollectionRequest) GetCollectionID() (value int) {
	if u == nil {
		return
	}
	return u.CollectionID
}

// SetTitle sets value of Title conditional field.
func (u *PaymentsUpdateStarGiftCollectionRequest) SetTitle(value string) {
	u.Flags.Set(0)
	u.Title = value
}

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

// SetDeleteStargift sets value of DeleteStargift conditional field.
func (u *PaymentsUpdateStarGiftCollectionRequest) SetDeleteStargift(value []InputSavedStarGiftClass) {
	u.Flags.Set(1)
	u.DeleteStargift = value
}

// GetDeleteStargift returns value of DeleteStargift conditional field and
// boolean which is true if field was set.
func (u *PaymentsUpdateStarGiftCollectionRequest) GetDeleteStargift() (value []InputSavedStarGiftClass, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(1) {
		return value, false
	}
	return u.DeleteStargift, true
}

// SetAddStargift sets value of AddStargift conditional field.
func (u *PaymentsUpdateStarGiftCollectionRequest) SetAddStargift(value []InputSavedStarGiftClass) {
	u.Flags.Set(2)
	u.AddStargift = value
}

// GetAddStargift returns value of AddStargift conditional field and
// boolean which is true if field was set.
func (u *PaymentsUpdateStarGiftCollectionRequest) GetAddStargift() (value []InputSavedStarGiftClass, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(2) {
		return value, false
	}
	return u.AddStargift, true
}

// SetOrder sets value of Order conditional field.
func (u *PaymentsUpdateStarGiftCollectionRequest) SetOrder(value []InputSavedStarGiftClass) {
	u.Flags.Set(3)
	u.Order = value
}

// GetOrder returns value of Order conditional field and
// boolean which is true if field was set.
func (u *PaymentsUpdateStarGiftCollectionRequest) GetOrder() (value []InputSavedStarGiftClass, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(3) {
		return value, false
	}
	return u.Order, true
}

// MapDeleteStargift returns field DeleteStargift wrapped in InputSavedStarGiftClassArray helper.
func (u *PaymentsUpdateStarGiftCollectionRequest) MapDeleteStargift() (value InputSavedStarGiftClassArray, ok bool) {
	if !u.Flags.Has(1) {
		return value, false
	}
	return InputSavedStarGiftClassArray(u.DeleteStargift), true
}

// MapAddStargift returns field AddStargift wrapped in InputSavedStarGiftClassArray helper.
func (u *PaymentsUpdateStarGiftCollectionRequest) MapAddStargift() (value InputSavedStarGiftClassArray, ok bool) {
	if !u.Flags.Has(2) {
		return value, false
	}
	return InputSavedStarGiftClassArray(u.AddStargift), true
}

// MapOrder returns field Order wrapped in InputSavedStarGiftClassArray helper.
func (u *PaymentsUpdateStarGiftCollectionRequest) MapOrder() (value InputSavedStarGiftClassArray, ok bool) {
	if !u.Flags.Has(3) {
		return value, false
	}
	return InputSavedStarGiftClassArray(u.Order), true
}

// PaymentsUpdateStarGiftCollection invokes method payments.updateStarGiftCollection#4fddbee7 returning error if any.
// Add or remove gifts from a star gift collection »¹, or rename the collection.
//
// Links:
//  1. https://core.telegram.org/api/gifts#gift-collections
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.updateStarGiftCollection for reference.
func (c *Client) PaymentsUpdateStarGiftCollection(ctx context.Context, request *PaymentsUpdateStarGiftCollectionRequest) (*StarGiftCollection, error) {
	var result StarGiftCollection

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
