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

namespace MyTelegram.Schema.Phone;

///<summary>
/// Create a group call or livestream
/// <para>Possible errors</para>
/// Code Type Description
/// 400 CHANNEL_PRIVATE You haven't joined this channel/supergroup.
/// 400 CHAT_ADMIN_REQUIRED You must be an admin in this chat to do this.
/// 400 CREATE_CALL_FAILED An error occurred while creating the call.
/// 400 PEER_ID_INVALID The provided peer id is invalid.
/// 400 SCHEDULE_DATE_INVALID Invalid schedule date provided.
/// See <a href="https://corefork.telegram.org/method/phone.createGroupCall" />
///</summary>
[TlObject(0x48cdc6d8)]
public sealed class RequestCreateGroupCall : IRequest<MyTelegram.Schema.IUpdates>
{
    public uint ConstructorId => 0x48cdc6d8;
    ///<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 RTMP stream support should be enabled: only the <a href="https://corefork.telegram.org/api/channel">group/supergroup/channel</a> owner can use this flag.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool RtmpStream { get; set; }

    ///<summary>
    /// Associate the group call or livestream to the provided <a href="https://corefork.telegram.org/api/channel">group/supergroup/channel</a>
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer Peer { get; set; }

    ///<summary>
    /// Unique client message ID required to prevent creation of duplicate group calls
    ///</summary>
    public int RandomId { get; set; }

    ///<summary>
    /// Call title
    ///</summary>
    public string? Title { get; set; }

    ///<summary>
    /// For scheduled group call or livestreams, the absolute date when the group call will start
    ///</summary>
    public int? ScheduleDate { get; set; }

    public void ComputeFlag()
    {
        if (RtmpStream) { Flags = Flags.SetBit(2); }
        if (Title != null) { Flags = Flags.SetBit(0); }
        if (/*ScheduleDate != 0 && */ScheduleDate.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Peer);
        writer.Write(RandomId);
        if (Flags.IsBitSet(0)) { writer.Write(Title); }
        if (Flags.IsBitSet(1)) { writer.Write(ScheduleDate.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { RtmpStream = true; }
        Peer = buffer.Read<MyTelegram.Schema.IInputPeer>();
        RandomId = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Title = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { ScheduleDate = buffer.ReadInt32(); }
    }
}
