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

namespace MyTelegram.Schema.Help;

///<summary>
/// An update is available for the application.
/// See <a href="https://corefork.telegram.org/constructor/help.appUpdate" />
///</summary>
[TlObject(0xccbbce30)]
public sealed class TAppUpdate : IAppUpdate
{
    public uint ConstructorId => 0xccbbce30;
    ///<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>
    /// Unskippable, the new info must be shown to the user (with a popup or something else)
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CanNotSkip { get; set; }

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

    ///<summary>
    /// New version name
    ///</summary>
    public string Version { get; set; }

    ///<summary>
    /// Text description of the update
    ///</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>
    /// Application binary
    /// See <a href="https://corefork.telegram.org/type/Document" />
    ///</summary>
    public MyTelegram.Schema.IDocument? Document { get; set; }

    ///<summary>
    /// Application download URL
    ///</summary>
    public string? Url { get; set; }

    ///<summary>
    /// Associated sticker
    /// See <a href="https://corefork.telegram.org/type/Document" />
    ///</summary>
    public MyTelegram.Schema.IDocument? Sticker { get; set; }

    public void ComputeFlag()
    {
        if (CanNotSkip) { Flags = Flags.SetBit(0); }
        if (Document != null) { Flags = Flags.SetBit(1); }
        if (Url != null) { Flags = Flags.SetBit(2); }
        if (Sticker != null) { Flags = Flags.SetBit(3); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Version);
        writer.Write(Text);
        writer.Write(Entities);
        if (Flags.IsBitSet(1)) { writer.Write(Document); }
        if (Flags.IsBitSet(2)) { writer.Write(Url); }
        if (Flags.IsBitSet(3)) { writer.Write(Sticker); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { CanNotSkip = true; }
        Id = buffer.ReadInt32();
        Version = buffer.ReadString();
        Text = buffer.ReadString();
        Entities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>();
        if (Flags.IsBitSet(1)) { Document = buffer.Read<MyTelegram.Schema.IDocument>(); }
        if (Flags.IsBitSet(2)) { Url = buffer.ReadString(); }
        if (Flags.IsBitSet(3)) { Sticker = buffer.Read<MyTelegram.Schema.IDocument>(); }
    }
}