// <auto-generated/>
// ReSharper disable All

namespace MyTelegram.Schema;

///<summary>
/// Info about a forwarded message
/// See <a href="https://corefork.telegram.org/constructor/messageFwdHeader" />
///</summary>
[TlObject(0x4e4df4bb)]
public sealed class TMessageFwdHeader : IMessageFwdHeader
{
    public uint ConstructorId => 0x4e4df4bb;
    ///<summary>
    /// Flags, see <a href="https://corefork.telegram.org/mtproto/TL-combinators#conditional-fields">TL conditional fields</a>
    ///</summary>
    public int Flags { get; set; }

    ///<summary>
    /// Whether this message was <a href="https://corefork.telegram.org/api/import">imported from a foreign chat service, click here for more info »</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Imported { get; set; }

    ///<summary>
    /// Only for messages forwarded to <a href="https://corefork.telegram.org/api/saved-messages">saved messages »</a>, set if the original message was outgoing (though the message may have been originally outgoing even if this flag is not set, if <code>from_id</code> points to the current user).
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool SavedOut { get; set; }

    ///<summary>
    /// The ID of the user that originally sent the message
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer? FromId { get; set; }

    ///<summary>
    /// The name of the user that originally sent the message
    ///</summary>
    public string? FromName { get; set; }

    ///<summary>
    /// When was the message originally sent
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// ID of the channel message that was forwarded
    ///</summary>
    public int? ChannelPost { get; set; }

    ///<summary>
    /// For channels and if signatures are enabled, author of the channel message
    ///</summary>
    public string? PostAuthor { get; set; }

    ///<summary>
    /// Only for messages forwarded to <a href="https://corefork.telegram.org/api/saved-messages">saved messages »</a>, contains the dialog where the message was originally sent.
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer? SavedFromPeer { get; set; }

    ///<summary>
    /// Only for messages forwarded to <a href="https://corefork.telegram.org/api/saved-messages">saved messages »</a>, contains the original ID of the message in <code>saved_from_peer</code>.
    ///</summary>
    public int? SavedFromMsgId { get; set; }

    ///<summary>
    /// Only for forwarded messages reforwarded to <a href="https://corefork.telegram.org/api/saved-messages">saved messages »</a>, contains the sender of the original message (i.e. if user A sends a message, then user B forwards it somewhere, then user C saves it to saved messages, this field will contain the ID of user B and <code>from_id</code> will contain the ID of user A).
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer? SavedFromId { get; set; }

    ///<summary>
    /// Only for forwarded messages from users with forward privacy enabled, sent by users with forward privacy enabled, reforwarded to <a href="https://corefork.telegram.org/api/saved-messages">saved messages »</a>, contains the sender of the original message (i.e. if user A (fwd privacy enabled) sends a message, then user B (fwd privacy enabled) forwards it somewhere, then user C saves it to saved messages, this field will contain the name of user B and <code>from_name</code> will contain the name of user A).
    ///</summary>
    public string? SavedFromName { get; set; }

    ///<summary>
    /// Only for forwarded messages reforwarded to <a href="https://corefork.telegram.org/api/saved-messages">saved messages »</a>, indicates when was the original message sent (i.e. if user A sends a message @ unixtime 1, then user B forwards it somewhere @ unixtime 2, then user C saves it to saved messages @ unixtime 3, this field will contain 2, <code>date</code> will contain 1 and the <code>date</code> of the containing <a href="https://corefork.telegram.org/constructor/message">message</a> will contain 3).
    ///</summary>
    public int? SavedDate { get; set; }

    ///<summary>
    /// PSA type
    ///</summary>
    public string? PsaType { get; set; }

    public void ComputeFlag()
    {
        if (Imported) { Flags = Flags.SetBit(7); }
        if (SavedOut) { Flags = Flags.SetBit(11); }
        if (FromId != null) { Flags = Flags.SetBit(0); }
        if (FromName != null) { Flags = Flags.SetBit(5); }
        if (/*ChannelPost != 0 && */ChannelPost.HasValue) { Flags = Flags.SetBit(2); }
        if (PostAuthor != null) { Flags = Flags.SetBit(3); }
        if (SavedFromPeer != null && SavedFromMsgId.HasValue) { Flags = Flags.SetBit(4); }
        //if (/*SavedFromMsgId != 0 && */SavedFromMsgId.HasValue) { Flags = Flags.SetBit(4); }
        if (SavedFromId != null) { Flags = Flags.SetBit(8); }
        if (SavedFromName != null) { Flags = Flags.SetBit(9); }
        if (/*SavedDate != 0 && */SavedDate.HasValue) { Flags = Flags.SetBit(10); }
        if (PsaType != null) { Flags = Flags.SetBit(6); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(FromId); }
        if (Flags.IsBitSet(5)) { writer.Write(FromName); }
        writer.Write(Date);
        if (Flags.IsBitSet(2)) { writer.Write(ChannelPost.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(PostAuthor); }
        if (Flags.IsBitSet(4)) { writer.Write(SavedFromPeer); }
        if (Flags.IsBitSet(4)) { writer.Write(SavedFromMsgId.Value); }
        if (Flags.IsBitSet(8)) { writer.Write(SavedFromId); }
        if (Flags.IsBitSet(9)) { writer.Write(SavedFromName); }
        if (Flags.IsBitSet(10)) { writer.Write(SavedDate.Value); }
        if (Flags.IsBitSet(6)) { writer.Write(PsaType); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(7)) { Imported = true; }
        if (Flags.IsBitSet(11)) { SavedOut = true; }
        if (Flags.IsBitSet(0)) { FromId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(5)) { FromName = buffer.ReadString(); }
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { ChannelPost = buffer.ReadInt32(); }
        if (Flags.IsBitSet(3)) { PostAuthor = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { SavedFromPeer = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(4)) { SavedFromMsgId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(8)) { SavedFromId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(9)) { SavedFromName = buffer.ReadString(); }
        if (Flags.IsBitSet(10)) { SavedDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(6)) { PsaType = buffer.ReadString(); }
    }
}