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

// ForwardSource represents TL type `forwardSource#6b02aec9`.
type ForwardSource struct {
	// Identifier of the chat to which the message that was forwarded belonged; may be 0 if
	// unknown
	ChatID int64
	// Identifier of the message; may be 0 if unknown
	MessageID int64
	// Identifier of the sender of the message; may be null if unknown or the new message was
	// forwarded not to Saved Messages
	SenderID MessageSenderClass
	// Name of the sender of the message if the sender is hidden by their privacy settings
	SenderName string
	// Point in time (Unix timestamp) when the message is sent; 0 if unknown
	Date int32
	// True, if the message that was forwarded is outgoing; always false if sender is unknown
	IsOutgoing bool
}

// ForwardSourceTypeID is TL type id of ForwardSource.
const ForwardSourceTypeID = 0x6b02aec9

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

func (f *ForwardSource) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.ChatID == 0) {
		return false
	}
	if !(f.MessageID == 0) {
		return false
	}
	if !(f.SenderID == nil) {
		return false
	}
	if !(f.SenderName == "") {
		return false
	}
	if !(f.Date == 0) {
		return false
	}
	if !(f.IsOutgoing == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *ForwardSource) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "forwardSource",
		ID:   ForwardSourceTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "SenderID",
			SchemaName: "sender_id",
		},
		{
			Name:       "SenderName",
			SchemaName: "sender_name",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "IsOutgoing",
			SchemaName: "is_outgoing",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *ForwardSource) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode forwardSource#6b02aec9 as nil")
	}
	b.PutID(ForwardSourceTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *ForwardSource) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode forwardSource#6b02aec9 as nil")
	}
	b.PutInt53(f.ChatID)
	b.PutInt53(f.MessageID)
	if f.SenderID == nil {
		return fmt.Errorf("unable to encode forwardSource#6b02aec9: field sender_id is nil")
	}
	if err := f.SenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode forwardSource#6b02aec9: field sender_id: %w", err)
	}
	b.PutString(f.SenderName)
	b.PutInt32(f.Date)
	b.PutBool(f.IsOutgoing)
	return nil
}

// Decode implements bin.Decoder.
func (f *ForwardSource) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode forwardSource#6b02aec9 to nil")
	}
	if err := b.ConsumeID(ForwardSourceTypeID); err != nil {
		return fmt.Errorf("unable to decode forwardSource#6b02aec9: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *ForwardSource) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode forwardSource#6b02aec9 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode forwardSource#6b02aec9: field chat_id: %w", err)
		}
		f.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode forwardSource#6b02aec9: field message_id: %w", err)
		}
		f.MessageID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode forwardSource#6b02aec9: field sender_id: %w", err)
		}
		f.SenderID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode forwardSource#6b02aec9: field sender_name: %w", err)
		}
		f.SenderName = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode forwardSource#6b02aec9: field date: %w", err)
		}
		f.Date = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode forwardSource#6b02aec9: field is_outgoing: %w", err)
		}
		f.IsOutgoing = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *ForwardSource) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode forwardSource#6b02aec9 as nil")
	}
	b.ObjStart()
	b.PutID("forwardSource")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(f.ChatID)
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(f.MessageID)
	b.Comma()
	b.FieldStart("sender_id")
	if f.SenderID == nil {
		return fmt.Errorf("unable to encode forwardSource#6b02aec9: field sender_id is nil")
	}
	if err := f.SenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode forwardSource#6b02aec9: field sender_id: %w", err)
	}
	b.Comma()
	b.FieldStart("sender_name")
	b.PutString(f.SenderName)
	b.Comma()
	b.FieldStart("date")
	b.PutInt32(f.Date)
	b.Comma()
	b.FieldStart("is_outgoing")
	b.PutBool(f.IsOutgoing)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *ForwardSource) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode forwardSource#6b02aec9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("forwardSource"); err != nil {
				return fmt.Errorf("unable to decode forwardSource#6b02aec9: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode forwardSource#6b02aec9: field chat_id: %w", err)
			}
			f.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode forwardSource#6b02aec9: field message_id: %w", err)
			}
			f.MessageID = value
		case "sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode forwardSource#6b02aec9: field sender_id: %w", err)
			}
			f.SenderID = value
		case "sender_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode forwardSource#6b02aec9: field sender_name: %w", err)
			}
			f.SenderName = value
		case "date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode forwardSource#6b02aec9: field date: %w", err)
			}
			f.Date = value
		case "is_outgoing":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode forwardSource#6b02aec9: field is_outgoing: %w", err)
			}
			f.IsOutgoing = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (f *ForwardSource) GetChatID() (value int64) {
	if f == nil {
		return
	}
	return f.ChatID
}

// GetMessageID returns value of MessageID field.
func (f *ForwardSource) GetMessageID() (value int64) {
	if f == nil {
		return
	}
	return f.MessageID
}

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

// GetSenderName returns value of SenderName field.
func (f *ForwardSource) GetSenderName() (value string) {
	if f == nil {
		return
	}
	return f.SenderName
}

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

// GetIsOutgoing returns value of IsOutgoing field.
func (f *ForwardSource) GetIsOutgoing() (value bool) {
	if f == nil {
		return
	}
	return f.IsOutgoing
}
