"""
Name: prod_mode
Title: 
Author: Cooper
Date: 10/10/2019

Desc:  This is the script for the production mode

No longer required!

"""
import time
from hanip.onionip import hano1
from hanip.onionip import xferFileHandler

app_version = "-"
desc = "Production mode.  Version %s" % app_version

print(desc)

class ProductionMode(object):
    def __init__(self, config_dict, hw_dict, conf_dir, data_dir):
        self.debug = False

        self.config_dict = config_dict
        self.hw_dict = hw_dict

        self.conf_dir = conf_dir
        self.data_dir = data_dir

        self.host_comport  = self.config_dict["SERIAL_unix_comport"]
        self.rs485_comport = self.config_dict["SERIAL_rs485_comport"]
        self.hano1 = hano1.HANO1(self.host_comport, self.config_dict["SERIAL_baudrate_host_console"], True, self.data_dir)

        self.xfer_handler = xferFileHandler.XferFileHandler(self.conf_dir, self.data_dir)

    def show_terminal_status(self):
        self.hano1.clear_terminal()
        # self.hano1.show_on_terminal("0", "C", "Onion Terminal")
        # time.sleep(1)

        self.hano1.show_on_terminal("0", "L", "Onion ver: %s %s" %
                                    (self.hw_dict["onion_ver"], self.config_dict["MODE_service_mode"]))
        self.hano1.show_on_terminal("1", "L", "DG3 ver: %s" % self.hw_dict["software_version"])
        self.hano1.show_on_terminal("2", "L", "IP: %s" % self.hw_dict["unit_IP"])

        time.sleep(5)
        self.hano1.end_terminal_mode()

    def process_updated_files(self):
        restartApp = False

        if self.xfer_handler.newEric:
            restartApp = False
            self.xfer_handler.newEric = False

            # if self.ftp_update:
            #     self.hano1.showOnConsole("Transferring database to DG3", 0, 2)
            #     if self.config_dict["FTP_hano1_enable_legacy_block"]:
            #         print("Using C type block")
            #         self.hano1.sendEric("C")
            #     else:
            #         print("Using B type block")
            #         self.hano1.sendEric("B")
            #     self.ftp_update = False


        if self.xfer_handler.newFont:
            print("New font")
            restartApp = False
            self.xfer_handler.newFont = False

        if self.xfer_handler.newConf:
            print("New configuration")
            restartApp = True
            self.xfer_handler.newConf = False

        if self.xfer_handler.newSerial:
            print("New serial data")
            restartApp = False
            self.xfer_handler.newSerial = False

        if self.xfer_handler.newScirpts:
            print("New application files")
            restartApp = True
            self.xfer_handler.newConf = False

        return restartApp

    def check_for_payload(self):
        #Check USB payload
        status = self.hano1.poll_console()

        if status == 0:
            if not self.xfer_handler.unzipPayload("xfer.zip"):
                self.xfer_handler.discoverFiles()
            else:
                return False

            return self.process_updated_files()

        elif status == 2:
            self.show_terminal_status()

    def run(self):
        while 1:
            if self.check_for_payload():
                self.hano1.showOnConsole("New config, rebooting", 0, 3)
                return  #Causes the application to restart itself

            self.hano1.showOnConsole("Onion App ver: %s" % self.hw_dict["onion_ver"], 0, 3)
            time.sleep(1)
            self.hano1.showOnConsole("Please load configuration", 1, 3)




if __name__ == "__main__":
    pass