# -*- coding: utf-8 -*-
import click
from .decorators import online
from .main import main, config
from .ui import print_table, print_tx


@main.group()
def tools():
    """ Further tools
    """
    pass


@tools.command()
@click.pass_context
@online
@click.argument("account")
def getcloudloginkey(ctx, account):
    """ Return keys for cloudlogin
    """
    from bitsharesbase.account import PasswordKey

    password = click.prompt("Passphrase", hide_input=True).strip()
    t = [["role", "wif", "pubkey", "accounts"]]
    for role in ["owner", "active", "memo"]:
        wif = PasswordKey(account, password, role=role)
        pubkey = format(wif.get_public_key(), ctx.bitshares.rpc.chain_params["prefix"])

        t.append(
            [
                role,
                str(wif.get_private_key()),
                pubkey,
                ctx.bitshares.wallet.getAccountFromPublicKey(pubkey) or "",
            ]
        )

    print_table(t)


@tools.command()
@click.pass_context
@online
@click.option("--limit", default=10, type=int)
def getbrainkeys(ctx, limit):
    """ Return keys for cloudlogin
    """
    from bitsharesbase.account import BrainKey

    password = click.prompt("Passphrase", hide_input=True).strip()
    t = [["index", "wif", "pubkey", "accounts"]]
    wif = BrainKey(password)
    for i in range(limit):
        pubkey = format(wif.get_public_key(), ctx.bitshares.rpc.chain_params["prefix"])

        t.append(
            [
                i,
                str(wif.get_private_key()),
                pubkey,
                ctx.bitshares.wallet.getAccountFromPublicKey(pubkey) or "",
            ]
        )
        next(wif)

    print_table(t)


@tools.command()
@click.argument("identifiers", nargs=-1)
def operation(identifiers):
    from bitsharesbase.operations import operations, getOperationNameForId

    ret = [["id", "name"]]
    for identifier in identifiers:
        try:
            i = int(identifier)
            name = operations[i]
            ret.append([i, name])
        except Exception:
            name = getOperationNameForId(identifier)
            ret.append([identifier, name])
    print_table(ret)

@@ -1770,16 +1770,62 @@
 fiers):%0A
+    %22%22%22 Get an operation name/id pair%0A    %22%22%22%0A
     from
@@ -1980,16 +1980,17 @@
        i
+d
  = int(i
@@ -2019,25 +2019,28 @@
  name = 
-o
+getO
 peration
 s%5Bi%5D%0A   
@@ -2035,45 +2035,19 @@
 tion
-s%5Bi%5D%0A            ret.append(%5Bi, name%5D
+NameForId(i
 )%0A  
@@ -2089,38 +2089,16 @@
  name = 
-getOperationNameForId(
 identifi
@@ -2091,38 +2091,67 @@
 ame = identifier
-)
 %0A    
+        id = operations%5Bname%5D%0A
         ret.appe
@@ -2156,24 +2156,16 @@
 pend(%5Bid
-entifier
 , name%5D)

fix tools operation