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

namespace MyTelegram.Schema;

///<summary>
/// Indicates a service message
/// See <a href="https://corefork.telegram.org/constructor/messageService" />
///</summary>
[TlObject(0x7a800e0a)]
public sealed class TMessageService : MyTelegram.Schema.IMessage, ILayeredServiceMessage
{
    public uint ConstructorId => 0x7a800e0a;
    ///<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 the message
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Mentioned { get; set; }

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

    ///<summary>
    /// Whether the message is silent
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Silent { get; set; }

    ///<summary>
    /// Whether it's a channel post
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Post { get; set; }

    ///<summary>
    /// This is a legacy message: it has to be refetched with the new layer
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Legacy { get; set; }

    ///<summary>
    /// Message ID
    ///</summary>
    public int Id { get; set; }

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

    ///<summary>
    /// Sender of service message
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer PeerId { get; set; }
    public MyTelegram.Schema.IPeer? SavedPeerId { get; set; }

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

    ///<summary>
    /// Message date
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// Event connected with the service message
    /// See <a href="https://corefork.telegram.org/type/MessageAction" />
    ///</summary>
    public MyTelegram.Schema.IMessageAction Action { get; set; }
    public MyTelegram.Schema.IMessageReactions? Reactions { get; set; }

    ///<summary>
    /// Time To Live of the message, once message.date+message.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 (ReactionsArePossible) { Flags = Flags.SetBit(9); }
        if (Silent) { Flags = Flags.SetBit(13); }
        if (Post) { Flags = Flags.SetBit(14); }
        if (Legacy) { Flags = Flags.SetBit(19); }
        if (FromId != null) { Flags = Flags.SetBit(8); }
        if (SavedPeerId != null) { Flags = Flags.SetBit(28); }
        if (ReplyTo != null) { Flags = Flags.SetBit(3); }
        if (Reactions != null) { Flags = Flags.SetBit(20); }
        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);
        if (Flags.IsBitSet(8)) { writer.Write(FromId); }
        writer.Write(PeerId);
        if (Flags.IsBitSet(28)) { writer.Write(SavedPeerId); }
        if (Flags.IsBitSet(3)) { writer.Write(ReplyTo); }
        writer.Write(Date);
        writer.Write(Action);
        if (Flags.IsBitSet(20)) { writer.Write(Reactions); }
        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(9)) { ReactionsArePossible = true; }
        if (Flags.IsBitSet(13)) { Silent = true; }
        if (Flags.IsBitSet(14)) { Post = true; }
        if (Flags.IsBitSet(19)) { Legacy = true; }
        Id = buffer.ReadInt32();
        if (Flags.IsBitSet(8)) { FromId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        PeerId = buffer.Read<MyTelegram.Schema.IPeer>();
        if (Flags.IsBitSet(28)) { SavedPeerId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(3)) { ReplyTo = buffer.Read<MyTelegram.Schema.IMessageReplyHeader>(); }
        Date = buffer.ReadInt32();
        Action = buffer.Read<MyTelegram.Schema.IMessageAction>();
        if (Flags.IsBitSet(20)) { Reactions = buffer.Read<MyTelegram.Schema.IMessageReactions>(); }
        if (Flags.IsBitSet(25)) { TtlPeriod = buffer.ReadInt32(); }
    }
}