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

namespace MyTelegram.Schema;

///<summary>
/// Shortened constructor containing info on one new incoming text message from a chat
/// See <a href="https://corefork.telegram.org/constructor/updateShortChatMessage" />
///</summary>
[TlObject(0x4d6deea5)]
public sealed class TUpdateShortChatMessage : IUpdates
{
    public uint ConstructorId => 0x4d6deea5;
    ///<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 the message is outgoing
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Out { get; set; }

    ///<summary>
    /// Whether we were mentioned in this message
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Mentioned { get; set; }

    ///<summary>
    /// Whether the message contains some <strong>unread</strong> mentions
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool MediaUnread { get; set; }

    ///<summary>
    /// If true, the message is a silent message, no notifications should be triggered
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Silent { get; set; }

    ///<summary>
    /// ID of the message
    ///</summary>
    public int Id { get; set; }

    ///<summary>
    /// ID of the sender of the message
    ///</summary>
    public long FromId { get; set; }

    ///<summary>
    /// ID of the chat where the message was sent
    ///</summary>
    public long ChatId { get; set; }

    ///<summary>
    /// Message
    ///</summary>
    public string Message { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/updates">PTS</a>
    ///</summary>
    public int Pts { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/updates">PTS count</a>
    ///</summary>
    public int PtsCount { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/updates">date</a>
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// Info about a forwarded message
    /// See <a href="https://corefork.telegram.org/type/MessageFwdHeader" />
    ///</summary>
    public MyTelegram.Schema.IMessageFwdHeader? FwdFrom { get; set; }

    ///<summary>
    /// Info about the inline bot used to generate this message
    ///</summary>
    public long? ViaBotId { get; set; }

    ///<summary>
    /// Reply (thread) information
    /// See <a href="https://corefork.telegram.org/type/MessageReplyHeader" />
    ///</summary>
    public MyTelegram.Schema.IMessageReplyHeader? ReplyTo { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/entities">Entities</a> for styled text
    ///</summary>
    public TVector<MyTelegram.Schema.IMessageEntity>? Entities { get; set; }

    ///<summary>
    /// Time To Live of the message, once updateShortChatMessage.date+updateShortChatMessage.ttl_period === time(), the message will be deleted on the server, and must be deleted locally as well.
    ///</summary>
    public int? TtlPeriod { get; set; }

    public void ComputeFlag()
    {
        if (Out) { Flags = Flags.SetBit(1); }
        if (Mentioned) { Flags = Flags.SetBit(4); }
        if (MediaUnread) { Flags = Flags.SetBit(5); }
        if (Silent) { Flags = Flags.SetBit(13); }
        if (FwdFrom != null) { Flags = Flags.SetBit(2); }
        if (/*ViaBotId != 0 &&*/ ViaBotId.HasValue) { Flags = Flags.SetBit(11); }
        if (ReplyTo != null) { Flags = Flags.SetBit(3); }
        if (Entities?.Count > 0) { Flags = Flags.SetBit(7); }
        if (/*TtlPeriod != 0 && */TtlPeriod.HasValue) { Flags = Flags.SetBit(25); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(FromId);
        writer.Write(ChatId);
        writer.Write(Message);
        writer.Write(Pts);
        writer.Write(PtsCount);
        writer.Write(Date);
        if (Flags.IsBitSet(2)) { writer.Write(FwdFrom); }
        if (Flags.IsBitSet(11)) { writer.Write(ViaBotId.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(ReplyTo); }
        if (Flags.IsBitSet(7)) { writer.Write(Entities); }
        if (Flags.IsBitSet(25)) { writer.Write(TtlPeriod.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { Out = true; }
        if (Flags.IsBitSet(4)) { Mentioned = true; }
        if (Flags.IsBitSet(5)) { MediaUnread = true; }
        if (Flags.IsBitSet(13)) { Silent = true; }
        Id = buffer.ReadInt32();
        FromId = buffer.ReadInt64();
        ChatId = buffer.ReadInt64();
        Message = buffer.ReadString();
        Pts = buffer.ReadInt32();
        PtsCount = buffer.ReadInt32();
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { FwdFrom = buffer.Read<MyTelegram.Schema.IMessageFwdHeader>(); }
        if (Flags.IsBitSet(11)) { ViaBotId = buffer.ReadInt64(); }
        if (Flags.IsBitSet(3)) { ReplyTo = buffer.Read<MyTelegram.Schema.IMessageReplyHeader>(); }
        if (Flags.IsBitSet(7)) { Entities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
        if (Flags.IsBitSet(25)) { TtlPeriod = buffer.ReadInt32(); }
    }
}