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

// LaunchPrepaidGiveawayRequest represents TL type `launchPrepaidGiveaway#261d783a`.
type LaunchPrepaidGiveawayRequest struct {
	// Unique identifier of the prepaid giveaway
	GiveawayID int64
	// Giveaway parameters
	Parameters GiveawayParameters
	// The number of users to receive giveaway prize
	WinnerCount int32
	// The number of Telegram Stars to be distributed through the giveaway; pass 0 for
	// Telegram Premium giveaways
	StarCount int64
}

// LaunchPrepaidGiveawayRequestTypeID is TL type id of LaunchPrepaidGiveawayRequest.
const LaunchPrepaidGiveawayRequestTypeID = 0x261d783a

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

func (l *LaunchPrepaidGiveawayRequest) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.GiveawayID == 0) {
		return false
	}
	if !(l.Parameters.Zero()) {
		return false
	}
	if !(l.WinnerCount == 0) {
		return false
	}
	if !(l.StarCount == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LaunchPrepaidGiveawayRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "launchPrepaidGiveaway",
		ID:   LaunchPrepaidGiveawayRequestTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GiveawayID",
			SchemaName: "giveaway_id",
		},
		{
			Name:       "Parameters",
			SchemaName: "parameters",
		},
		{
			Name:       "WinnerCount",
			SchemaName: "winner_count",
		},
		{
			Name:       "StarCount",
			SchemaName: "star_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LaunchPrepaidGiveawayRequest) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode launchPrepaidGiveaway#261d783a as nil")
	}
	b.PutID(LaunchPrepaidGiveawayRequestTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LaunchPrepaidGiveawayRequest) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode launchPrepaidGiveaway#261d783a as nil")
	}
	b.PutLong(l.GiveawayID)
	if err := l.Parameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode launchPrepaidGiveaway#261d783a: field parameters: %w", err)
	}
	b.PutInt32(l.WinnerCount)
	b.PutInt53(l.StarCount)
	return nil
}

// Decode implements bin.Decoder.
func (l *LaunchPrepaidGiveawayRequest) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode launchPrepaidGiveaway#261d783a to nil")
	}
	if err := b.ConsumeID(LaunchPrepaidGiveawayRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LaunchPrepaidGiveawayRequest) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode launchPrepaidGiveaway#261d783a to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: field giveaway_id: %w", err)
		}
		l.GiveawayID = value
	}
	{
		if err := l.Parameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: field parameters: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: field winner_count: %w", err)
		}
		l.WinnerCount = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: field star_count: %w", err)
		}
		l.StarCount = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LaunchPrepaidGiveawayRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode launchPrepaidGiveaway#261d783a as nil")
	}
	b.ObjStart()
	b.PutID("launchPrepaidGiveaway")
	b.Comma()
	b.FieldStart("giveaway_id")
	b.PutLong(l.GiveawayID)
	b.Comma()
	b.FieldStart("parameters")
	if err := l.Parameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode launchPrepaidGiveaway#261d783a: field parameters: %w", err)
	}
	b.Comma()
	b.FieldStart("winner_count")
	b.PutInt32(l.WinnerCount)
	b.Comma()
	b.FieldStart("star_count")
	b.PutInt53(l.StarCount)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LaunchPrepaidGiveawayRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode launchPrepaidGiveaway#261d783a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("launchPrepaidGiveaway"); err != nil {
				return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: %w", err)
			}
		case "giveaway_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: field giveaway_id: %w", err)
			}
			l.GiveawayID = value
		case "parameters":
			if err := l.Parameters.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: field parameters: %w", err)
			}
		case "winner_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: field winner_count: %w", err)
			}
			l.WinnerCount = value
		case "star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode launchPrepaidGiveaway#261d783a: field star_count: %w", err)
			}
			l.StarCount = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetGiveawayID returns value of GiveawayID field.
func (l *LaunchPrepaidGiveawayRequest) GetGiveawayID() (value int64) {
	if l == nil {
		return
	}
	return l.GiveawayID
}

// GetParameters returns value of Parameters field.
func (l *LaunchPrepaidGiveawayRequest) GetParameters() (value GiveawayParameters) {
	if l == nil {
		return
	}
	return l.Parameters
}

// GetWinnerCount returns value of WinnerCount field.
func (l *LaunchPrepaidGiveawayRequest) GetWinnerCount() (value int32) {
	if l == nil {
		return
	}
	return l.WinnerCount
}

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

// LaunchPrepaidGiveaway invokes method launchPrepaidGiveaway#261d783a returning error if any.
func (c *Client) LaunchPrepaidGiveaway(ctx context.Context, request *LaunchPrepaidGiveawayRequest) error {
	var ok Ok

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