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

namespace MyTelegram.Schema;

///<summary>
/// <a href="https://corefork.telegram.org/api/forum#forum-topics">Forum topic</a> information was edited.
/// See <a href="https://corefork.telegram.org/constructor/messageActionTopicEdit" />
///</summary>
[TlObject(0xc0944820)]
public sealed class TMessageActionTopicEdit : IMessageAction
{
    public uint ConstructorId => 0xc0944820;
    ///<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>
    /// New topic title.
    ///</summary>
    public string? Title { get; set; }

    ///<summary>
    /// ID of the new <a href="https://corefork.telegram.org/api/custom-emoji">custom emoji</a> used as topic icon, or if it was removed.
    ///</summary>
    public long? IconEmojiId { get; set; }

    ///<summary>
    /// Whether the topic was opened or closed.
    /// See <a href="https://corefork.telegram.org/type/Bool" />
    ///</summary>
    public bool? Closed { get; set; }

    ///<summary>
    /// Whether the topic was hidden or unhidden (only valid for the "General" topic, <code>id=1</code>).
    /// See <a href="https://corefork.telegram.org/type/Bool" />
    ///</summary>
    public bool? Hidden { get; set; }

    public void ComputeFlag()
    {
        if (Title != null) { Flags = Flags.SetBit(0); }
        if (/*IconEmojiId != 0 &&*/ IconEmojiId.HasValue) { Flags = Flags.SetBit(1); }
        if (Closed !=null) { Flags = Flags.SetBit(2); }
        if (Hidden !=null) { Flags = Flags.SetBit(3); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(Title); }
        if (Flags.IsBitSet(1)) { writer.Write(IconEmojiId.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(Closed.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(Hidden.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Title = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { IconEmojiId = buffer.ReadInt64(); }
        if (Flags.IsBitSet(2)) { Closed = buffer.Read(); }
        if (Flags.IsBitSet(3)) { Hidden = buffer.Read(); }
    }
}