"""
This is the entry point to the entire program.
It contains a few global variables that the whole
program has access to so that management of
I/O is done properly.

| author     = "Seth Miers"
| copyright  = "Copyright, Xilinx Inc. All rights reserved."
| credits    = ["Seth Miers"]
| license    = "GPL"
| version    = "3.0"
| maintainer = "Seth Miers"
| email      = "sethm@xilinx.com"
| status     = "Development"


"""

##########################################
#               Imports                  #
##########################################

# --------------Packages---------------- #
import logging    # Used as the global logger
import argparse   # Used to parse args
# import threading  # to start separate threads

# --------------Local------------------- #
import serialmanager
import filemanager
# pylint: disable=cyclic-import
# pylint: disable=fixme
# Todo: remove the cyclic import at another time
import step
# import communicationpipe

##########################################
#          GLOBAL VARIABLES              #
##########################################

VERSION = 'v1.02.4'

#: this variable is used for the logger
LOGGER = logging.getLogger("BoardFramework.Commander")
SERIAL_MANAGER = serialmanager.SerialManager(LOGGER)
FILE_MANAGER = filemanager.FileManager(LOGGER)
# IIC_MANAGER = iicmanager.IicManager(LOGGER)
# ETHERNET_MANAGER = ethernetmanager.EthernetManager(LOGGER)

##########################################
#              Main Code                 #
##########################################


def serial_to_iic_bridge():
    '''
    Runs the most basic SerialToIicBridge, which has
    been depricated and is no longer in development,
    but was implemented to smooth the transition from
    the old BoardUI and Msp/Zynq System Controller
    to the Versal devices.

    Attributes:
        runonboot (FileComm): The file that accesses
            the non-volatile runonboot space. Note, this
            space will not work if switching between
            the BoardFramework and the SerailToIicBridge,
            How the file gets written to is not compatible
            between both.
        bridge (Step): Stores the class for running the
            SerialToIicBridge.

    TODO:
        * Make runonboot compatible between both BoardFramework
          and the SerialToIicBridge
    '''
    runonboot = FILE_MANAGER.get_file_comm('runonboot')
    runonboot_list = runonboot.get_keys()
    for entry in runonboot_list:
        bridge = step.SerialToIicBridge(command=runonboot.get(entry),
                                        port_regex="/dev/ttyPS0")
        bridge.start_step()
        bridge.run_step()
    bridge = step.SerialToIicBridge(port_regex="/dev/ttyPS0")
    bridge.start_step()
    bridge.run_step()


if __name__ == "__main__":
    # pylint: disable=pointless-string-statement
    '''
    Create a parser to pull out the arguments
    that were used to
    start the program
    '''
    PARSER = argparse.ArgumentParser(description='Run the BoardFramework.')
    PARSER.add_argument('-b', '--bridge', action='store_true',
                        help='When this is set, only the \'SerialToIicBridge\' \
                        is run.')
    ARGS = vars(PARSER.parse_args(['--bridge']))

    '''
    Determine what to do based on those arguments.
    '''
    if ARGS.get('bridge'):
        serial_to_iic_bridge()
    else:
        print("BoardFramework not yet fully implemented, \
              please use in bridge mode")
        # communication_pipe = communicationpipe.CommunicationPipe('both',
        #                                                          logging.INFO)
        # communication_pipe_thread = \
        #     threading.Thread(target=communication_pipe.start_communication_pipe,
        #                      name='communication_pipe_thread')
        # communication_pipe_thread.setDaemon(True)
        # communication_pipe_thread.start()
