"""
Name: interactive_terminal
Title: Interactive Terminal
Author: Cooper
Date: 06/07/2021

Desc:  Script that allows you to poke certain things like database loading etc, especially helpful in a remote situation
where there is no physical access to the console

"""
import os
import time
import subprocess

from hanip.onionip import hano1

def print_main_menu():
    menu_options = [
        "start: Starts hanip",
        "stop: Stops hanip",
        " ",
        "v: Get console fw version",
        "b: Load database (B Block)",
        "c: Load database (C Block)",
        "z?: Get route/dest code",
        "z: Set route/dest code",
        "t: Toggle test mode",
        "o: Poll console",
        "p: Reboot console"
    ]

    print("\nPlease select from the following")
    for option in menu_options:
        if option[0] == "b":
            print("\tNote: b and c Will load /tmp/eric.bin")
        print("\t" + option)

def hanip_service(state):
    """
    Allows the starting/stopping of other hanip background service
    """
    if state:
        command = ["/etc/init.d/hanip", "start"]
    else:
        command = ["/etc/init.d/hanip", "stop"]

    process = subprocess.Popen(command, stdout=subprocess.PIPE)
    output, error = process.communicate()

    return output.decode("latin-1")


def get_console_version():
    ver = hano.getSoftwareVersion()
    print(ver)

def load_database_C():
    hano.send_file(r"/tmp/eric.bin", "D", "C")

def load_database_B():
    hano.send_file(r"/tmp/eric.bin", "D", "B")

def change_dest_code():
    route = input("Enter route: ")
    route = route.rstrip().zfill(4)

    dest = input("Enter dest: ")
    dest = dest.zfill(4)

    hano.setRouteCode(route)
    hano.setDestCode(dest)

def get_dest_code():
    codes = hano.getDestCode()
    print("R: %s" % codes[1])
    print("D: %s" % codes[0])
    print("T: %s" % codes[2])

testmode = True

def toggle_test_mode():
    global testmode

    if testmode:
        hano.transmitMessage("t1", True, 0)
    else:
        hano.transmitMessage("t2", True, 0)

    testmode = not testmode

def reboot_console():
    hano.transmitMessage("!", True, 0)

def poll_console():
    hano.poll_console()

def run():
    while 1:
        print_main_menu()
        choice = input(">: ")

        if choice == "v":
            get_console_version()
        elif choice == "c":
            load_database_C()
        elif choice == "b":
            load_database_B()
        elif choice == "z?":
            get_dest_code()
        elif choice == "z":
            change_dest_code()
        elif choice == "t":
            toggle_test_mode()
        elif choice == "o":
            hano.poll_console()
        elif choice == "r":
            reboot_console()
        elif choice == "start":
            hanip_service(True)
        elif choice == "stop":
            hanip_service(False)
        else:
            pass


comport = "/dev/ttyS1"
baud = 115200

database_dir = "/usr/share/"

print("Ensure the hanip application isnt running, unless of course you want it to.")
print("Loading module(s)")
hano = hano1.HANO1(comport, baud, True, database_dir)
hano.initSerial()

if __name__ == "__main__":
    run()





