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

namespace MyTelegram.Schema;

///<summary>
/// Phone call
/// See <a href="https://corefork.telegram.org/constructor/phoneCall" />
///</summary>
[TlObject(0x30535af5)]
public sealed class TPhoneCall : IPhoneCall
{
    public uint ConstructorId => 0x30535af5;
    ///<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 P2P connection to the other peer is allowed
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool P2pAllowed { get; set; }

    ///<summary>
    /// Whether this is a video call
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Video { get; set; }
    public bool ConferenceSupported { get; set; }

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

    ///<summary>
    /// Access hash
    ///</summary>
    public long AccessHash { get; set; }

    ///<summary>
    /// Date of creation of the call
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// User ID of the creator of the call
    ///</summary>
    public long AdminId { get; set; }

    ///<summary>
    /// User ID of the other participant in the call
    ///</summary>
    public long ParticipantId { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/end-to-end/voice-calls">Parameter for key exchange</a>
    ///</summary>
    public ReadOnlyMemory<byte> GAOrB { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/end-to-end/voice-calls">Key fingerprint</a>
    ///</summary>
    public long KeyFingerprint { get; set; }

    ///<summary>
    /// Call protocol info to be passed to libtgvoip
    /// See <a href="https://corefork.telegram.org/type/PhoneCallProtocol" />
    ///</summary>
    public MyTelegram.Schema.IPhoneCallProtocol Protocol { get; set; }

    ///<summary>
    /// List of endpoints the user can connect to exchange call data
    ///</summary>
    public TVector<MyTelegram.Schema.IPhoneConnection> Connections { get; set; }

    ///<summary>
    /// When was the call actually started
    ///</summary>
    public int StartDate { get; set; }

    ///<summary>
    /// Custom JSON-encoded call parameters to be passed to tgcalls.
    /// See <a href="https://corefork.telegram.org/type/DataJSON" />
    ///</summary>
    public MyTelegram.Schema.IDataJSON? CustomParameters { get; set; }

    public void ComputeFlag()
    {
        if (P2pAllowed) { Flags = Flags.SetBit(5); }
        if (Video) { Flags = Flags.SetBit(6); }
        if (ConferenceSupported) { Flags = Flags.SetBit(8); }
        if (CustomParameters != null) { Flags = Flags.SetBit(7); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(AccessHash);
        writer.Write(Date);
        writer.Write(AdminId);
        writer.Write(ParticipantId);
        writer.Write(GAOrB);
        writer.Write(KeyFingerprint);
        writer.Write(Protocol);
        writer.Write(Connections);
        writer.Write(StartDate);
        if (Flags.IsBitSet(7)) { writer.Write(CustomParameters); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(5)) { P2pAllowed = true; }
        if (Flags.IsBitSet(6)) { Video = true; }
        if (Flags.IsBitSet(8)) { ConferenceSupported = true; }
        Id = buffer.ReadInt64();
        AccessHash = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        AdminId = buffer.ReadInt64();
        ParticipantId = buffer.ReadInt64();
        GAOrB = buffer.ReadBytes();
        KeyFingerprint = buffer.ReadInt64();
        Protocol = buffer.Read<MyTelegram.Schema.IPhoneCallProtocol>();
        Connections = buffer.Read<TVector<MyTelegram.Schema.IPhoneConnection>>();
        StartDate = buffer.ReadInt32();
        if (Flags.IsBitSet(7)) { CustomParameters = buffer.Read<MyTelegram.Schema.IDataJSON>(); }
    }
}