from coinlib.helper import in_ipynb, set_loglevel
from coinlib.helper import pip_install_or_ignore
from coinlib.helper import log, debug
import asyncio

from coinlib.featureSaver.FeatureSaverServer import FeatureSaverServer
from coinlib.notification.Notification import Notification
from coinlib.symbols.Symbols import Symbols

pip_install_or_ignore("ipynb_path", "ipynb_path")
# pip_install_or_ignore("import_ipynb", "import_ipynb")
pip_install_or_ignore("websocket", "websocket-client")
pip_install_or_ignore("plotly", "plotly")
pip_install_or_ignore("simplejson", "simplejson")
pip_install_or_ignore("asyncio", "asyncio")
pip_install_or_ignore("grpc", "grpcio-tools")
pip_install_or_ignore("matplotlib", "matplotlib")
pip_install_or_ignore("zlib", "zlib")
pip_install_or_ignore("pandas", "pandas")
pip_install_or_ignore("timeit", "timeit")
pip_install_or_ignore("dateutil", "python-dateutil")
pip_install_or_ignore("chipmunkdb", "chipmunkdb-python-client")

global coinlib_job_task

from coinlib.feature.Features import Features
from coinlib.Registrar import Registrar
from coinlib.logics.Logic import Logic
from coinlib.Functions import Functions
from coinlib.CoinlibDataInterface import CoinlibDataInterface
from coinlib.statistics.Statistics import Statistics
from coinlib.Simulator import Simulator
from coinlib.ChartsIndicatorJob import ChartsIndicatorJob
from coinlib.broker.Broker import Broker

registrar = Registrar()

if registrar.features is None:
    registrar.features = Features()
    registrar.logic = Logic()
    registrar.functions = Functions()
    registrar.statistics = Statistics()
    registrar.simulator = Simulator()
    registrar.symbols = Symbols()
    registrar.data = CoinlibDataInterface()
    registrar.featureSaverServer = FeatureSaverServer()
    registrar.notifications = Notification()
    registrar.broker = Broker()


features = registrar.features
logic = registrar.logic
functions = registrar.functions
data = registrar.data
notifications = registrar.notifications
symbols = registrar.symbols
statistics = registrar.statistics
simulator = registrar.simulator
broker = registrar.broker
pip_install_or_ignore = pip_install_or_ignore
set_loglevel = set_loglevel
debug = debug

def registerToWorkspace(workspaceId):
    registrar.workspaceId = workspaceId

def addLogicToWorkspace(identifier, type, logicComponentId, params=None, workspaceId=registrar.workspaceId):
    return logic.addLogicToWorkspace(identifier,
                                     type,
                                     logicComponentId,
                                     params, workspaceId)

def connectAsLogic(hostname=None, workspaceId=None):
    logicModule = "offlineLogics"
    if registrar.isLiveEnvironment():
        logicModule = "onlineLogics"
    return connectCoinlib(hostname, workspaceId=workspaceId, worker_modules=[logicModule])

def connectAsBrokerSymbol(hostname=None, workspaceId=None):
    return connectCoinlib(hostname, workspaceId=workspaceId, worker_modules=["symbolBroker"])

def connectAsBroker(hostname=None, workspaceId=None):
    return connectCoinlib(hostname, workspaceId=workspaceId, worker_modules=["broker"])

def connectAsFeature(hostname=None):
    return connectCoinlib(hostname, workspaceId=None, worker_modules=["features"])

def connectCoinlib(hostname=None, workspaceId=None, worker_modules=["data"], reconnect=False):
    if registrar.connected and not reconnect:
        return

    if not registrar.hasEnvironment():
        if hostname is not None:
            registrar.setBackendPath(hostname)
        registrar.setEnvironment("dev")
        registrar.fixed_modules = worker_modules
        registrar.worker_modules = worker_modules
        registrar.workspaceId = workspaceId

    registrar.connected = True
    registrar.simulator.connect()

def waitForJobs():

    # register as a worker if not already done
    # check if plugin and then ignore this
    if registrar.currentPluginLoading is None:

        loop = asyncio.get_event_loop()
        try:
            loop.run_forever()
        finally:
            loop.close()
