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

namespace MyTelegram.Schema;

///<summary>
/// Chat
/// See <a href="https://corefork.telegram.org/constructor/dialog" />
///</summary>
[TlObject(0xd58a08c6)]
public sealed class TDialog : ILayeredDialog
{
    public uint ConstructorId => 0xd58a08c6;
    ///<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>
    /// Is the dialog pinned
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Pinned { get; set; }

    ///<summary>
    /// Whether the chat was manually marked as unread
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool UnreadMark { get; set; }

    ///<summary>
    /// Users may also choose to display messages from all topics of a <a href="https://corefork.telegram.org/api/forum">forum</a> as if they were sent to a normal group, using a "View as messages" setting in the local client.  <br>This setting only affects the current account, and is synced to other logged in sessions using the <a href="https://corefork.telegram.org/method/channels.toggleViewForumAsMessages">channels.toggleViewForumAsMessages</a> method; invoking this method will update the value of this flag.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ViewForumAsMessages { get; set; }

    ///<summary>
    /// The chat
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer Peer { get; set; }

    ///<summary>
    /// The latest message ID
    ///</summary>
    public int TopMessage { get; set; }

    ///<summary>
    /// Position up to which all incoming messages are read.
    ///</summary>
    public int ReadInboxMaxId { get; set; }

    ///<summary>
    /// Position up to which all outgoing messages are read.
    ///</summary>
    public int ReadOutboxMaxId { get; set; }

    ///<summary>
    /// Number of unread messages
    ///</summary>
    public int UnreadCount { get; set; }

    ///<summary>
    /// Number of <a href="https://corefork.telegram.org/api/mentions">unread mentions</a>
    ///</summary>
    public int UnreadMentionsCount { get; set; }

    ///<summary>
    /// Number of unread reactions to messages you sent
    ///</summary>
    public int UnreadReactionsCount { get; set; }

    ///<summary>
    /// Notification settings
    /// See <a href="https://corefork.telegram.org/type/PeerNotifySettings" />
    ///</summary>
    public MyTelegram.Schema.IPeerNotifySettings NotifySettings { get; set; }

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

    ///<summary>
    /// Message <a href="https://corefork.telegram.org/api/drafts">draft</a>
    /// See <a href="https://corefork.telegram.org/type/DraftMessage" />
    ///</summary>
    public MyTelegram.Schema.IDraftMessage? Draft { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/folders#peer-folders">Peer folder ID, for more info click here</a>
    ///</summary>
    public int? FolderId { get; set; }

    ///<summary>
    /// Time-to-live of all messages sent in this dialog
    ///</summary>
    public int? TtlPeriod { get; set; }

    public void ComputeFlag()
    {
        if (Pinned) { Flags = Flags.SetBit(2); }
        if (UnreadMark) { Flags = Flags.SetBit(3); }
        if (ViewForumAsMessages) { Flags = Flags.SetBit(6); }
        if (/*Pts != 0 && */Pts.HasValue) { Flags = Flags.SetBit(0); }
        if (Draft != null) { Flags = Flags.SetBit(1); }
        if (/*FolderId != 0 && */FolderId.HasValue) { Flags = Flags.SetBit(4); }
        if (/*TtlPeriod != 0 && */TtlPeriod.HasValue) { Flags = Flags.SetBit(5); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Peer);
        writer.Write(TopMessage);
        writer.Write(ReadInboxMaxId);
        writer.Write(ReadOutboxMaxId);
        writer.Write(UnreadCount);
        writer.Write(UnreadMentionsCount);
        writer.Write(UnreadReactionsCount);
        writer.Write(NotifySettings);
        if (Flags.IsBitSet(0)) { writer.Write(Pts.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(Draft); }
        if (Flags.IsBitSet(4)) { writer.Write(FolderId.Value); }
        if (Flags.IsBitSet(5)) { writer.Write(TtlPeriod.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { Pinned = true; }
        if (Flags.IsBitSet(3)) { UnreadMark = true; }
        if (Flags.IsBitSet(6)) { ViewForumAsMessages = true; }
        Peer = buffer.Read<MyTelegram.Schema.IPeer>();
        TopMessage = buffer.ReadInt32();
        ReadInboxMaxId = buffer.ReadInt32();
        ReadOutboxMaxId = buffer.ReadInt32();
        UnreadCount = buffer.ReadInt32();
        UnreadMentionsCount = buffer.ReadInt32();
        UnreadReactionsCount = buffer.ReadInt32();
        NotifySettings = buffer.Read<MyTelegram.Schema.IPeerNotifySettings>();
        if (Flags.IsBitSet(0)) { Pts = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { Draft = buffer.Read<MyTelegram.Schema.IDraftMessage>(); }
        if (Flags.IsBitSet(4)) { FolderId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(5)) { TtlPeriod = buffer.ReadInt32(); }
    }
}