import json
import click
import datetime
from bitshares.worker import Workers
from bitshares.account import Account
from bitshares.amount import Amount
from .decorators import onlineChain, unlockWallet
from .main import main, config
from .ui import print_table, print_tx


@main.command()
@click.pass_context
@onlineChain
@click.argument("workers", nargs=-1)
@click.option(
    "--account",
    default=config["default_account"],
    help="Account that takes this action",
    type=str,
)
@unlockWallet
def approveworker(ctx, workers, account):
    """ Approve worker(es)
    """
    print_tx(ctx.bitshares.approveworker(workers, account=account))


@main.command()
@click.pass_context
@onlineChain
@click.argument("workers", nargs=-1)
@click.option(
    "--account",
    help="Account that takes this action",
    default=config["default_account"],
    type=str,
)
@unlockWallet
def disapproveworker(ctx, workers, account):
    """ Disapprove worker(es)
    """
    print_tx(ctx.bitshares.disapproveworker(workers, account=account))


@main.command()
@click.pass_context
@onlineChain
@click.argument("account", default=None, required=False)
@click.option("--top", type=int)
def workers(ctx, account, top):
    """ List all workers (of an account)
    """
    workers = Workers(account)
    t = [["id", "name/url", "daily_pay", "votes", "time", "account"]]
    workers_sorted = sorted(
        workers, key=lambda x: int(x["total_votes_for"]), reverse=True
    )
    if top:
        workers_sorted = workers_sorted[: top + 1]
    for worker in workers_sorted:
        if worker["work_end_date"] < datetime.datetime.utcnow():
            continue
        votes = Amount({"amount": worker["total_votes_for"], "asset_id": "1.3.0"})
        amount = Amount({"amount": worker["daily_pay"], "asset_id": "1.3.0"})
        t.append(
            [
                worker["id"],
                "{name}\n{url}".format(**worker),
                str(amount),
                str(votes),
                "{work_begin_date:%Y-%m-%d}\n-\n{work_end_date:%Y-%m-%d}".format(
                    **worker
                ),
                str(Account(worker["worker_account"])["name"]),
            ]
        )
    print_table(t)

@@ -1,12 +1,36 @@
+# -*- coding: utf-8 -*-%0A
 import json%0A
@@ -1192,16 +1192,67 @@
 pe=int)%0A
+@click.option(%22--sort%22, default=%22total_votes_for%22)%0A
 def work
@@ -1272,16 +1272,22 @@
 unt, top
+, sort
 ):%0A    %22
@@ -1334,138 +1334,463 @@
 %22%22%22%0A
-    workers = Workers(account)%0A    t = %5B%5B%22id%22, %22name/url%22, %22daily_pay%22, %22votes%22, %22time%22, %22account%22%5D%5D%0A    workers_sorted = sorted(%0A
+%0A    def normalize_sort_keys(name):%0A        if name == %22votes%22:%0A            return %22total_votes_for%22%0A        return name%0A%0A    workers = Workers(account)%0A    t = %5B%5B%22id%22, %22name/url%22, %22daily_pay%22, %22votes%22, %22time%22, %22account%22%5D%5D%0A    sort = sort%0A    sort = normalize_sort_keys(sort)%0A    if sort in %5B%22total_votes_for%22%5D:%0A        workers_sorted = sorted(workers, key=lambda x: int(x%5Bsort%5D), reverse=True)%0A    elif sort == %22id%22:%0A        workers_sorted = sorted(%0A    
     
@@ -1822,33 +1822,34 @@
 : int(x%5B
-%22total_votes_for%22
+sort%5D.split(%22.%22)%5B2
 %5D), reve
@@ -1857,24 +1857,116 @@
 se=True%0A    
+    )%0A    else:%0A        workers_sorted = sorted(workers, key=lambda x: x%5Bsort%5D, reverse=True
 )%0A    if top

Allow to change sorting for workers