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

namespace MyTelegram.Schema;

///<summary>
/// Initialize connection
/// <para>Possible errors</para>
/// Code Type Description
/// 400 CONNECTION_LAYER_INVALID Layer invalid.
/// See <a href="https://corefork.telegram.org/method/initConnection" />
///</summary>
[TlObject(0xc1cd5ea9)]
public sealed class RequestInitConnection : IRequest<IObject>, IHasSubQuery
{
    public uint ConstructorId => 0xc1cd5ea9;
    ///<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>
    /// Application identifier (see. <a href="https://corefork.telegram.org/myapp">App configuration</a>)
    ///</summary>
    public int ApiId { get; set; }

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

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

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

    ///<summary>
    /// Code for the language used on the device's OS, ISO 639-1 standard
    ///</summary>
    public string SystemLangCode { get; set; }

    ///<summary>
    /// Language pack to use
    ///</summary>
    public string LangPack { get; set; }

    ///<summary>
    /// Code for the language used on the client, ISO 639-1 standard
    ///</summary>
    public string LangCode { get; set; }

    ///<summary>
    /// Info about an MTProto proxy
    /// See <a href="https://corefork.telegram.org/type/InputClientProxy" />
    ///</summary>
    public MyTelegram.Schema.IInputClientProxy? Proxy { get; set; }

    ///<summary>
    /// Additional initConnection parameters. <br>For now, only the <code>tz_offset</code> field is supported, for specifying timezone offset in seconds.
    /// See <a href="https://corefork.telegram.org/type/JSONValue" />
    ///</summary>
    public MyTelegram.Schema.IJSONValue? Params { get; set; }

    ///<summary>
    /// The query itself
    ///</summary>
    public IObject Query { get; set; }

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

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(ApiId);
        writer.Write(DeviceModel);
        writer.Write(SystemVersion);
        writer.Write(AppVersion);
        writer.Write(SystemLangCode);
        writer.Write(LangPack);
        writer.Write(LangCode);
        if (Flags.IsBitSet(0)) { writer.Write(Proxy); }
        if (Flags.IsBitSet(1)) { writer.Write(Params); }
        writer.Write(Query);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        ApiId = buffer.ReadInt32();
        DeviceModel = buffer.ReadString();
        SystemVersion = buffer.ReadString();
        AppVersion = buffer.ReadString();
        SystemLangCode = buffer.ReadString();
        LangPack = buffer.ReadString();
        LangCode = buffer.ReadString();
        if (Flags.IsBitSet(0)) { Proxy = buffer.Read<MyTelegram.Schema.IInputClientProxy>(); }
        if (Flags.IsBitSet(1)) { Params = buffer.Read<MyTelegram.Schema.IJSONValue>(); }
        Query = buffer.Read<IObject>();
    }
}
