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

namespace MyTelegram.Schema;

///<summary>
/// Represents a message <a href="https://corefork.telegram.org/api/drafts">draft</a>.
/// See <a href="https://corefork.telegram.org/constructor/draftMessage" />
///</summary>
[TlObject(0x96eaa5eb)]
public sealed class TDraftMessage : IDraftMessage
{
    public uint ConstructorId => 0x96eaa5eb;
    ///<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 no webpage preview will be generated
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool NoWebpage { get; set; }

    ///<summary>
    /// If set, any eventual webpage preview will be shown on top of the message instead of at the bottom.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool InvertMedia { get; set; }

    ///<summary>
    /// If set, indicates that the message should be sent in reply to the specified message or story.
    /// See <a href="https://corefork.telegram.org/type/InputReplyTo" />
    ///</summary>
    public MyTelegram.Schema.IInputReplyTo? ReplyTo { get; set; }

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

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

    ///<summary>
    /// Media.
    /// See <a href="https://corefork.telegram.org/type/InputMedia" />
    ///</summary>
    public MyTelegram.Schema.IInputMedia? Media { get; set; }

    ///<summary>
    /// Date of last update of the draft.
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// A <a href="https://corefork.telegram.org/api/effects">message effect that should be played as specified here »</a>.
    ///</summary>
    public long? Effect { get; set; }
    public MyTelegram.Schema.ISuggestedPost? SuggestedPost { get; set; }

    public void ComputeFlag()
    {
        if (NoWebpage) { Flags = Flags.SetBit(1); }
        if (InvertMedia) { Flags = Flags.SetBit(6); }
        if (ReplyTo != null) { Flags = Flags.SetBit(4); }
        if (Entities?.Count > 0) { Flags = Flags.SetBit(3); }
        if (Media != null) { Flags = Flags.SetBit(5); }
        if (/*Effect != 0 &&*/ Effect.HasValue) { Flags = Flags.SetBit(7); }
        if (SuggestedPost != null) { Flags = Flags.SetBit(8); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(4)) { writer.Write(ReplyTo); }
        writer.Write(Message);
        if (Flags.IsBitSet(3)) { writer.Write(Entities); }
        if (Flags.IsBitSet(5)) { writer.Write(Media); }
        writer.Write(Date);
        if (Flags.IsBitSet(7)) { writer.Write(Effect.Value); }
        if (Flags.IsBitSet(8)) { writer.Write(SuggestedPost); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { NoWebpage = true; }
        if (Flags.IsBitSet(6)) { InvertMedia = true; }
        if (Flags.IsBitSet(4)) { ReplyTo = buffer.Read<MyTelegram.Schema.IInputReplyTo>(); }
        Message = buffer.ReadString();
        if (Flags.IsBitSet(3)) { Entities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
        if (Flags.IsBitSet(5)) { Media = buffer.Read<MyTelegram.Schema.IInputMedia>(); }
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(7)) { Effect = buffer.ReadInt64(); }
        if (Flags.IsBitSet(8)) { SuggestedPost = buffer.Read<MyTelegram.Schema.ISuggestedPost>(); }
    }
}