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

// PaymentsSaveStarGiftRequest represents TL type `payments.saveStarGift#2a2a697c`.
// Display or remove a received gift »¹ from our profile.
//
// Links:
//  1. https://core.telegram.org/api/gifts
//
// See https://core.telegram.org/method/payments.saveStarGift for reference.
type PaymentsSaveStarGiftRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, hides the gift from our profile.
	Unsave bool
	// The gift to display or remove.
	Stargift InputSavedStarGiftClass
}

// PaymentsSaveStarGiftRequestTypeID is TL type id of PaymentsSaveStarGiftRequest.
const PaymentsSaveStarGiftRequestTypeID = 0x2a2a697c

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

func (s *PaymentsSaveStarGiftRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Unsave == false) {
		return false
	}
	if !(s.Stargift == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsSaveStarGiftRequest from given interface.
func (s *PaymentsSaveStarGiftRequest) FillFrom(from interface {
	GetUnsave() (value bool)
	GetStargift() (value InputSavedStarGiftClass)
}) {
	s.Unsave = from.GetUnsave()
	s.Stargift = from.GetStargift()
}

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

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

// TypeInfo returns info about TL type.
func (s *PaymentsSaveStarGiftRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.saveStarGift",
		ID:   PaymentsSaveStarGiftRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Unsave",
			SchemaName: "unsave",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Stargift",
			SchemaName: "stargift",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *PaymentsSaveStarGiftRequest) SetFlags() {
	if !(s.Unsave == false) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *PaymentsSaveStarGiftRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.saveStarGift#2a2a697c as nil")
	}
	b.PutID(PaymentsSaveStarGiftRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsSaveStarGiftRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.saveStarGift#2a2a697c as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.saveStarGift#2a2a697c: field flags: %w", err)
	}
	if s.Stargift == nil {
		return fmt.Errorf("unable to encode payments.saveStarGift#2a2a697c: field stargift is nil")
	}
	if err := s.Stargift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.saveStarGift#2a2a697c: field stargift: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsSaveStarGiftRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.saveStarGift#2a2a697c to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.saveStarGift#2a2a697c: field flags: %w", err)
		}
	}
	s.Unsave = s.Flags.Has(0)
	{
		value, err := DecodeInputSavedStarGift(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.saveStarGift#2a2a697c: field stargift: %w", err)
		}
		s.Stargift = value
	}
	return nil
}

// SetUnsave sets value of Unsave conditional field.
func (s *PaymentsSaveStarGiftRequest) SetUnsave(value bool) {
	if value {
		s.Flags.Set(0)
		s.Unsave = true
	} else {
		s.Flags.Unset(0)
		s.Unsave = false
	}
}

// GetUnsave returns value of Unsave conditional field.
func (s *PaymentsSaveStarGiftRequest) GetUnsave() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// GetStargift returns value of Stargift field.
func (s *PaymentsSaveStarGiftRequest) GetStargift() (value InputSavedStarGiftClass) {
	if s == nil {
		return
	}
	return s.Stargift
}

// PaymentsSaveStarGift invokes method payments.saveStarGift#2a2a697c returning error if any.
// Display or remove a received gift »¹ from our profile.
//
// Links:
//  1. https://core.telegram.org/api/gifts
//
// Possible errors:
//
//	400 MESSAGE_ID_INVALID: The provided message id is invalid.
//	400 SAVED_ID_EMPTY: The passed inputSavedStarGiftChat.saved_id is empty.
//	400 STARGIFT_OWNER_INVALID: You cannot transfer or sell a gift owned by another user.
//	400 USER_ID_INVALID: The provided user ID is invalid.
//
// See https://core.telegram.org/method/payments.saveStarGift for reference.
func (c *Client) PaymentsSaveStarGift(ctx context.Context, request *PaymentsSaveStarGiftRequest) (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
}
