import retrying

from test.integration import moving_coins
from test.integration.v1.base_integration_test import BaseV1IntegrationTest, Satoshi


class TestWalletInfo(BaseV1IntegrationTest):
    _SATOSHI = Satoshi(12345600)

    @moving_coins
    def test_get_wallet_info(self):
        authentication_headers = self.signin_with_new_user().authentication_headers
        wallet_info = self.get_wallet_info(authentication_headers)
        self.assertEqual(0, wallet_info.confirmed_satoshi_amount)
        self.assertEqual(0, wallet_info.unconfirmed_satoshi_amount)
        cur_btc_address = self.get_current_btc_address(authentication_headers)
        self.receive_satoshi(self._SATOSHI, authentication_headers)
        self.assertEqual('2', cur_btc_address[0])
        print('Current BTC address:', cur_btc_address)

        @retrying.retry(
                wait_exponential_multiplier=100,
                wait_exponential_max=1000, stop_max_delay=60000,
                retry_on_exception=lambda e: isinstance(e, AssertionError)
        )
        def _f():
            print('Attempting to get {} unconfirmed satoshi'.format(self._SATOSHI))
            cur_wallet_info = self.get_wallet_info(authentication_headers)
            self.assertEqual(self._SATOSHI, cur_wallet_info.unconfirmed_satoshi_amount)
            self.assertEqual(0, cur_wallet_info.confirmed_satoshi_amount)
            self.assertNotEqual(cur_btc_address, self.get_current_btc_address(authentication_headers))
        _f()

        self.generate_blocks()

        @retrying.retry(
                wait_exponential_multiplier=100,
                wait_exponential_max=1000, stop_max_delay=60000,
                retry_on_exception=lambda e: isinstance(e, AssertionError)
        )
        def _f():
            print('Attempting to get {} satoshi'.format(self._SATOSHI))
            cur_wallet_info = self.get_wallet_info(authentication_headers)
            self.assertEqual(self._SATOSHI, cur_wallet_info.confirmed_satoshi_amount)
            self.assertEqual(0, cur_wallet_info.unconfirmed_satoshi_amount)
        _f()
