// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// UpgradeGiftRequest represents TL type `upgradeGift#95c6c2d9`.
type UpgradeGiftRequest struct {
	// Unique identifier of business connection on behalf of which to send the request; for
	// bots only
	BusinessConnectionID string
	// Identifier of the gift
	ReceivedGiftID string
	// Pass true to keep the original gift text, sender and receiver in the upgraded gift
	KeepOriginalDetails bool
	// The amount of Telegram Stars required to pay for the upgrade. It the gift has
	// prepaid_upgrade_star_count > 0, then pass 0, otherwise, pass gift.upgrade_star_count
	StarCount int64
}

// UpgradeGiftRequestTypeID is TL type id of UpgradeGiftRequest.
const UpgradeGiftRequestTypeID = 0x95c6c2d9

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

func (u *UpgradeGiftRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.BusinessConnectionID == "") {
		return false
	}
	if !(u.ReceivedGiftID == "") {
		return false
	}
	if !(u.KeepOriginalDetails == false) {
		return false
	}
	if !(u.StarCount == 0) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*UpgradeGiftRequest) TypeName() string {
	return "upgradeGift"
}

// TypeInfo returns info about TL type.
func (u *UpgradeGiftRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "upgradeGift",
		ID:   UpgradeGiftRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "ReceivedGiftID",
			SchemaName: "received_gift_id",
		},
		{
			Name:       "KeepOriginalDetails",
			SchemaName: "keep_original_details",
		},
		{
			Name:       "StarCount",
			SchemaName: "star_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UpgradeGiftRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradeGift#95c6c2d9 as nil")
	}
	b.PutID(UpgradeGiftRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UpgradeGiftRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradeGift#95c6c2d9 as nil")
	}
	b.PutString(u.BusinessConnectionID)
	b.PutString(u.ReceivedGiftID)
	b.PutBool(u.KeepOriginalDetails)
	b.PutInt53(u.StarCount)
	return nil
}

// Decode implements bin.Decoder.
func (u *UpgradeGiftRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradeGift#95c6c2d9 to nil")
	}
	if err := b.ConsumeID(UpgradeGiftRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *UpgradeGiftRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradeGift#95c6c2d9 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: field business_connection_id: %w", err)
		}
		u.BusinessConnectionID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: field received_gift_id: %w", err)
		}
		u.ReceivedGiftID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: field keep_original_details: %w", err)
		}
		u.KeepOriginalDetails = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: field star_count: %w", err)
		}
		u.StarCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UpgradeGiftRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradeGift#95c6c2d9 as nil")
	}
	b.ObjStart()
	b.PutID("upgradeGift")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(u.BusinessConnectionID)
	b.Comma()
	b.FieldStart("received_gift_id")
	b.PutString(u.ReceivedGiftID)
	b.Comma()
	b.FieldStart("keep_original_details")
	b.PutBool(u.KeepOriginalDetails)
	b.Comma()
	b.FieldStart("star_count")
	b.PutInt53(u.StarCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (u *UpgradeGiftRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradeGift#95c6c2d9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("upgradeGift"); err != nil {
				return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: field business_connection_id: %w", err)
			}
			u.BusinessConnectionID = value
		case "received_gift_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: field received_gift_id: %w", err)
			}
			u.ReceivedGiftID = value
		case "keep_original_details":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: field keep_original_details: %w", err)
			}
			u.KeepOriginalDetails = value
		case "star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode upgradeGift#95c6c2d9: field star_count: %w", err)
			}
			u.StarCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBusinessConnectionID returns value of BusinessConnectionID field.
func (u *UpgradeGiftRequest) GetBusinessConnectionID() (value string) {
	if u == nil {
		return
	}
	return u.BusinessConnectionID
}

// GetReceivedGiftID returns value of ReceivedGiftID field.
func (u *UpgradeGiftRequest) GetReceivedGiftID() (value string) {
	if u == nil {
		return
	}
	return u.ReceivedGiftID
}

// GetKeepOriginalDetails returns value of KeepOriginalDetails field.
func (u *UpgradeGiftRequest) GetKeepOriginalDetails() (value bool) {
	if u == nil {
		return
	}
	return u.KeepOriginalDetails
}

// GetStarCount returns value of StarCount field.
func (u *UpgradeGiftRequest) GetStarCount() (value int64) {
	if u == nil {
		return
	}
	return u.StarCount
}

// UpgradeGift invokes method upgradeGift#95c6c2d9 returning error if any.
func (c *Client) UpgradeGift(ctx context.Context, request *UpgradeGiftRequest) (*UpgradeGiftResult, error) {
	var result UpgradeGiftResult

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