#!/usr/bin/python3
# -*- coding: utf-8 -*-

# built ins
import asyncio
from asyncio import Queue
import sys

# installed
import uvloop
import redis.asyncio as aioredis
#from redistimeseries.client import Client

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

from messaging.telegram_bot import telegram_bot_sendtext
from transaction_management import relaying_result
from utilities import string_modification as str_mod, system_tools

async def main():
    """
    https://redis-py.readthedocs.io/en/stable/examples/asyncio_examples.html
    """
    sub_account_id = "deribit-148510"

    # registering strategy config file    
    file_toml = "config_strategies.toml"
    
    try:
        pool = aioredis.ConnectionPool.from_url(
            "redis://localhost", 
            port=6379, 
            db=0, 
            protocol=3, 
            encoding="utf-8",
            decode_responses=True
            )
        
        client_redis: object = aioredis.Redis.from_pool(pool)
        
        # parsing config file
        config_app = system_tools.get_config_tomli(file_toml)

        # get redis channels
        redis_channels: dict = config_app["redis_channels"][0]

        queue = Queue(maxsize=1)
                 
        await asyncio.sleep(0.0005)
            
        await asyncio.gather(
         
            relaying_result.relaying_result(
            client_redis,
            config_app,
            redis_channels,
                ),   
                        )  

        await queue.join()

    except Exception as error:
        
        system_tools.parse_error_message(error)
        await telegram_bot_sendtext (
            f"app-{error}",
            "general_error"
            )

if __name__ == "__main__":
    
    try:
        
        uvloop.run(main())
        
    except(
        KeyboardInterrupt, 
        SystemExit
        ):
        
        asyncio.get_event_loop().run_until_complete(main())
        
    except Exception as error:
        system_tools.parse_error_message(error)
        
        asyncio.run(telegram_bot_sendtext (
            error,
            "general_error"
            ))
