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 -*-

import asyncio
from ccxt.base.errors import ArgumentsRequired  # noqa E402
from ccxt.test.exchange.base import test_ticker  # noqa E402
from ccxt.test.exchange.base import test_shared_methods  # noqa E402

async def test_watch_bids_asks(exchange, skipped_properties, symbol):
    without_symbol = test_watch_bids_asks_helper(exchange, skipped_properties, None)
    with_symbol = test_watch_bids_asks_helper(exchange, skipped_properties, [symbol])
    await asyncio.gather(*[with_symbol, without_symbol])


async def test_watch_bids_asks_helper(exchange, skipped_properties, arg_symbols, arg_params={}):
    method = 'watchBidsAsks'
    now = exchange.milliseconds()
    ends = now + 15000
    while now < ends:
        success = True
        should_return = False
        response = None
        try:
            response = await exchange.watch_bids_asks(arg_symbols, arg_params)
        except Exception as e:
            # for some exchanges, multi symbol methods might require symbols array to be present, so
            # so, if method throws "arguments-required" exception, we don't fail test, but just skip silently,
            # because tests will make a second call of this method with symbols array
            if (isinstance(e, ArgumentsRequired)) and (arg_symbols is None or len(arg_symbols) == 0):
                # todo: provide random symbols to try
                # return false;
                should_return = True
            elif not test_shared_methods.is_temporary_failure(e):
                raise e
            now = exchange.milliseconds()
            # continue;
            success = False
        if should_return:
            return False
        if success:
            assert isinstance(response, dict), exchange.id + ' ' + method + ' ' + exchange.json(arg_symbols) + ' must return an object. ' + exchange.json(response)
            values = list(response.values())
            checked_symbol = None
            if arg_symbols is not None and len(arg_symbols) == 1:
                checked_symbol = arg_symbols[0]
            test_shared_methods.assert_non_emtpy_array(exchange, skipped_properties, method, values, checked_symbol)
            for i in range(0, len(values)):
                ticker = values[i]
                test_ticker(exchange, skipped_properties, method, ticker, checked_symbol)
            now = exchange.milliseconds()
    return True
