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

namespace MyTelegram.Schema;

///<summary>
/// Indicates a discarded phone call
/// See <a href="https://corefork.telegram.org/constructor/phoneCallDiscarded" />
///</summary>
[TlObject(0x50ca4de1)]
public sealed class TPhoneCallDiscarded : IPhoneCall
{
    public uint ConstructorId => 0x50ca4de1;
    ///<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 server required the user to <a href="https://corefork.telegram.org/method/phone.setCallRating">rate</a> the call
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool NeedRating { get; set; }

    ///<summary>
    /// Whether the server required the client to <a href="https://corefork.telegram.org/method/phone.saveCallDebug">send</a> the libtgvoip call debug data
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool NeedDebug { get; set; }

    ///<summary>
    /// Whether the call was 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>
    /// Why was the phone call discarded
    /// See <a href="https://corefork.telegram.org/type/PhoneCallDiscardReason" />
    ///</summary>
    public MyTelegram.Schema.IPhoneCallDiscardReason? Reason { get; set; }

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

    public void ComputeFlag()
    {
        if (NeedRating) { Flags = Flags.SetBit(2); }
        if (NeedDebug) { Flags = Flags.SetBit(3); }
        if (Video) { Flags = Flags.SetBit(6); }
        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(Id);
        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)) { NeedRating = true; }
        if (Flags.IsBitSet(3)) { NeedDebug = true; }
        if (Flags.IsBitSet(6)) { Video = true; }
        Id = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { Reason = buffer.Read<MyTelegram.Schema.IPhoneCallDiscardReason>(); }
        if (Flags.IsBitSet(1)) { Duration = buffer.ReadInt32(); }
    }
}