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

namespace MyTelegram.Schema.E2e;


[TlObject(0x639a3db6)]
public sealed class TBlock : IBlock
{
    public uint ConstructorId => 0x639a3db6;
    public ReadOnlyMemory<byte> Signature { get; set; }
    public int Flags { get; set; }
    public ReadOnlyMemory<byte> PrevBlockHash { get; set; }
    public TVector<MyTelegram.Schema.E2e.IChange> Changes { get; set; }
    public int Height { get; set; }
    public MyTelegram.Schema.E2e.IStateProof StateProof { get; set; }
    public ReadOnlyMemory<byte>? SignaturePublicKey { get; set; }

    public void ComputeFlag()
    {
        if (SignaturePublicKey != null) { Flags = Flags.SetBit(0); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.WriteRawBytes(Signature);
        writer.Write(Flags);
        writer.WriteRawBytes(PrevBlockHash);
        writer.WriteVector(Changes);
        writer.Write(Height);
        writer.Write(StateProof);
        if (Flags.IsBitSet(0)) { writer.WriteRawBytes(SignaturePublicKey.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Signature = buffer.ReadInt512();
        Flags = buffer.ReadInt32();
        PrevBlockHash = buffer.ReadInt256();
        Changes = buffer.ReadVector<MyTelegram.Schema.E2e.IChange>();
        Height = buffer.ReadInt32();
        StateProof = buffer.Read<MyTelegram.Schema.E2e.IStateProof>();
        if (Flags.IsBitSet(0)) { SignaturePublicKey = buffer.ReadInt256(); }
    }
}