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

namespace MyTelegram.Schema;

///<summary>
/// Media autosave settings
/// See <a href="https://corefork.telegram.org/constructor/autoSaveSettings" />
///</summary>
[TlObject(0xc84834ce)]
public sealed class TAutoSaveSettings : IAutoSaveSettings
{
    public uint ConstructorId => 0xc84834ce;
    ///<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 photos should be autosaved to the gallery.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Photos { get; set; }

    ///<summary>
    /// Whether videos should be autosaved to the gallery.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Videos { get; set; }

    ///<summary>
    /// If set, specifies a size limit for autosavable videos
    ///</summary>
    public long? VideoMaxSize { get; set; }

    public void ComputeFlag()
    {
        if (Photos) { Flags = Flags.SetBit(0); }
        if (Videos) { Flags = Flags.SetBit(1); }
        if (/*VideoMaxSize != 0 &&*/ VideoMaxSize.HasValue) { Flags = Flags.SetBit(2); }
    }

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Photos = true; }
        if (Flags.IsBitSet(1)) { Videos = true; }
        if (Flags.IsBitSet(2)) { VideoMaxSize = buffer.ReadInt64(); }
    }
}