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

namespace MyTelegram.Schema;

///<summary>
/// A new session logged into the current user's account through an unknown device.
/// See <a href="https://corefork.telegram.org/constructor/updateNewAuthorization" />
///</summary>
[TlObject(0x8951abef)]
public sealed class TUpdateNewAuthorization : IUpdate
{
    public uint ConstructorId => 0x8951abef;
    ///<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 the session is <a href="https://corefork.telegram.org/api/auth#confirming-login">unconfirmed, see here »</a> for more info.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Unconfirmed { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/offsets#hash-generation">Hash used for caching, for more info click here</a>
    ///</summary>
    public long Hash { get; set; }

    ///<summary>
    /// Authorization date
    ///</summary>
    public int? Date { get; set; }

    ///<summary>
    /// Name of device, for example <em>Android</em>
    ///</summary>
    public string? Device { get; set; }

    ///<summary>
    /// Location, for example <em>USA, NY (IP=1.2.3.4)</em>
    ///</summary>
    public string? Location { get; set; }

    public void ComputeFlag()
    {
        if (Unconfirmed) { Flags = Flags.SetBit(0); }
        if (/*Date != 0 && */Date.HasValue) { Flags = Flags.SetBit(0); }
        if (Device != null) { Flags = Flags.SetBit(0); }
        if (Location != null) { Flags = Flags.SetBit(0); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Hash);
        if (Flags.IsBitSet(0)) { writer.Write(Date.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(Device); }
        if (Flags.IsBitSet(0)) { writer.Write(Location); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Unconfirmed = true; }
        Hash = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { Date = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { Device = buffer.ReadString(); }
        if (Flags.IsBitSet(0)) { Location = buffer.ReadString(); }
    }
}