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

// GiftPremiumWithStarsRequest represents TL type `giftPremiumWithStars#854f60d8`.
type GiftPremiumWithStarsRequest struct {
	// Identifier of the user which will receive Telegram Premium
	UserID int64
	// The number of Telegram Stars to pay for subscription
	StarCount int64
	// Number of months the Telegram Premium subscription will be active for the user
	MonthCount int32
	// Text to show to the user receiving Telegram Premium;
	// 0-getOption("gift_text_length_max") characters. Only Bold, Italic, Underline,
	// Strikethrough, Spoiler, and CustomEmoji entities are allowed
	Text FormattedText
}

// GiftPremiumWithStarsRequestTypeID is TL type id of GiftPremiumWithStarsRequest.
const GiftPremiumWithStarsRequestTypeID = 0x854f60d8

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

func (g *GiftPremiumWithStarsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.UserID == 0) {
		return false
	}
	if !(g.StarCount == 0) {
		return false
	}
	if !(g.MonthCount == 0) {
		return false
	}
	if !(g.Text.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GiftPremiumWithStarsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "giftPremiumWithStars",
		ID:   GiftPremiumWithStarsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "StarCount",
			SchemaName: "star_count",
		},
		{
			Name:       "MonthCount",
			SchemaName: "month_count",
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GiftPremiumWithStarsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giftPremiumWithStars#854f60d8 as nil")
	}
	b.PutID(GiftPremiumWithStarsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GiftPremiumWithStarsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giftPremiumWithStars#854f60d8 as nil")
	}
	b.PutInt53(g.UserID)
	b.PutInt53(g.StarCount)
	b.PutInt32(g.MonthCount)
	if err := g.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode giftPremiumWithStars#854f60d8: field text: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GiftPremiumWithStarsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giftPremiumWithStars#854f60d8 to nil")
	}
	if err := b.ConsumeID(GiftPremiumWithStarsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GiftPremiumWithStarsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giftPremiumWithStars#854f60d8 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: field user_id: %w", err)
		}
		g.UserID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: field star_count: %w", err)
		}
		g.StarCount = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: field month_count: %w", err)
		}
		g.MonthCount = value
	}
	{
		if err := g.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: field text: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GiftPremiumWithStarsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode giftPremiumWithStars#854f60d8 as nil")
	}
	b.ObjStart()
	b.PutID("giftPremiumWithStars")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(g.UserID)
	b.Comma()
	b.FieldStart("star_count")
	b.PutInt53(g.StarCount)
	b.Comma()
	b.FieldStart("month_count")
	b.PutInt32(g.MonthCount)
	b.Comma()
	b.FieldStart("text")
	if err := g.Text.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode giftPremiumWithStars#854f60d8: field text: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GiftPremiumWithStarsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode giftPremiumWithStars#854f60d8 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("giftPremiumWithStars"); err != nil {
				return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: field user_id: %w", err)
			}
			g.UserID = value
		case "star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: field star_count: %w", err)
			}
			g.StarCount = value
		case "month_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: field month_count: %w", err)
			}
			g.MonthCount = value
		case "text":
			if err := g.Text.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode giftPremiumWithStars#854f60d8: field text: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (g *GiftPremiumWithStarsRequest) GetUserID() (value int64) {
	if g == nil {
		return
	}
	return g.UserID
}

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

// GetMonthCount returns value of MonthCount field.
func (g *GiftPremiumWithStarsRequest) GetMonthCount() (value int32) {
	if g == nil {
		return
	}
	return g.MonthCount
}

// GetText returns value of Text field.
func (g *GiftPremiumWithStarsRequest) GetText() (value FormattedText) {
	if g == nil {
		return
	}
	return g.Text
}

// GiftPremiumWithStars invokes method giftPremiumWithStars#854f60d8 returning error if any.
func (c *Client) GiftPremiumWithStars(ctx context.Context, request *GiftPremiumWithStarsRequest) error {
	var ok Ok

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