import json
import os
import pytest

from Configuration import Configuration
from DataCache_v3 import DataCache
from candles import Candles
from ExchangeInterface import ExchangeInterface
from indicators import Indicators
from Users import Users

# API keys from environment variables
ALPACA_API_KEY = os.environ.get('BRIGHTER_ALPACA_API_KEY', '')
ALPACA_API_SECRET = os.environ.get('BRIGHTER_ALPACA_API_SECRET', '')


@pytest.mark.skipif(not ALPACA_API_KEY, reason="ALPACA API keys not configured")
def test_indicators():
    # Object that interacts with the persistent data.
    data = DataCache()

    # Object that interacts and maintains exchange_interface and account data
    exchanges = ExchangeInterface(data)

    # Configuration and settings for the user app and charts
    config = Configuration()

    # Object that manages users in the system.
    users = Users(data_cache=data)

    # Object that maintains candlestick and price data.
    candles = Candles(exchanges=exchanges, users=users, datacache=data, config=config)

    # Object that interacts with and maintains data from available indicators
    ind_obj = Indicators(candles, users, data)

    user_name = 'guest'
    exchanges.connect_exchange('alpaca', user_name=user_name, api_keys={'key': ALPACA_API_KEY,
                                                                        'secret': ALPACA_API_SECRET})

    ind_obj.load_indicators(user_name)
    en_inds = ind_obj.get_enabled_indicators(user_name)
    all_inds = ind_obj.get_indicator_list(user_name)
    print(en_inds)
    print(all_inds)
    # volume_ind = all_inds.query("name=='EMA 5'").iloc[0]
    # a = ind_obj.process_indicator(indicator=volume_ind, num_results=10)
    # print(a)
    source = {'user_name': 'guest_226', 'market': {'exchange': 'alpaca', 'timeframe': '5m', 'market': 'BTC/USD'}}
    b = ind_obj.get_indicator_data(user_name=user_name, source=source)
    print(b)
    # src = {'symbol': 'BTC/USD', 'timeframe': '5m', 'exchange': 'alpaca'}
    # specific_property = {'period': 8, 'visible': True, 'color': '#241571', 'value': 0}
    # ind_obj.create_indicator(creator=user_name, name='RSI 8', kind='RSI', source=src, properties=specific_property)

    assert not en_inds.empty and all_inds is not None
