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

// ReceivedGift represents TL type `receivedGift#3ab2ed8`.
type ReceivedGift struct {
	// Unique identifier of the received gift for the current user; only for the receiver of
	// the gift
	ReceivedGiftID string
	// Identifier of a user or a chat that sent the gift; may be null if unknown
	SenderID MessageSenderClass
	// Message added to the gift
	Text FormattedText
	// True, if the sender and gift text are shown only to the gift receiver; otherwise,
	// everyone are able to see them
	IsPrivate bool
	// True, if the gift is displayed on the chat's profile page; only for the receiver of
	// the gift
	IsSaved bool
	// True, if the gift is pinned to the top of the chat's profile page
	IsPinned bool
	// True, if the gift is a regular gift that can be upgraded to a unique gift; only for
	// the receiver of the gift
	CanBeUpgraded bool
	// True, if the gift is an upgraded gift that can be transferred to another owner; only
	// for the receiver of the gift
	CanBeTransferred bool
	// True, if the gift was refunded and isn't available anymore
	WasRefunded bool
	// Point in time (Unix timestamp) when the gift was sent
	Date int32
	// The gift
	Gift SentGiftClass
	// Number of Telegram Stars that can be claimed by the receiver instead of the regular
	// gift; 0 if the gift can't be sold by the current user
	SellStarCount int64
	// Number of Telegram Stars that were paid by the sender for the ability to upgrade the
	// gift
	PrepaidUpgradeStarCount int64
	// Number of Telegram Stars that must be paid to transfer the upgraded gift; only for the
	// receiver of the gift
	TransferStarCount int64
	// Point in time (Unix timestamp) when the gift can be transferred to another owner; 0 if
	// the gift can be transferred immediately or transfer isn't possible; only for the
	// receiver of the gift
	NextTransferDate int32
	// Point in time (Unix timestamp) when the gift can be resold to another user; 0 if the
	// gift can't be resold; only for the receiver of the gift
	NextResaleDate int32
	// Point in time (Unix timestamp) when the upgraded gift can be transferred to the TON
	// blockchain as an NFT; 0 if NFT export isn't possible; only for the receiver of the
	// gift
	ExportDate int32
}

// ReceivedGiftTypeID is TL type id of ReceivedGift.
const ReceivedGiftTypeID = 0x3ab2ed8

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

func (r *ReceivedGift) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.ReceivedGiftID == "") {
		return false
	}
	if !(r.SenderID == nil) {
		return false
	}
	if !(r.Text.Zero()) {
		return false
	}
	if !(r.IsPrivate == false) {
		return false
	}
	if !(r.IsSaved == false) {
		return false
	}
	if !(r.IsPinned == false) {
		return false
	}
	if !(r.CanBeUpgraded == false) {
		return false
	}
	if !(r.CanBeTransferred == false) {
		return false
	}
	if !(r.WasRefunded == false) {
		return false
	}
	if !(r.Date == 0) {
		return false
	}
	if !(r.Gift == nil) {
		return false
	}
	if !(r.SellStarCount == 0) {
		return false
	}
	if !(r.PrepaidUpgradeStarCount == 0) {
		return false
	}
	if !(r.TransferStarCount == 0) {
		return false
	}
	if !(r.NextTransferDate == 0) {
		return false
	}
	if !(r.NextResaleDate == 0) {
		return false
	}
	if !(r.ExportDate == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *ReceivedGift) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "receivedGift",
		ID:   ReceivedGiftTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ReceivedGiftID",
			SchemaName: "received_gift_id",
		},
		{
			Name:       "SenderID",
			SchemaName: "sender_id",
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "IsPrivate",
			SchemaName: "is_private",
		},
		{
			Name:       "IsSaved",
			SchemaName: "is_saved",
		},
		{
			Name:       "IsPinned",
			SchemaName: "is_pinned",
		},
		{
			Name:       "CanBeUpgraded",
			SchemaName: "can_be_upgraded",
		},
		{
			Name:       "CanBeTransferred",
			SchemaName: "can_be_transferred",
		},
		{
			Name:       "WasRefunded",
			SchemaName: "was_refunded",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Gift",
			SchemaName: "gift",
		},
		{
			Name:       "SellStarCount",
			SchemaName: "sell_star_count",
		},
		{
			Name:       "PrepaidUpgradeStarCount",
			SchemaName: "prepaid_upgrade_star_count",
		},
		{
			Name:       "TransferStarCount",
			SchemaName: "transfer_star_count",
		},
		{
			Name:       "NextTransferDate",
			SchemaName: "next_transfer_date",
		},
		{
			Name:       "NextResaleDate",
			SchemaName: "next_resale_date",
		},
		{
			Name:       "ExportDate",
			SchemaName: "export_date",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ReceivedGift) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode receivedGift#3ab2ed8 as nil")
	}
	b.PutID(ReceivedGiftTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ReceivedGift) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode receivedGift#3ab2ed8 as nil")
	}
	b.PutString(r.ReceivedGiftID)
	if r.SenderID == nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field sender_id is nil")
	}
	if err := r.SenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field sender_id: %w", err)
	}
	if err := r.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field text: %w", err)
	}
	b.PutBool(r.IsPrivate)
	b.PutBool(r.IsSaved)
	b.PutBool(r.IsPinned)
	b.PutBool(r.CanBeUpgraded)
	b.PutBool(r.CanBeTransferred)
	b.PutBool(r.WasRefunded)
	b.PutInt32(r.Date)
	if r.Gift == nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field gift is nil")
	}
	if err := r.Gift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field gift: %w", err)
	}
	b.PutInt53(r.SellStarCount)
	b.PutInt53(r.PrepaidUpgradeStarCount)
	b.PutInt53(r.TransferStarCount)
	b.PutInt32(r.NextTransferDate)
	b.PutInt32(r.NextResaleDate)
	b.PutInt32(r.ExportDate)
	return nil
}

// Decode implements bin.Decoder.
func (r *ReceivedGift) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode receivedGift#3ab2ed8 to nil")
	}
	if err := b.ConsumeID(ReceivedGiftTypeID); err != nil {
		return fmt.Errorf("unable to decode receivedGift#3ab2ed8: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ReceivedGift) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode receivedGift#3ab2ed8 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field received_gift_id: %w", err)
		}
		r.ReceivedGiftID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field sender_id: %w", err)
		}
		r.SenderID = value
	}
	{
		if err := r.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field text: %w", err)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field is_private: %w", err)
		}
		r.IsPrivate = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field is_saved: %w", err)
		}
		r.IsSaved = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field is_pinned: %w", err)
		}
		r.IsPinned = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field can_be_upgraded: %w", err)
		}
		r.CanBeUpgraded = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field can_be_transferred: %w", err)
		}
		r.CanBeTransferred = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field was_refunded: %w", err)
		}
		r.WasRefunded = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field date: %w", err)
		}
		r.Date = value
	}
	{
		value, err := DecodeSentGift(b)
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field gift: %w", err)
		}
		r.Gift = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field sell_star_count: %w", err)
		}
		r.SellStarCount = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field prepaid_upgrade_star_count: %w", err)
		}
		r.PrepaidUpgradeStarCount = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field transfer_star_count: %w", err)
		}
		r.TransferStarCount = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field next_transfer_date: %w", err)
		}
		r.NextTransferDate = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field next_resale_date: %w", err)
		}
		r.NextResaleDate = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field export_date: %w", err)
		}
		r.ExportDate = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *ReceivedGift) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode receivedGift#3ab2ed8 as nil")
	}
	b.ObjStart()
	b.PutID("receivedGift")
	b.Comma()
	b.FieldStart("received_gift_id")
	b.PutString(r.ReceivedGiftID)
	b.Comma()
	b.FieldStart("sender_id")
	if r.SenderID == nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field sender_id is nil")
	}
	if err := r.SenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field sender_id: %w", err)
	}
	b.Comma()
	b.FieldStart("text")
	if err := r.Text.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field text: %w", err)
	}
	b.Comma()
	b.FieldStart("is_private")
	b.PutBool(r.IsPrivate)
	b.Comma()
	b.FieldStart("is_saved")
	b.PutBool(r.IsSaved)
	b.Comma()
	b.FieldStart("is_pinned")
	b.PutBool(r.IsPinned)
	b.Comma()
	b.FieldStart("can_be_upgraded")
	b.PutBool(r.CanBeUpgraded)
	b.Comma()
	b.FieldStart("can_be_transferred")
	b.PutBool(r.CanBeTransferred)
	b.Comma()
	b.FieldStart("was_refunded")
	b.PutBool(r.WasRefunded)
	b.Comma()
	b.FieldStart("date")
	b.PutInt32(r.Date)
	b.Comma()
	b.FieldStart("gift")
	if r.Gift == nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field gift is nil")
	}
	if err := r.Gift.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode receivedGift#3ab2ed8: field gift: %w", err)
	}
	b.Comma()
	b.FieldStart("sell_star_count")
	b.PutInt53(r.SellStarCount)
	b.Comma()
	b.FieldStart("prepaid_upgrade_star_count")
	b.PutInt53(r.PrepaidUpgradeStarCount)
	b.Comma()
	b.FieldStart("transfer_star_count")
	b.PutInt53(r.TransferStarCount)
	b.Comma()
	b.FieldStart("next_transfer_date")
	b.PutInt32(r.NextTransferDate)
	b.Comma()
	b.FieldStart("next_resale_date")
	b.PutInt32(r.NextResaleDate)
	b.Comma()
	b.FieldStart("export_date")
	b.PutInt32(r.ExportDate)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *ReceivedGift) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode receivedGift#3ab2ed8 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("receivedGift"); err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: %w", err)
			}
		case "received_gift_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field received_gift_id: %w", err)
			}
			r.ReceivedGiftID = value
		case "sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field sender_id: %w", err)
			}
			r.SenderID = value
		case "text":
			if err := r.Text.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field text: %w", err)
			}
		case "is_private":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field is_private: %w", err)
			}
			r.IsPrivate = value
		case "is_saved":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field is_saved: %w", err)
			}
			r.IsSaved = value
		case "is_pinned":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field is_pinned: %w", err)
			}
			r.IsPinned = value
		case "can_be_upgraded":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field can_be_upgraded: %w", err)
			}
			r.CanBeUpgraded = value
		case "can_be_transferred":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field can_be_transferred: %w", err)
			}
			r.CanBeTransferred = value
		case "was_refunded":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field was_refunded: %w", err)
			}
			r.WasRefunded = value
		case "date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field date: %w", err)
			}
			r.Date = value
		case "gift":
			value, err := DecodeTDLibJSONSentGift(b)
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field gift: %w", err)
			}
			r.Gift = value
		case "sell_star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field sell_star_count: %w", err)
			}
			r.SellStarCount = value
		case "prepaid_upgrade_star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field prepaid_upgrade_star_count: %w", err)
			}
			r.PrepaidUpgradeStarCount = value
		case "transfer_star_count":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field transfer_star_count: %w", err)
			}
			r.TransferStarCount = value
		case "next_transfer_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field next_transfer_date: %w", err)
			}
			r.NextTransferDate = value
		case "next_resale_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field next_resale_date: %w", err)
			}
			r.NextResaleDate = value
		case "export_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGift#3ab2ed8: field export_date: %w", err)
			}
			r.ExportDate = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetSenderID returns value of SenderID field.
func (r *ReceivedGift) GetSenderID() (value MessageSenderClass) {
	if r == nil {
		return
	}
	return r.SenderID
}

// GetText returns value of Text field.
func (r *ReceivedGift) GetText() (value FormattedText) {
	if r == nil {
		return
	}
	return r.Text
}

// GetIsPrivate returns value of IsPrivate field.
func (r *ReceivedGift) GetIsPrivate() (value bool) {
	if r == nil {
		return
	}
	return r.IsPrivate
}

// GetIsSaved returns value of IsSaved field.
func (r *ReceivedGift) GetIsSaved() (value bool) {
	if r == nil {
		return
	}
	return r.IsSaved
}

// GetIsPinned returns value of IsPinned field.
func (r *ReceivedGift) GetIsPinned() (value bool) {
	if r == nil {
		return
	}
	return r.IsPinned
}

// GetCanBeUpgraded returns value of CanBeUpgraded field.
func (r *ReceivedGift) GetCanBeUpgraded() (value bool) {
	if r == nil {
		return
	}
	return r.CanBeUpgraded
}

// GetCanBeTransferred returns value of CanBeTransferred field.
func (r *ReceivedGift) GetCanBeTransferred() (value bool) {
	if r == nil {
		return
	}
	return r.CanBeTransferred
}

// GetWasRefunded returns value of WasRefunded field.
func (r *ReceivedGift) GetWasRefunded() (value bool) {
	if r == nil {
		return
	}
	return r.WasRefunded
}

// GetDate returns value of Date field.
func (r *ReceivedGift) GetDate() (value int32) {
	if r == nil {
		return
	}
	return r.Date
}

// GetGift returns value of Gift field.
func (r *ReceivedGift) GetGift() (value SentGiftClass) {
	if r == nil {
		return
	}
	return r.Gift
}

// GetSellStarCount returns value of SellStarCount field.
func (r *ReceivedGift) GetSellStarCount() (value int64) {
	if r == nil {
		return
	}
	return r.SellStarCount
}

// GetPrepaidUpgradeStarCount returns value of PrepaidUpgradeStarCount field.
func (r *ReceivedGift) GetPrepaidUpgradeStarCount() (value int64) {
	if r == nil {
		return
	}
	return r.PrepaidUpgradeStarCount
}

// GetTransferStarCount returns value of TransferStarCount field.
func (r *ReceivedGift) GetTransferStarCount() (value int64) {
	if r == nil {
		return
	}
	return r.TransferStarCount
}

// GetNextTransferDate returns value of NextTransferDate field.
func (r *ReceivedGift) GetNextTransferDate() (value int32) {
	if r == nil {
		return
	}
	return r.NextTransferDate
}

// GetNextResaleDate returns value of NextResaleDate field.
func (r *ReceivedGift) GetNextResaleDate() (value int32) {
	if r == nil {
		return
	}
	return r.NextResaleDate
}

// GetExportDate returns value of ExportDate field.
func (r *ReceivedGift) GetExportDate() (value int32) {
	if r == nil {
		return
	}
	return r.ExportDate
}
