import asyncio
import atexit
import os
import signal
import subprocess
import time
import json

from pyrogram import Client
from pyrogram import filters
from pyrogram.types import Message

from pytgcalls import idle
from pytgcalls import PyTgCalls
from pytgcalls import StreamType
from pytgcalls.types import Update
from pytgcalls.types.input_stream import InputAudioStream
from pytgcalls.types.input_stream import InputStream

with open('config.json', 'r') as f:
    config = json.load(f)

app = Client(
    'py-tgcalls',
    api_id=config['api_id'],
    api_hash=config['api_hash'],
)

call_py = PyTgCalls(app)

group_call_id = None

@call_py.on_stream_end()
async def on_stream_end(client: PyTgCalls, update: Update):
    await client.leave_group_call(group_call_id)

@app.on_message(filters.regex('!test'))
async def test_handler(client: Client, message: Message):
    global group_call_id
    group_call_id = message.chat.id

    await call_py.join_group_call(
        message.chat.id,
        InputStream(
            InputAudioStream(
                "mineur.pcm",
            ),
        ),
        stream_type=StreamType().pulse_stream,
    )

call_py.start()
idle()
