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

namespace MyTelegram.Schema.Phone;

///<summary>
/// Join a group call
/// <para>Possible errors</para>
/// Code Type Description
/// 400 DATA_JSON_INVALID The provided JSON data is invalid.
/// 403 GROUPCALL_FORBIDDEN The group call has already ended.
/// 400 GROUPCALL_INVALID The specified group call is invalid.
/// 400 GROUPCALL_SSRC_DUPLICATE_MUCH The app needs to retry joining the group call with a new SSRC value.
/// 400 JOIN_AS_PEER_INVALID The specified peer cannot be used to join a group call.
/// See <a href="https://corefork.telegram.org/method/phone.joinGroupCall" />
///</summary>
[TlObject(0x8fb53057)]
public sealed class RequestJoinGroupCall : IRequest<MyTelegram.Schema.IUpdates>
{
    public uint ConstructorId => 0x8fb53057;
    ///<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>
    /// If set, the user will be muted by default upon joining.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Muted { get; set; }

    ///<summary>
    /// If set, the user's video will be disabled by default upon joining.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool VideoStopped { get; set; }

    ///<summary>
    /// The group call
    /// See <a href="https://corefork.telegram.org/type/InputGroupCall" />
    ///</summary>
    public MyTelegram.Schema.IInputGroupCall Call { get; set; }

    ///<summary>
    /// Join the group call, presenting yourself as the specified user/channel
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer JoinAs { get; set; }

    ///<summary>
    /// The invitation hash from the <a href="https://corefork.telegram.org/api/links#video-chat-livestream-links">invite link »</a>, if provided allows speaking in a livestream or muted group chat.
    ///</summary>
    public string? InviteHash { get; set; }
    public ReadOnlyMemory<byte>? PublicKey { get; set; }
    public byte[]? Block { get; set; }

    ///<summary>
    /// WebRTC parameters
    /// See <a href="https://corefork.telegram.org/type/DataJSON" />
    ///</summary>
    public MyTelegram.Schema.IDataJSON Params { get; set; }

    public void ComputeFlag()
    {
        if (Muted) { Flags = Flags.SetBit(0); }
        if (VideoStopped) { Flags = Flags.SetBit(2); }
        if (InviteHash != null) { Flags = Flags.SetBit(1); }
        if (PublicKey != null) { Flags = Flags.SetBit(3); }
        if (Block != null) { Flags = Flags.SetBit(3); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Call);
        writer.Write(JoinAs);
        if (Flags.IsBitSet(1)) { writer.Write(InviteHash); }
        if (Flags.IsBitSet(3)) { writer.WriteRawBytes(PublicKey); }
        if (Flags.IsBitSet(3)) { writer.Write(Block); }
        writer.Write(Params);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Muted = true; }
        if (Flags.IsBitSet(2)) { VideoStopped = true; }
        Call = buffer.Read<MyTelegram.Schema.IInputGroupCall>();
        JoinAs = buffer.Read<MyTelegram.Schema.IInputPeer>();
        if (Flags.IsBitSet(1)) { InviteHash = buffer.ReadString(); }
        if (Flags.IsBitSet(3)) { PublicKey = buffer.ReadInt256(); }
        if (Flags.IsBitSet(3)) { Block = buffer.ReadBytes(); }
        Params = buffer.Read<MyTelegram.Schema.IDataJSON>();
    }
}
