from Adding_To_Book import AddingToBook
from Retreiving_From_Book import RetrievingFromBook

task = int(input("What do you want to do?  \nPress 1 for accessing contact. \nPress 2 for updating existing contact. \nPress 3 for creating new contact."))
while task not in range(1, 4):
    task = int(input("Error! \nPress 1 for accessing contact. \nPress 2 for updating existing contact. \nPress 3 for creating new contact. "))
if task == 1:
    print("Accessing contact")
    finding_keys = []
    finding_values = []
    continue_to_find = True
    while continue_to_find:
        type = input("What kind of detail do you have? \nPlz input Name, Phone No, Email ID, or Address.")
        if type == ("Name" or "name") and "Name" not in finding_keys:
            finding_keys.append('Name')
            value = input("Plz input the name. ")
            finding_values.append(value)
        elif type == ("Phone No" or "PhoneNo" or "phone no" or "phoneno" or "Phone no" or "Phoneno" or "phone No" or "phoneNo") and 'Phone No' not in finding_keys:
            finding_keys.append('Phone No')
            value = input("Plz input the phone no. ")
            finding_values.append(value)
        elif type == ("Email ID" or "Email id" or "Email Id" or "Email iD" or "email ID" or "EMAIL ID" or "email id" or "EmailID" or "emailID") and 'Email ID' not in finding_keys:
            finding_keys.append('Email ID')
            value = input("Plz input the email id. ")
            finding_values.append(value)
        elif type == ("Address" or "address") and 'Address' not in finding_keys:
            finding_keys.append('Address')
            value = input("Plz input the address. ")
            finding_values.append(value)
        else:
            print("Error!")
        to_continue = input("do you have any more details you want to give? y/n ")
        while (to_continue != "y") and (to_continue != 'n'):
            to_continue = input("do you have any more details you want to give? y/n ")
        if to_continue == 'n':
            continue_to_find = False
            continue
        else:
            continue
    RetrievingFromBook.find_contact(finding_keys, finding_values)

elif task == 2:
    print("Updating existing contact")
    num_identifying_keys = int(input('How many details do you have to identify the contact you need to update? '))
    identifying_keys = []
    identifying_values = []
    for num in range(num_identifying_keys):
        identifying_key = input('what kind of detail do you have? \nName, Phone, EmailID or Address. Plz input in same form.')
        identifying_keys.append(identifying_key)
        identifying_value = input('Plz input the contact detail now. ')
        identifying_values.append(identifying_value)
    num_tbc_keys = int(input('How many details of this contact do you want to change? '))
    tbc_keys = []
    new_values = []
    for num in range(num_tbc_keys):
        tbc_key = input('what kind of detail do you want to change? \nName, Phone, EmailID or Address. Plz input in same form.')
        tbc_keys.append(tbc_key)
        new_value = input('Plz input the new contact detail now. ')
        new_values.append(new_value)
    AddingToBook.update_existing_contact(identifying_keys, identifying_values, tbc_keys, new_values)
elif task == 3:
    print("Creating new contact")
    print(" ")
    name = input("Plz input name of contact. Plz leave blank if you don't have the name. ")
    phone = input("Plz input phone number of contact. Plz leave blank if you don't have the phone number. ")
    email = input("Plz input email of contact. Plz leave blank if you don't have the email. ")
    address = input("Plz input address of contact. Plz leave blank if you don't have the address. ")
    AddingToBook.create_new_contact(name, phone, email, address)


import eosfactory.core.setup as setup
import eosfactory.core.manager as manager
import eosfactory.core.logger as logger

class Testnet:
    def __init__(
            self, url=None,
            account_name=None, owner_key=None, active_key=None,
            reset=False):

        if not url:
            if reset:
                manager.reset(verbosity=[logger.Verbosity.ERROR])
            else:
                manager.resume(verbosity=[logger.Verbosity.ERROR])
            import eosfactory.shell.account as account
            eosio = account.Eosio("account_master")
            setup.is_local_address = True
            account_name = eosio.name
            owner_key = eosio.owner_key.key_private
            active_key = eosio.active_key.key_private

        if not account_name or not owner_key or not active_key:
            logger.ERROR('''
        If the ``url`` is set, the ``account_name`` and keys have to be set, as well.
            ''')
        self.url = url
        self.account_name = account_name
        self.owner_key = owner_key
        self.active_key = active_key

    def configure(self, prefix=None):
        setup.set_nodeos_address(self.url, prefix)

    def verify_production(self):
        return manager.verify_testnet_production()

    def clear_cache(self):
        manager.clear_testnet_cache()

    def is_local(self):
        return manager.is_local_testnet()

def get_testnet(alias, testnet=None, reset=False):
    if not alias and not testnet:
        return Testnet(reset=reset)

    if alias:
        mapping = get_mapping()
        if alias in mapping:
            return Testnet(
                mapping[alias]["url"], mapping[alias]["account_name"],
                mapping[alias]["owner_key"], mapping[alias]["active_key"])
        elif alias == "JUNGLE":
            return JUNGLE
        elif alias == "KYLIN":
            return KYLIN
        else:
            logger.ERROR('''
            Testnet ``{}`` is not defined in the testnet mapping.
            '''.format(alias))
    elif testnet:
        return Testnet(testnet[0], testnet[1], testnet[2], testnet[3])

    logger.ERROR('''
        Cannot determine testnet.
        ''')

TESTNET_FILE = "testnet.json"

def get_mapping():
    return manager.read_map(TESTNET_FILE)

def save_mapping(mapping):
    manager.save_map(mapping, TESTNET_FILE)

def edit_mapping():
    manager.edit_map(TESTNET_FILE)

def add_to_mapping(url, account_name, owner_key, active_key, alias=None):
    mapping = get_mapping()
    testnet = {}
    testnet["url"] = url
    testnet["account_name"] = account_name
    testnet["owner_key"] = owner_key
    testnet["active_key"] = active_key
    if not alias:
        alias = setup.url_prefix(url)
    mapping[alias] = testnet
    save_mapping(mapping)

def remove_from_mapping(testnet):
    mapping = get_mapping()
    if testnet in mapping:
        del mapping[testnet]
        save_mapping(mapping)

def testnets():
    mapping = get_mapping()
    if not mapping:
        logger.INFO('''
        Testnet mapping is empty.
        ''')
        return
    for alias, testnet in mapping.items():
        print("%25s: %13s @ %s" % (alias, testnet["account_name"], testnet["url"]))

JUNGLE = Testnet(
    "http://145.239.133.201:8888",
    "dgxo1uyhoytn",
    "5JE9XSurh4Bmdw8Ynz72Eh6ZCKrxf63SmQWKrYJSXf1dEnoiKFY",
    "5JgLo7jZhmY4huDNXwExmaWQJqyS1hGZrnSjECcpWwGU25Ym8tA"
)

KYLIN = Testnet(
    "http://145.239.133.201:9999",
    "xlg3pao3idlq",
    "5JBbCwe3t6j63yerYmguRVWg7ZVDY3nKXzGYMwkR9y5w4appKhk",
    "5JYZU9xPS54NhnJrmgQWzVXxZCWpzsVUPS3SBZVZnsPUBFtV5YK"
)

# /mnt/d/Workspaces/EOS/eos/build/programs/cleos/cleos --url http://145.239.133.201:8888 get info
# /mnt/d/Workspaces/EOS/eos/build/programs/cleos/cleos --url http://145.239.133.201:9999 get info