import os
import sys

root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
sys.path.append(root)

# ----------------------------------------------------------------------------

# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

# ----------------------------------------------------------------------------
# -*- coding: utf-8 -*-

from ccxt.test.exchange.base import test_ohlcv  # noqa E402
from ccxt.test.exchange.base import test_shared_methods  # noqa E402

async def test_watch_ohlcv_for_symbols(exchange, skipped_properties, symbol):
    method = 'watchOHLCVForSymbols'
    now = exchange.milliseconds()
    ends = now + 15000
    timeframe_keys = list(exchange.timeframes.keys())
    assert len(timeframe_keys), exchange.id + ' ' + method + ' - no timeframes found'
    # prefer 1m timeframe if available, otherwise return the first one
    chosen_timeframe_key = '1m'
    if not exchange.in_array(chosen_timeframe_key, timeframe_keys):
        chosen_timeframe_key = timeframe_keys[0]
    limit = 10
    duration = exchange.parse_timeframe(chosen_timeframe_key)
    since = exchange.milliseconds() - duration * limit * 1000 - 1000
    while now < ends:
        response = None
        success = True
        try:
            response = await exchange.watch_ohlcv_for_symbols([[symbol, chosen_timeframe_key]], since, limit)
        except Exception as e:
            if not test_shared_methods.is_temporary_failure(e):
                raise e
            now = exchange.milliseconds()
            # continue;
            success = False
        if success:
            assertion_message = exchange.id + ' ' + method + ' ' + symbol + ' ' + chosen_timeframe_key + ' | ' + exchange.json(response)
            assert isinstance(response, dict), 'Response must be a dictionary. ' + assertion_message
            assert symbol in response, 'Response should contain the symbol as key. ' + assertion_message
            symbol_obj = response[symbol]
            assert isinstance(symbol_obj, dict), 'Response.Symbol should be a dictionary. ' + assertion_message
            assert chosen_timeframe_key in symbol_obj, 'Response.symbol should contain the timeframe key. ' + assertion_message
            ohlcvs = symbol_obj[chosen_timeframe_key]
            assert isinstance(ohlcvs, list), 'Response.symbol.timeframe should be an array. ' + assertion_message
            now = exchange.milliseconds()
            for i in range(0, len(ohlcvs)):
                test_ohlcv(exchange, skipped_properties, method, ohlcvs[i], symbol, now)
    return True
