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

namespace MyTelegram.Schema;

///<summary>
/// Shortened constructor containing info on one outgoing message to a contact (the destination chat has to be extracted from the method call that returned this object).
/// See <a href="https://corefork.telegram.org/constructor/updateShortSentMessage" />
///</summary>
[TlObject(0x9015e101)]
public sealed class TUpdateShortSentMessage : IUpdates
{
    public uint ConstructorId => 0x9015e101;
    ///<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>
    /// ID of the sent message
    ///</summary>
    public int Id { 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>
    /// Attached media
    /// See <a href="https://corefork.telegram.org/type/MessageMedia" />
    ///</summary>
    public MyTelegram.Schema.IMessageMedia? Media { 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 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 (Media != null) { Flags = Flags.SetBit(9); }
        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(Pts);
        writer.Write(PtsCount);
        writer.Write(Date);
        if (Flags.IsBitSet(9)) { writer.Write(Media); }
        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; }
        Id = buffer.ReadInt32();
        Pts = buffer.ReadInt32();
        PtsCount = buffer.ReadInt32();
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(9)) { Media = buffer.Read<MyTelegram.Schema.IMessageMedia>(); }
        if (Flags.IsBitSet(7)) { Entities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
        if (Flags.IsBitSet(25)) { TtlPeriod = buffer.ReadInt32(); }
    }
}