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

// UpgradedGiftOriginalDetails represents TL type `upgradedGiftOriginalDetails#34b0370`.
type UpgradedGiftOriginalDetails struct {
	// Identifier of the user or the chat that sent the gift; may be null if the gift was
	// private
	SenderID MessageSenderClass
	// Identifier of the user or the chat that received the gift
	ReceiverID MessageSenderClass
	// Message added to the gift
	Text FormattedText
	// Point in time (Unix timestamp) when the gift was sent
	Date int32
}

// UpgradedGiftOriginalDetailsTypeID is TL type id of UpgradedGiftOriginalDetails.
const UpgradedGiftOriginalDetailsTypeID = 0x34b0370

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

func (u *UpgradedGiftOriginalDetails) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.SenderID == nil) {
		return false
	}
	if !(u.ReceiverID == nil) {
		return false
	}
	if !(u.Text.Zero()) {
		return false
	}
	if !(u.Date == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (u *UpgradedGiftOriginalDetails) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "upgradedGiftOriginalDetails",
		ID:   UpgradedGiftOriginalDetailsTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SenderID",
			SchemaName: "sender_id",
		},
		{
			Name:       "ReceiverID",
			SchemaName: "receiver_id",
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UpgradedGiftOriginalDetails) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftOriginalDetails#34b0370 as nil")
	}
	b.PutID(UpgradedGiftOriginalDetailsTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UpgradedGiftOriginalDetails) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftOriginalDetails#34b0370 as nil")
	}
	if u.SenderID == nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field sender_id is nil")
	}
	if err := u.SenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field sender_id: %w", err)
	}
	if u.ReceiverID == nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field receiver_id is nil")
	}
	if err := u.ReceiverID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field receiver_id: %w", err)
	}
	if err := u.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field text: %w", err)
	}
	b.PutInt32(u.Date)
	return nil
}

// Decode implements bin.Decoder.
func (u *UpgradedGiftOriginalDetails) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftOriginalDetails#34b0370 to nil")
	}
	if err := b.ConsumeID(UpgradedGiftOriginalDetailsTypeID); err != nil {
		return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *UpgradedGiftOriginalDetails) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftOriginalDetails#34b0370 to nil")
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: field sender_id: %w", err)
		}
		u.SenderID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: field receiver_id: %w", err)
		}
		u.ReceiverID = value
	}
	{
		if err := u.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: field text: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: field date: %w", err)
		}
		u.Date = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *UpgradedGiftOriginalDetails) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode upgradedGiftOriginalDetails#34b0370 as nil")
	}
	b.ObjStart()
	b.PutID("upgradedGiftOriginalDetails")
	b.Comma()
	b.FieldStart("sender_id")
	if u.SenderID == nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field sender_id is nil")
	}
	if err := u.SenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field sender_id: %w", err)
	}
	b.Comma()
	b.FieldStart("receiver_id")
	if u.ReceiverID == nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field receiver_id is nil")
	}
	if err := u.ReceiverID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field receiver_id: %w", err)
	}
	b.Comma()
	b.FieldStart("text")
	if err := u.Text.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode upgradedGiftOriginalDetails#34b0370: field text: %w", err)
	}
	b.Comma()
	b.FieldStart("date")
	b.PutInt32(u.Date)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (u *UpgradedGiftOriginalDetails) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode upgradedGiftOriginalDetails#34b0370 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("upgradedGiftOriginalDetails"); err != nil {
				return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: %w", err)
			}
		case "sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: field sender_id: %w", err)
			}
			u.SenderID = value
		case "receiver_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: field receiver_id: %w", err)
			}
			u.ReceiverID = value
		case "text":
			if err := u.Text.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: field text: %w", err)
			}
		case "date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode upgradedGiftOriginalDetails#34b0370: field date: %w", err)
			}
			u.Date = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetReceiverID returns value of ReceiverID field.
func (u *UpgradedGiftOriginalDetails) GetReceiverID() (value MessageSenderClass) {
	if u == nil {
		return
	}
	return u.ReceiverID
}

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

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