#!/usr/bin/env python3

""" Experimental tool to upload a microapp to a Crownstone. """

import asyncio
import logging
from os import path

from bluenet_logs import BluenetLogs
from crownstone_ble import CrownstoneBle
from crownstone_uart import CrownstoneUart

from tools.util.config import getToolConfig, loadKeysFromConfig, setupDefaultCommandLineArguments, macFilterPassed

tool_version = "1.0.0"

argParser = setupDefaultCommandLineArguments('Enable a microapp on a Crownstone.')
argParser.add_argument('-a', '--bleAddress',
                       default=None,
                       help='To upload via BLE: the MAC address/handle of the Crownstone you want to connect to. Example: 11:22:33:AA:BB:CC')
argParser.add_argument('-d', '--device',
                       default=None,
                       help='The UART device of the Crownstone you want to connect to. Example: /dev/ttyACM0')
argParser.add_argument('-D', '--disable',
                       action='store_true',
                       help='Disable the microapp instead of enable.')
argParser.add_argument('-i', '--crownstoneId',
                       required=False,
                       default=None,
                       type=int,
                       help='Send the command via the mesh to this Crownstone ID.')
argParser.add_argument('--verbose', '-v',
                       dest="verbose",
                       action='store_true',
                       help='Show verbose output')
argParser.add_argument('--logStringsFile', '-l',
                       default="",
                       dest='logStringsFileName',
                       metavar='path',
                       type=str,
                       help='The path of the file with the extracted firmware logs on your system.')

try:
    file_path = path.dirname(path.realpath(__file__))
    [tool_config, args] = getToolConfig(file_path, argParser)
except Exception as e:
    print("ERROR", e)
    quit()

if args.verbose:
    logging.basicConfig(format='%(asctime)s %(levelname)-7s: %(message)s', level=logging.DEBUG)

core = None
bluenetLogs = None
useBle = False

if args.bleAddress is not None:
    # Use BLE for communication.
    useBle = True

    # Create the library instance.
    print(f'Initializing tool with bleAdapterAddress={tool_config["bleAdapterAddress"]}')
    core = CrownstoneBle(bleAdapterAddress=tool_config["bleAdapterAddress"])

    # Load the encryption keys into the library.
    loadKeysFromConfig(core, tool_config)

else:
    # Use UART for communication.

    # Create the library instance.
    print(f'Initializing tool with device={args.device}')
    core = CrownstoneUart()
    core.initialize_usb_sync(port=args.device)

    if args.logStringsFileName:
        bluenetLogs = BluenetLogs()
        bluenetLogs.setLogStringsFile(args.logStringsFileName)

# The index where we want to put our microapp.
appIndex = 0

# The microapp upload protocol version
protocol = 1

async def enable():
    print("Enable..")
    await core.microapp.enableMicroapp(appIndex, protocol, args.crownstoneId)
    print("Enable done")

async def disable():
    print("Disable..")
    await core.microapp.disableMicroapp(appIndex, protocol, args.crownstoneId)
    print("Disable done")

async def main():
    if useBle:
        await core.connect(args.bleAddress)

    if args.disable:
        await disable()
    else:
        await enable()

    if useBle:
        await core.disconnect()
        await core.shutDown()

    print("Done!")

try:
    # asyncio.run does not work here.
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
except KeyboardInterrupt:
    print("Stopping")

if not useBle:
    core.stop()