class  Balance_updater(threading.Thread):

    def __init__(self, parent):
        threading.Thread.__init__(self)
        self.daemon = True
        self.parent = parent
        if hasattr(self.parent, 'client'):
            self.client = self.parent.client
        else:
            authmethod = self.parent.config.get('coinapult_auth_method', 'REST')
            if authmethod == 'REST':
                self.client = CoinapultClient(credentials={'key': self.parent.api_key(),
                                                           'secret': self.parent.api_secret()})
            else:
                ecc_pub = self.parent.wallet.storage.get("coinapult_ecc_public", '')
                ecc_priv = self.parent.wallet.storage.get("coinapult_ecc_private", '')
                try:
                    self.client = CoinapultClient(ecc={'pubkey': ecc_pub, 'privkey': ecc_priv}, authmethod='ecc')
                except (CoinapultError, CoinapultErrorECC):
                    self.client = None
        self.lock = threading.Lock()
        self.query_balances = threading.Event()
        # self.parent.gui.main_window.emit(SIGNAL("refresh_locks_account()"))
        self.is_running = False

    def stop(self):
        self.is_running = False

    def refresh_locks_account(self):
        try:
            locks_bals = copy(LOCKS_BALS)
            bals = self.client.accountInfo(balanceType='locks', locksAsBTC=True)
            if bals and 'balances' in bals:
                if len(bals['balances']) > 0:
                    locks_bals['BTC'] = bals['balances'][0]['amount']
                else:
                    locks_bals['BTC'] = 0

            bals = self.client.accountInfo(balanceType='locks')
            if bals and 'balances' in bals:
                for cur in LOCKS_CURRENCIES:
                    found = False
                    for bal in bals['balances']:
                        if bal['currency'] == cur:
                            found = True
                            locks_bals[bal['currency']] = bal['amount']
                    if not found:
                        locks_bals[cur] = 0
            self.parent.config.set_key('Locks_balances', locks_bals, True)

            #calculate processing balances
            unbals = copy(LOCKS_BALS)
            pending_locks = self.parent.config.get('pending_locks', [])
            for l in pending_locks:
                try:
                    lock = self.client.search(transaction_id=l['transaction_id'])
                    # print lock
                    if lock and lock['state'] not in ('pending', 'complete', 'canceled'):
                        unbals[lock['out']['currency']] += lock['out']['expected']
                    elif lock and lock['state'] in ('complete', 'canceled'):
                        pending_locks.remove(l)
                except (CoinapultError, CoinapultErrorECC) as ce:
                    print ce
            self.parent.config.set_key('Locks_unbalances', unbals, True)
            self.parent.config.set_key('pending_locks', pending_locks, True)

            #update labels with unlock details
            pending_unlocks = self.parent.config.get('pending_unlocks', [])
            for ul in pending_unlocks:
                try:
                    unlock = self.client.search(transaction_id=ul['transaction_id'])
                    # print unlock
                    if unlock and unlock['state'] in ('complete', 'canceled'):
                        #TODO update labels
                        pending_unlocks.remove(ul)
                        # pending_unlocks = self.parent.config.set_key('pending_unlocks', pending_unlocks, True)
                        # self.parent.config.set_key('pending_unlocks', pending_unlocks)
                        continue
                except (CoinapultError, CoinapultErrorECC) as ce:
                    print ce
            self.parent.config.set_key('pending_unlocks', pending_unlocks, True)

            self.parent.gui.main_window.emit(SIGNAL("refresh_locks_account()"))
        except (CoinapultError, CoinapultErrorECC) as ce:
            # TODO: this isn't really something to bother the user about, it is probably just a bad internet connection
            print ce
            return

    def run(self):
        self.is_running = True
        while self.is_running:
            self.query_balances.clear()
            self.refresh_locks_account()
            self.query_balances.wait(15)