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

namespace MyTelegram.Schema;

///<summary>
/// Data center
/// See <a href="https://corefork.telegram.org/constructor/dcOption" />
///</summary>
[TlObject(0x18b7a10d)]
public sealed class TDcOption : IDcOption
{
    public uint ConstructorId => 0x18b7a10d;
    ///<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 specified IP is an IPv6 address
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Ipv6 { get; set; }

    ///<summary>
    /// Whether this DC should only be used to <a href="https://corefork.telegram.org/api/files">download or upload files</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool MediaOnly { get; set; }

    ///<summary>
    /// Whether this DC only supports connection with <a href="https://corefork.telegram.org/mtproto/mtproto-transports#transport-obfuscation">transport obfuscation</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool TcpoOnly { get; set; }

    ///<summary>
    /// Whether this is a <a href="https://corefork.telegram.org/cdn">CDN DC</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Cdn { get; set; }

    ///<summary>
    /// If set, this IP should be used when connecting through a proxy
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Static { get; set; }

    ///<summary>
    /// If set, clients must connect using only the specified port, without trying any other port.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ThisPortOnly { get; set; }

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

    ///<summary>
    /// IP address of DC
    ///</summary>
    public string IpAddress { get; set; }

    ///<summary>
    /// Port
    ///</summary>
    public int Port { get; set; }

    ///<summary>
    /// If the <code>tcpo_only</code> flag is set, specifies the secret to use when connecting using <a href="https://corefork.telegram.org/mtproto/mtproto-transports#transport-obfuscation">transport obfuscation</a>
    ///</summary>
    public ReadOnlyMemory<byte>? Secret { get; set; }

    public void ComputeFlag()
    {
        if (Ipv6) { Flags = Flags.SetBit(0); }
        if (MediaOnly) { Flags = Flags.SetBit(1); }
        if (TcpoOnly) { Flags = Flags.SetBit(2); }
        if (Cdn) { Flags = Flags.SetBit(3); }
        if (Static) { Flags = Flags.SetBit(4); }
        if (ThisPortOnly) { Flags = Flags.SetBit(5); }
        if (Secret != null) { Flags = Flags.SetBit(10); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(IpAddress);
        writer.Write(Port);
        if (Flags.IsBitSet(10)) { writer.Write(Secret); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Ipv6 = true; }
        if (Flags.IsBitSet(1)) { MediaOnly = true; }
        if (Flags.IsBitSet(2)) { TcpoOnly = true; }
        if (Flags.IsBitSet(3)) { Cdn = true; }
        if (Flags.IsBitSet(4)) { Static = true; }
        if (Flags.IsBitSet(5)) { ThisPortOnly = true; }
        Id = buffer.ReadInt32();
        IpAddress = buffer.ReadString();
        Port = buffer.ReadInt32();
        if (Flags.IsBitSet(10)) { Secret = buffer.ReadBytes(); }
    }
}