import pytest
from cryptotik import Bittrex
from decimal import Decimal

private = pytest.mark.skipif(
    not pytest.config.getoption("--apikey"),
    reason="needs --apikey option to run."
)


def test_format_pair():
    '''test string formating to match API expectations'''

    assert Bittrex.format_pair("btc_ppc") == "btc-ppc"


def test_get_markets():
    '''test get_markets'''

    assert isinstance(Bittrex.get_markets(), list)
    assert "btc-ltc" in Bittrex.get_markets()


def test_get_market_ticker():
    '''test get_market_ticker'''

    ticker = Bittrex.get_market_ticker("btc-ltc")

    assert isinstance(ticker, dict)
    assert list(ticker.keys()) == ['Bid', 'Ask', 'Last']


@pytest.mark.parametrize("depth", [10, 20, 50])
def test_get_market_orders(depth):
    '''test get_market_orderbook'''

    market_orders = Bittrex.get_market_orders("btc-ppc", depth)

    assert isinstance(market_orders, dict)
    assert isinstance(market_orders["buy"], list)
    assert isinstance(market_orders["sell"], list)


def test_get_market_trade_history():
    '''test get_market_trade_history'''

    trade_history = Bittrex.get_market_trade_history("btc-ppc", 10)

    assert isinstance(trade_history, list)
    assert len(trade_history) == 10
    assert list(trade_history[0].keys()) == ['Id', 'TimeStamp', 'Quantity',
                                             'Price', 'Total', 'FillType',
                                             'OrderType']


def test_get_market_depth():
    '''test get_market_depth'''

    market_depth = Bittrex.get_market_depth("btc-ppc")

    assert isinstance(market_depth, dict)
    assert isinstance(market_depth["asks"], Decimal)


def test_get_market_spread():
    '''test get_market spread'''

    assert isinstance(Bittrex.get_market_spread("btc-vtc"), Decimal)


@private
def test_get_balance(apikey, secret):
    '''test get_balances'''

    btrx = Bittrex(apikey, secret)
    balances = btrx.get_balances()

    assert isinstance(balances, list)
    assert list(balances[0].keys()) == ['Currency', 'Balance', 'Available',
                                        'Pending', 'CryptoAddress']


@private
def test_get_open_orders(apikey, secret):
    '''test get_balances'''

    btrx = Bittrex(apikey, secret)
    orders = btrx.get_open_orders()

    if orders:
        assert isinstance(orders, list)
        assert list(orders[0].keys()) == ['Uuid', 'OrderUuid',
                                          'Exchange', 'OrderType',
                                          'Quantity', 'QuantityRemaining',
                                          'Limit', 'CommissionPaid', 'Price',
                                          'PricePerUnit', 'Opened', 'Closed',
                                          'CancelInitiated', 'ImmediateOrCancel',
                                          'IsConditional', 'Condition',
                                          'ConditionTarget']


@private
@pytest.mark.xfail
def test_get_deposit_address(apikey, secret):
    '''test get_deposit_address'''

    btrx = Bittrex(apikey, secret)

    assert isinstance(btrx.get_deposit_address("btc"), str)


@private
def test_buy(apikey, secret):
    '''test buy'''

    btrx = Bittrex(apikey, secret)
    buy = btrx.buy("btc_ppc", 0.0000001, 0.0001)

    assert buy == {'message': 'DUST_TRADE_DISALLOWED_MIN_VALUE_50K_SAT',
                   'result': None,
                   'success': False
                   }


@private
def test_sell(apikey, secret):
    '''test buy'''

    btrx = Bittrex(apikey, secret)
    sell = btrx.sell("btc_ppc", 0.0000001, 0.0001)

    assert sell == {'message': 'DUST_TRADE_DISALLOWED_MIN_VALUE_50K_SAT',
                    'result': None,
                    'success': False
                    }


@private
@pytest.mark.xfail
def test_get_order_history(apikey, secret):
    '''test get_order_history'''

    btrx = Bittrex(apikey, secret)
    order_history = btrx.get_order_history()

    assert isinstance(order_history, list)
    assert list(order_history[0].keys()) == ['OrderUuid', 'Exchange',
                                             'TimeStamp', 'OrderType',
                                             'Limit', 'Quantity',
                                             'QuantityRemaining', 'Commission',
                                             'Price', 'PricePerUnit', 'IsConditional',
                                             'Condition', 'ConditionTarget',
                                             'ImmediateOrCancel', 'Closed']


@@ -4514,12 +4514,1225 @@
  'Closed'%5D%0A%0A
+%0A@private%0A@pytest.mark.xfail%0Adef test_get_withdrawal_history(apikey, secret):%0A    '''test get_withdrawal_history'''%0A%0A    btrx = Bittrex(apikey, secret)%0A    withdrawal_history = btrx.get_withdrawal_history()%0A%0A    assert isinstance(withdrawal_history, list)%0A    assert list(withdrawal_history%5B0%5D.keys()) == %5B'PaymentUuid',%0A                                                  'Currency', 'Amount',%0A                                                  'Address', 'Opened',%0A                                                  'Authorized', 'PendingPayment',%0A                                                  'TxCost', 'TxId', 'Canceled',%0A                                                  'InvalidAddress'%5D%0A%0A%0A@private%0A@pytest.mark.xfail%0Adef test_get_deposit_history(apikey, secret):%0A    '''test get_withdrawal_history'''%0A%0A    btrx = Bittrex(apikey, secret)%0A    deposit_history = btrx.get_deposit_history()%0A%0A    assert isinstance(deposit_history, list)%0A    assert list(deposit_history%5B0%5D.keys()) == %5B'Id', 'Amount',%0A                                               'Currency', 'Confirmations',%0A                                               'LastUpdated', 'TxId',%0A                                               'CryptoAddress'%5D%0A%0A

add get_withdrawal_history and get_deposit_history unit test.