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

namespace MyTelegram.Schema;

///<summary>
/// Folder
/// See <a href="https://corefork.telegram.org/constructor/folder" />
///</summary>
[TlObject(0xff544e65)]
public sealed class TFolder : IFolder
{
    public uint ConstructorId => 0xff544e65;
    ///<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>
    /// Automatically add new channels to this folder
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AutofillNewBroadcasts { get; set; }

    ///<summary>
    /// Automatically add joined new public supergroups to this folder
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AutofillPublicGroups { get; set; }

    ///<summary>
    /// Automatically add new private chats to this folder
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AutofillNewCorrespondents { get; set; }

    ///<summary>
    /// Folder ID
    ///</summary>
    public int Id { get; set; }

    ///<summary>
    /// Folder title
    ///</summary>
    public string Title { get; set; }

    ///<summary>
    /// Folder picture
    /// See <a href="https://corefork.telegram.org/type/ChatPhoto" />
    ///</summary>
    public MyTelegram.Schema.IChatPhoto? Photo { get; set; }

    public void ComputeFlag()
    {
        if (AutofillNewBroadcasts) { Flags = Flags.SetBit(0); }
        if (AutofillPublicGroups) { Flags = Flags.SetBit(1); }
        if (AutofillNewCorrespondents) { Flags = Flags.SetBit(2); }
        if (Photo != null) { Flags = Flags.SetBit(3); }
    }

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { AutofillNewBroadcasts = true; }
        if (Flags.IsBitSet(1)) { AutofillPublicGroups = true; }
        if (Flags.IsBitSet(2)) { AutofillNewCorrespondents = true; }
        Id = buffer.ReadInt32();
        Title = buffer.ReadString();
        if (Flags.IsBitSet(3)) { Photo = buffer.Read<MyTelegram.Schema.IChatPhoto>(); }
    }
}