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

// PremiumGiveawayPaymentOptions represents TL type `premiumGiveawayPaymentOptions#3c57c30c`.
type PremiumGiveawayPaymentOptions struct {
	// The list of options
	Options []PremiumGiveawayPaymentOption
}

// PremiumGiveawayPaymentOptionsTypeID is TL type id of PremiumGiveawayPaymentOptions.
const PremiumGiveawayPaymentOptionsTypeID = 0x3c57c30c

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

func (p *PremiumGiveawayPaymentOptions) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Options == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PremiumGiveawayPaymentOptions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "premiumGiveawayPaymentOptions",
		ID:   PremiumGiveawayPaymentOptionsTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Options",
			SchemaName: "options",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PremiumGiveawayPaymentOptions) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiveawayPaymentOptions#3c57c30c as nil")
	}
	b.PutID(PremiumGiveawayPaymentOptionsTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumGiveawayPaymentOptions) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiveawayPaymentOptions#3c57c30c as nil")
	}
	b.PutInt(len(p.Options))
	for idx, v := range p.Options {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare premiumGiveawayPaymentOptions#3c57c30c: field options element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PremiumGiveawayPaymentOptions) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumGiveawayPaymentOptions#3c57c30c to nil")
	}
	if err := b.ConsumeID(PremiumGiveawayPaymentOptionsTypeID); err != nil {
		return fmt.Errorf("unable to decode premiumGiveawayPaymentOptions#3c57c30c: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PremiumGiveawayPaymentOptions) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumGiveawayPaymentOptions#3c57c30c to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode premiumGiveawayPaymentOptions#3c57c30c: field options: %w", err)
		}

		if headerLen > 0 {
			p.Options = make([]PremiumGiveawayPaymentOption, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value PremiumGiveawayPaymentOption
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare premiumGiveawayPaymentOptions#3c57c30c: field options: %w", err)
			}
			p.Options = append(p.Options, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PremiumGiveawayPaymentOptions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiveawayPaymentOptions#3c57c30c as nil")
	}
	b.ObjStart()
	b.PutID("premiumGiveawayPaymentOptions")
	b.Comma()
	b.FieldStart("options")
	b.ArrStart()
	for idx, v := range p.Options {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode premiumGiveawayPaymentOptions#3c57c30c: field options element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PremiumGiveawayPaymentOptions) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumGiveawayPaymentOptions#3c57c30c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("premiumGiveawayPaymentOptions"); err != nil {
				return fmt.Errorf("unable to decode premiumGiveawayPaymentOptions#3c57c30c: %w", err)
			}
		case "options":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value PremiumGiveawayPaymentOption
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode premiumGiveawayPaymentOptions#3c57c30c: field options: %w", err)
				}
				p.Options = append(p.Options, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode premiumGiveawayPaymentOptions#3c57c30c: field options: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetOptions returns value of Options field.
func (p *PremiumGiveawayPaymentOptions) GetOptions() (value []PremiumGiveawayPaymentOption) {
	if p == nil {
		return
	}
	return p.Options
}
