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

// GiftForResale represents TL type `giftForResale#43698bb3`.
type GiftForResale struct {
	// The gift
	Gift UpgradedGift
	// Unique identifier of the received gift for the current user; only for the gifts owned
	// by the current user
	ReceivedGiftID string
}

// GiftForResaleTypeID is TL type id of GiftForResale.
const GiftForResaleTypeID = 0x43698bb3

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

func (g *GiftForResale) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Gift.Zero()) {
		return false
	}
	if !(g.ReceivedGiftID == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GiftForResale) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "giftForResale",
		ID:   GiftForResaleTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Gift",
			SchemaName: "gift",
		},
		{
			Name:       "ReceivedGiftID",
			SchemaName: "received_gift_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GiftForResale) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giftForResale#43698bb3 as nil")
	}
	b.PutID(GiftForResaleTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GiftForResale) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giftForResale#43698bb3 as nil")
	}
	if err := g.Gift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode giftForResale#43698bb3: field gift: %w", err)
	}
	b.PutString(g.ReceivedGiftID)
	return nil
}

// Decode implements bin.Decoder.
func (g *GiftForResale) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giftForResale#43698bb3 to nil")
	}
	if err := b.ConsumeID(GiftForResaleTypeID); err != nil {
		return fmt.Errorf("unable to decode giftForResale#43698bb3: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GiftForResale) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giftForResale#43698bb3 to nil")
	}
	{
		if err := g.Gift.Decode(b); err != nil {
			return fmt.Errorf("unable to decode giftForResale#43698bb3: field gift: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode giftForResale#43698bb3: field received_gift_id: %w", err)
		}
		g.ReceivedGiftID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GiftForResale) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode giftForResale#43698bb3 as nil")
	}
	b.ObjStart()
	b.PutID("giftForResale")
	b.Comma()
	b.FieldStart("gift")
	if err := g.Gift.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode giftForResale#43698bb3: field gift: %w", err)
	}
	b.Comma()
	b.FieldStart("received_gift_id")
	b.PutString(g.ReceivedGiftID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GiftForResale) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode giftForResale#43698bb3 to nil")
	}

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

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

// GetReceivedGiftID returns value of ReceivedGiftID field.
func (g *GiftForResale) GetReceivedGiftID() (value string) {
	if g == nil {
		return
	}
	return g.ReceivedGiftID
}
