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

// PremiumGiftPaymentOptions represents TL type `premiumGiftPaymentOptions#a52b90f1`.
type PremiumGiftPaymentOptions struct {
	// The list of options sorted by Telegram Premium subscription duration
	Options []PremiumGiftPaymentOption
}

// PremiumGiftPaymentOptionsTypeID is TL type id of PremiumGiftPaymentOptions.
const PremiumGiftPaymentOptionsTypeID = 0xa52b90f1

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (p *PremiumGiftPaymentOptions) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiftPaymentOptions#a52b90f1 as nil")
	}
	b.PutID(PremiumGiftPaymentOptionsTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PremiumGiftPaymentOptions) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiftPaymentOptions#a52b90f1 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 premiumGiftPaymentOptions#a52b90f1: field options element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PremiumGiftPaymentOptions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode premiumGiftPaymentOptions#a52b90f1 as nil")
	}
	b.ObjStart()
	b.PutID("premiumGiftPaymentOptions")
	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 premiumGiftPaymentOptions#a52b90f1: 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 *PremiumGiftPaymentOptions) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode premiumGiftPaymentOptions#a52b90f1 to nil")
	}

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

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