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

namespace MyTelegram.Schema;

///<summary>
/// Incoming phone call
/// See <a href="https://corefork.telegram.org/constructor/phoneCallWaiting" />
///</summary>
[TlObject(0xc5226f17)]
public sealed class TPhoneCallWaiting : IPhoneCall
{
    public uint ConstructorId => 0xc5226f17;
    ///<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>
    /// Is this a video call
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Video { get; set; }

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

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

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

    ///<summary>
    /// Admin ID
    ///</summary>
    public long AdminId { get; set; }

    ///<summary>
    /// Participant ID
    ///</summary>
    public long ParticipantId { get; set; }

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

    ///<summary>
    /// When was the phone call received
    ///</summary>
    public int? ReceiveDate { get; set; }

    public void ComputeFlag()
    {
        if (Video) { Flags = Flags.SetBit(6); }
        if (/*ReceiveDate != 0 && */ReceiveDate.HasValue) { Flags = Flags.SetBit(0); }
    }

    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(Protocol);
        if (Flags.IsBitSet(0)) { writer.Write(ReceiveDate.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(6)) { Video = true; }
        Id = buffer.ReadInt64();
        AccessHash = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        AdminId = buffer.ReadInt64();
        ParticipantId = buffer.ReadInt64();
        Protocol = buffer.Read<MyTelegram.Schema.IPhoneCallProtocol>();
        if (Flags.IsBitSet(0)) { ReceiveDate = buffer.ReadInt32(); }
    }
}