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

// SentGiftRegular represents TL type `sentGiftRegular#2368ad19`.
type SentGiftRegular struct {
	// The gift
	Gift Gift
}

// SentGiftRegularTypeID is TL type id of SentGiftRegular.
const SentGiftRegularTypeID = 0x2368ad19

// construct implements constructor of SentGiftClass.
func (s SentGiftRegular) construct() SentGiftClass { return &s }

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

	_ SentGiftClass = &SentGiftRegular{}
)

func (s *SentGiftRegular) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Gift.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SentGiftRegular) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sentGiftRegular",
		ID:   SentGiftRegularTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Gift",
			SchemaName: "gift",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SentGiftRegular) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sentGiftRegular#2368ad19 as nil")
	}
	b.PutID(SentGiftRegularTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SentGiftRegular) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sentGiftRegular#2368ad19 as nil")
	}
	if err := s.Gift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sentGiftRegular#2368ad19: field gift: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SentGiftRegular) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sentGiftRegular#2368ad19 to nil")
	}
	if err := b.ConsumeID(SentGiftRegularTypeID); err != nil {
		return fmt.Errorf("unable to decode sentGiftRegular#2368ad19: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SentGiftRegular) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sentGiftRegular#2368ad19 to nil")
	}
	{
		if err := s.Gift.Decode(b); err != nil {
			return fmt.Errorf("unable to decode sentGiftRegular#2368ad19: field gift: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SentGiftRegular) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sentGiftRegular#2368ad19 as nil")
	}
	b.ObjStart()
	b.PutID("sentGiftRegular")
	b.Comma()
	b.FieldStart("gift")
	if err := s.Gift.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode sentGiftRegular#2368ad19: field gift: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SentGiftRegular) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode sentGiftRegular#2368ad19 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sentGiftRegular"); err != nil {
				return fmt.Errorf("unable to decode sentGiftRegular#2368ad19: %w", err)
			}
		case "gift":
			if err := s.Gift.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode sentGiftRegular#2368ad19: field gift: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetGift returns value of Gift field.
func (s *SentGiftRegular) GetGift() (value Gift) {
	if s == nil {
		return
	}
	return s.Gift
}

// SentGiftUpgraded represents TL type `sentGiftUpgraded#25674480`.
type SentGiftUpgraded struct {
	// The gift
	Gift UpgradedGift
}

// SentGiftUpgradedTypeID is TL type id of SentGiftUpgraded.
const SentGiftUpgradedTypeID = 0x25674480

// construct implements constructor of SentGiftClass.
func (s SentGiftUpgraded) construct() SentGiftClass { return &s }

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

	_ SentGiftClass = &SentGiftUpgraded{}
)

func (s *SentGiftUpgraded) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Gift.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SentGiftUpgraded) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sentGiftUpgraded",
		ID:   SentGiftUpgradedTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Gift",
			SchemaName: "gift",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SentGiftUpgraded) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sentGiftUpgraded#25674480 as nil")
	}
	b.PutID(SentGiftUpgradedTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SentGiftUpgraded) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sentGiftUpgraded#25674480 as nil")
	}
	if err := s.Gift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sentGiftUpgraded#25674480: field gift: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SentGiftUpgraded) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sentGiftUpgraded#25674480 to nil")
	}
	if err := b.ConsumeID(SentGiftUpgradedTypeID); err != nil {
		return fmt.Errorf("unable to decode sentGiftUpgraded#25674480: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SentGiftUpgraded) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sentGiftUpgraded#25674480 to nil")
	}
	{
		if err := s.Gift.Decode(b); err != nil {
			return fmt.Errorf("unable to decode sentGiftUpgraded#25674480: field gift: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SentGiftUpgraded) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sentGiftUpgraded#25674480 as nil")
	}
	b.ObjStart()
	b.PutID("sentGiftUpgraded")
	b.Comma()
	b.FieldStart("gift")
	if err := s.Gift.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode sentGiftUpgraded#25674480: field gift: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SentGiftUpgraded) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode sentGiftUpgraded#25674480 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sentGiftUpgraded"); err != nil {
				return fmt.Errorf("unable to decode sentGiftUpgraded#25674480: %w", err)
			}
		case "gift":
			if err := s.Gift.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode sentGiftUpgraded#25674480: field gift: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetGift returns value of Gift field.
func (s *SentGiftUpgraded) GetGift() (value UpgradedGift) {
	if s == nil {
		return
	}
	return s.Gift
}

// SentGiftClassName is schema name of SentGiftClass.
const SentGiftClassName = "SentGift"

// SentGiftClass represents SentGift generic type.
//
// Example:
//
//	g, err := tdapi.DecodeSentGift(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.SentGiftRegular: // sentGiftRegular#2368ad19
//	case *tdapi.SentGiftUpgraded: // sentGiftUpgraded#25674480
//	default: panic(v)
//	}
type SentGiftClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() SentGiftClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeSentGift implements binary de-serialization for SentGiftClass.
func DecodeSentGift(buf *bin.Buffer) (SentGiftClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case SentGiftRegularTypeID:
		// Decoding sentGiftRegular#2368ad19.
		v := SentGiftRegular{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SentGiftClass: %w", err)
		}
		return &v, nil
	case SentGiftUpgradedTypeID:
		// Decoding sentGiftUpgraded#25674480.
		v := SentGiftUpgraded{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SentGiftClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode SentGiftClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONSentGift implements binary de-serialization for SentGiftClass.
func DecodeTDLibJSONSentGift(buf tdjson.Decoder) (SentGiftClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "sentGiftRegular":
		// Decoding sentGiftRegular#2368ad19.
		v := SentGiftRegular{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SentGiftClass: %w", err)
		}
		return &v, nil
	case "sentGiftUpgraded":
		// Decoding sentGiftUpgraded#25674480.
		v := SentGiftUpgraded{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SentGiftClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode SentGiftClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// SentGift boxes the SentGiftClass providing a helper.
type SentGiftBox struct {
	SentGift SentGiftClass
}

// Decode implements bin.Decoder for SentGiftBox.
func (b *SentGiftBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode SentGiftBox to nil")
	}
	v, err := DecodeSentGift(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.SentGift = v
	return nil
}

// Encode implements bin.Encode for SentGiftBox.
func (b *SentGiftBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.SentGift == nil {
		return fmt.Errorf("unable to encode SentGiftClass as nil")
	}
	return b.SentGift.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for SentGiftBox.
func (b *SentGiftBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode SentGiftBox to nil")
	}
	v, err := DecodeTDLibJSONSentGift(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.SentGift = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for SentGiftBox.
func (b *SentGiftBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.SentGift == nil {
		return fmt.Errorf("unable to encode SentGiftClass as nil")
	}
	return b.SentGift.EncodeTDLibJSON(buf)
}
