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

// PaymentsToggleStarGiftsPinnedToTopRequest represents TL type `payments.toggleStarGiftsPinnedToTop#1513e7b0`.
// Pins a received gift on top of the profile of the user or owned channels by using
// payments.toggleStarGiftsPinnedToTop¹.
//
// Links:
//  1. https://core.telegram.org/method/payments.toggleStarGiftsPinnedToTop
//
// See https://core.telegram.org/method/payments.toggleStarGiftsPinnedToTop for reference.
type PaymentsToggleStarGiftsPinnedToTopRequest struct {
	// The peer where to pin the gift.
	Peer InputPeerClass
	// The gift to pin.
	Stargift []InputSavedStarGiftClass
}

// PaymentsToggleStarGiftsPinnedToTopRequestTypeID is TL type id of PaymentsToggleStarGiftsPinnedToTopRequest.
const PaymentsToggleStarGiftsPinnedToTopRequestTypeID = 0x1513e7b0

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

func (t *PaymentsToggleStarGiftsPinnedToTopRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Peer == nil) {
		return false
	}
	if !(t.Stargift == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsToggleStarGiftsPinnedToTopRequest from given interface.
func (t *PaymentsToggleStarGiftsPinnedToTopRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetStargift() (value []InputSavedStarGiftClass)
}) {
	t.Peer = from.GetPeer()
	t.Stargift = from.GetStargift()
}

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

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

// TypeInfo returns info about TL type.
func (t *PaymentsToggleStarGiftsPinnedToTopRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.toggleStarGiftsPinnedToTop",
		ID:   PaymentsToggleStarGiftsPinnedToTopRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Stargift",
			SchemaName: "stargift",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *PaymentsToggleStarGiftsPinnedToTopRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode payments.toggleStarGiftsPinnedToTop#1513e7b0 as nil")
	}
	b.PutID(PaymentsToggleStarGiftsPinnedToTopRequestTypeID)
	return t.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (t *PaymentsToggleStarGiftsPinnedToTopRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode payments.toggleStarGiftsPinnedToTop#1513e7b0 to nil")
	}
	if err := b.ConsumeID(PaymentsToggleStarGiftsPinnedToTopRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.toggleStarGiftsPinnedToTop#1513e7b0: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *PaymentsToggleStarGiftsPinnedToTopRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode payments.toggleStarGiftsPinnedToTop#1513e7b0 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.toggleStarGiftsPinnedToTop#1513e7b0: field peer: %w", err)
		}
		t.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.toggleStarGiftsPinnedToTop#1513e7b0: field stargift: %w", err)
		}

		if headerLen > 0 {
			t.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.toggleStarGiftsPinnedToTop#1513e7b0: field stargift: %w", err)
			}
			t.Stargift = append(t.Stargift, value)
		}
	}
	return nil
}

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

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

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

// PaymentsToggleStarGiftsPinnedToTop invokes method payments.toggleStarGiftsPinnedToTop#1513e7b0 returning error if any.
// Pins a received gift on top of the profile of the user or owned channels by using
// payments.toggleStarGiftsPinnedToTop¹.
//
// Links:
//  1. https://core.telegram.org/method/payments.toggleStarGiftsPinnedToTop
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.toggleStarGiftsPinnedToTop for reference.
func (c *Client) PaymentsToggleStarGiftsPinnedToTop(ctx context.Context, request *PaymentsToggleStarGiftsPinnedToTopRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
