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

namespace MyTelegram.Schema.Help;

///<summary>
/// Info about the latest telegram Terms Of Service
/// See <a href="https://corefork.telegram.org/constructor/help.termsOfService" />
///</summary>
[TlObject(0x780a0310)]
public sealed class TTermsOfService : ITermsOfService
{
    public uint ConstructorId => 0x780a0310;
    ///<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 a prompt must be showed to the user, in order to accept the new terms.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Popup { get; set; }

    ///<summary>
    /// ID of the new terms
    /// See <a href="https://corefork.telegram.org/type/DataJSON" />
    ///</summary>
    public MyTelegram.Schema.IDataJSON Id { get; set; }

    ///<summary>
    /// Text of the new terms
    ///</summary>
    public string Text { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/entities">Message entities for styled text</a>
    ///</summary>
    public TVector<MyTelegram.Schema.IMessageEntity> Entities { get; set; }

    ///<summary>
    /// Minimum age required to sign up to telegram, the user must confirm that they is older than the minimum age.
    ///</summary>
    public int? MinAgeConfirm { get; set; }

    public void ComputeFlag()
    {
        if (Popup) { Flags = Flags.SetBit(0); }
        if (/*MinAgeConfirm != 0 && */MinAgeConfirm.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Text);
        writer.Write(Entities);
        if (Flags.IsBitSet(1)) { writer.Write(MinAgeConfirm.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Popup = true; }
        Id = buffer.Read<MyTelegram.Schema.IDataJSON>();
        Text = buffer.ReadString();
        Entities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>();
        if (Flags.IsBitSet(1)) { MinAgeConfirm = buffer.ReadInt32(); }
    }
}