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

// PaymentsCreateStarGiftCollectionRequest represents TL type `payments.createStarGiftCollection#1f4a0e87`.
// Create a star gift collection »¹.
//
// Links:
//  1. https://core.telegram.org/api/gifts#gift-collections
//
// See https://core.telegram.org/method/payments.createStarGiftCollection for reference.
type PaymentsCreateStarGiftCollectionRequest struct {
	// Peer where to create the collection.
	Peer InputPeerClass
	// Title of the collection.
	Title string
	// Gifts added to the collection.
	Stargift []InputSavedStarGiftClass
}

// PaymentsCreateStarGiftCollectionRequestTypeID is TL type id of PaymentsCreateStarGiftCollectionRequest.
const PaymentsCreateStarGiftCollectionRequestTypeID = 0x1f4a0e87

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

func (c *PaymentsCreateStarGiftCollectionRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Peer == nil) {
		return false
	}
	if !(c.Title == "") {
		return false
	}
	if !(c.Stargift == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsCreateStarGiftCollectionRequest from given interface.
func (c *PaymentsCreateStarGiftCollectionRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetTitle() (value string)
	GetStargift() (value []InputSavedStarGiftClass)
}) {
	c.Peer = from.GetPeer()
	c.Title = from.GetTitle()
	c.Stargift = from.GetStargift()
}

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

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

// TypeInfo returns info about TL type.
func (c *PaymentsCreateStarGiftCollectionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.createStarGiftCollection",
		ID:   PaymentsCreateStarGiftCollectionRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "Stargift",
			SchemaName: "stargift",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *PaymentsCreateStarGiftCollectionRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.createStarGiftCollection#1f4a0e87 as nil")
	}
	b.PutID(PaymentsCreateStarGiftCollectionRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *PaymentsCreateStarGiftCollectionRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.createStarGiftCollection#1f4a0e87 as nil")
	}
	if c.Peer == nil {
		return fmt.Errorf("unable to encode payments.createStarGiftCollection#1f4a0e87: field peer is nil")
	}
	if err := c.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.createStarGiftCollection#1f4a0e87: field peer: %w", err)
	}
	b.PutString(c.Title)
	b.PutVectorHeader(len(c.Stargift))
	for idx, v := range c.Stargift {
		if v == nil {
			return fmt.Errorf("unable to encode payments.createStarGiftCollection#1f4a0e87: field stargift element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.createStarGiftCollection#1f4a0e87: field stargift element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *PaymentsCreateStarGiftCollectionRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.createStarGiftCollection#1f4a0e87 to nil")
	}
	if err := b.ConsumeID(PaymentsCreateStarGiftCollectionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.createStarGiftCollection#1f4a0e87: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *PaymentsCreateStarGiftCollectionRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.createStarGiftCollection#1f4a0e87 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.createStarGiftCollection#1f4a0e87: field peer: %w", err)
		}
		c.Peer = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.createStarGiftCollection#1f4a0e87: field title: %w", err)
		}
		c.Title = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.createStarGiftCollection#1f4a0e87: field stargift: %w", err)
		}

		if headerLen > 0 {
			c.Stargift = 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.createStarGiftCollection#1f4a0e87: field stargift: %w", err)
			}
			c.Stargift = append(c.Stargift, value)
		}
	}
	return nil
}

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

// GetTitle returns value of Title field.
func (c *PaymentsCreateStarGiftCollectionRequest) GetTitle() (value string) {
	if c == nil {
		return
	}
	return c.Title
}

// GetStargift returns value of Stargift field.
func (c *PaymentsCreateStarGiftCollectionRequest) GetStargift() (value []InputSavedStarGiftClass) {
	if c == nil {
		return
	}
	return c.Stargift
}

// MapStargift returns field Stargift wrapped in InputSavedStarGiftClassArray helper.
func (c *PaymentsCreateStarGiftCollectionRequest) MapStargift() (value InputSavedStarGiftClassArray) {
	return InputSavedStarGiftClassArray(c.Stargift)
}

// PaymentsCreateStarGiftCollection invokes method payments.createStarGiftCollection#1f4a0e87 returning error if any.
// Create a star gift 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.createStarGiftCollection for reference.
func (c *Client) PaymentsCreateStarGiftCollection(ctx context.Context, request *PaymentsCreateStarGiftCollectionRequest) (*StarGiftCollection, error) {
	var result StarGiftCollection

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