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

namespace MyTelegram.Schema;

///<summary>
/// A phone call
/// See <a href="https://corefork.telegram.org/constructor/messageActionPhoneCall" />
///</summary>
[TlObject(0x80e11a7f)]
public sealed class TMessageActionPhoneCall : IMessageAction
{
    public uint ConstructorId => 0x80e11a7f;
    ///<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 CallId { get; set; }

    ///<summary>
    /// If the call has ended, the reason why it ended
    /// See <a href="https://corefork.telegram.org/type/PhoneCallDiscardReason" />
    ///</summary>
    public MyTelegram.Schema.IPhoneCallDiscardReason? Reason { get; set; }

    ///<summary>
    /// Duration of the call in seconds
    ///</summary>
    public int? Duration { get; set; }

    public void ComputeFlag()
    {
        if (Video) { Flags = Flags.SetBit(2); }
        if (Reason != null) { Flags = Flags.SetBit(0); }
        if (/*Duration != 0 && */Duration.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(CallId);
        if (Flags.IsBitSet(0)) { writer.Write(Reason); }
        if (Flags.IsBitSet(1)) { writer.Write(Duration.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { Video = true; }
        CallId = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { Reason = buffer.Read<MyTelegram.Schema.IPhoneCallDiscardReason>(); }
        if (Flags.IsBitSet(1)) { Duration = buffer.ReadInt32(); }
    }
}