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

namespace MyTelegram.Schema;

///<summary>
/// Identifies an endpoint that can be used to connect to the other user in a phone call
/// See <a href="https://corefork.telegram.org/constructor/phoneConnection" />
///</summary>
[TlObject(0x9cc123c7)]
public sealed class TPhoneConnection : IPhoneConnection
{
    public uint ConstructorId => 0x9cc123c7;
    ///<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 TCP should be used
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Tcp { get; set; }

    ///<summary>
    /// Endpoint ID
    ///</summary>
    public long Id { get; set; }

    ///<summary>
    /// IP address of endpoint
    ///</summary>
    public string Ip { get; set; }

    ///<summary>
    /// IPv6 address of endpoint
    ///</summary>
    public string Ipv6 { get; set; }

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

    ///<summary>
    /// Our peer tag
    ///</summary>
    public ReadOnlyMemory<byte> PeerTag { get; set; }

    public void ComputeFlag()
    {
        if (Tcp) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Ip);
        writer.Write(Ipv6);
        writer.Write(Port);
        writer.Write(PeerTag);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Tcp = true; }
        Id = buffer.ReadInt64();
        Ip = buffer.ReadString();
        Ipv6 = buffer.ReadString();
        Port = buffer.ReadInt32();
        PeerTag = buffer.ReadBytes();
    }
}