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

namespace MyTelegram.Schema;

///<summary>
/// Logged-in session
/// See <a href="https://corefork.telegram.org/constructor/authorization" />
///</summary>
[TlObject(0xad01d61d)]
public sealed class TAuthorization : IAuthorization
{
    public uint ConstructorId => 0xad01d61d;
    ///<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 this is the current session
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Current { get; set; }

    ///<summary>
    /// Whether the session is from an official app
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool OfficialApp { get; set; }

    ///<summary>
    /// Whether the session is still waiting for a 2FA password
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool PasswordPending { get; set; }

    ///<summary>
    /// Whether this session will accept encrypted chats
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool EncryptedRequestsDisabled { get; set; }

    ///<summary>
    /// Whether this session will accept phone calls
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CallRequestsDisabled { 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>
    /// Identifier
    ///</summary>
    public long Hash { get; set; }

    ///<summary>
    /// Device model
    ///</summary>
    public string DeviceModel { get; set; }

    ///<summary>
    /// Platform
    ///</summary>
    public string Platform { get; set; }

    ///<summary>
    /// System version
    ///</summary>
    public string SystemVersion { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/obtaining_api_id">API ID</a>
    ///</summary>
    public int ApiId { get; set; }

    ///<summary>
    /// App name
    ///</summary>
    public string AppName { get; set; }

    ///<summary>
    /// App version
    ///</summary>
    public string AppVersion { get; set; }

    ///<summary>
    /// When was the session created
    ///</summary>
    public int DateCreated { get; set; }

    ///<summary>
    /// When was the session last active
    ///</summary>
    public int DateActive { get; set; }

    ///<summary>
    /// Last known IP
    ///</summary>
    public string Ip { get; set; }

    ///<summary>
    /// Country determined from IP
    ///</summary>
    public string Country { get; set; }

    ///<summary>
    /// Region determined from IP
    ///</summary>
    public string Region { get; set; }

    public void ComputeFlag()
    {
        if (Current) { Flags = Flags.SetBit(0); }
        if (OfficialApp) { Flags = Flags.SetBit(1); }
        if (PasswordPending) { Flags = Flags.SetBit(2); }
        if (EncryptedRequestsDisabled) { Flags = Flags.SetBit(3); }
        if (CallRequestsDisabled) { Flags = Flags.SetBit(4); }
        if (Unconfirmed) { Flags = Flags.SetBit(5); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Hash);
        writer.Write(DeviceModel);
        writer.Write(Platform);
        writer.Write(SystemVersion);
        writer.Write(ApiId);
        writer.Write(AppName);
        writer.Write(AppVersion);
        writer.Write(DateCreated);
        writer.Write(DateActive);
        writer.Write(Ip);
        writer.Write(Country);
        writer.Write(Region);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Current = true; }
        if (Flags.IsBitSet(1)) { OfficialApp = true; }
        if (Flags.IsBitSet(2)) { PasswordPending = true; }
        if (Flags.IsBitSet(3)) { EncryptedRequestsDisabled = true; }
        if (Flags.IsBitSet(4)) { CallRequestsDisabled = true; }
        if (Flags.IsBitSet(5)) { Unconfirmed = true; }
        Hash = buffer.ReadInt64();
        DeviceModel = buffer.ReadString();
        Platform = buffer.ReadString();
        SystemVersion = buffer.ReadString();
        ApiId = buffer.ReadInt32();
        AppName = buffer.ReadString();
        AppVersion = buffer.ReadString();
        DateCreated = buffer.ReadInt32();
        DateActive = buffer.ReadInt32();
        Ip = buffer.ReadString();
        Country = buffer.ReadString();
        Region = buffer.ReadString();
    }
}