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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Contains information about a chat export file <a href="https://corefork.telegram.org/api/import">generated by a foreign chat app, click here for more info</a>.<br>
/// If neither the <code>pm</code> or <code>group</code> flags are set, the specified chat export was generated from a chat of unknown type.
/// See <a href="https://corefork.telegram.org/constructor/messages.historyImportParsed" />
///</summary>
[TlObject(0x5e0fb7b9)]
public sealed class THistoryImportParsed : IHistoryImportParsed
{
    public uint ConstructorId => 0x5e0fb7b9;
    ///<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>
    /// The chat export file was generated from a private chat.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Pm { get; set; }

    ///<summary>
    /// The chat export file was generated from a group chat.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Group { get; set; }

    ///<summary>
    /// Title of the chat.
    ///</summary>
    public string? Title { get; set; }

    public void ComputeFlag()
    {
        if (Pm) { Flags = Flags.SetBit(0); }
        if (Group) { Flags = Flags.SetBit(1); }
        if (Title != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(2)) { writer.Write(Title); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Pm = true; }
        if (Flags.IsBitSet(1)) { Group = true; }
        if (Flags.IsBitSet(2)) { Title = buffer.ReadString(); }
    }
}