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

import json
from pprint import pprint

from tvizbase.api import Api

print('connect')
b4 = Api()
print('try call')

block = 3058791

tx = b4.get_ops_in_block(block)

pprint(tx)
input()
account = viz.get_accounts([settings['viz_account']['login']])[0]
receiver = 'committee'
#Calculating percentage of the award
award_percent = round(settings['award_percent'] * 100 *
                      settings['award_base'] / account['SHARES'])
#Sending award in the blockchain
res = viz.award(settings['viz_account']['login'], receiver, award_percent,
                settings['viz_account']['key'])
if res == False:
    sys.exit()
#Saving the last operation block number in the json file
settings['last_block_num'] = res['block_num']
with open(os.path.dirname(__file__) + '/settings.json', 'w') as sett_file:
    json.dump(settings, sett_file)
#Getting current award size from the virtual operations
ops = viz.get_ops_in_block(int(res['block_num']))
for op in ops:
    op = op['op']
    if (op[0] == 'receive_award'
            and op[1]['initiator'] == settings['viz_account']['login']
            and op[1]['receiver'] == receiver):
        award_size = round(
            float(op[1]['shares'].split()[0]) *
            (100 / settings['award_percent']) / settings['award_base'], 6)
        #Sending new custom operation in the blockchain
        viz.custom('award_informer', [
            'award_informer', {
                'time_utc': str(datetime.utcnow()),
                'award_on_capital': award_size
            }
        ], settings['viz_account']['login'], settings['viz_account']['key'])