"""
Name: passthrough_mode
Title: Passthrough mode
Author: Cooper
Date: 27/02/2023

Desc:  Not really sure why this mode is needed, but whatever...

The purpose of this mode is to obtain any sign data from the console and then throw it out the Onion's serial port

"""
import time
import _thread

from hanip.onionip.console import sign_manager
from hanip.onionip.console import console_task

class PassthroughMode(object):
    def __init__(self, config_dict, hw_dict, conf_dir, data_dir):
        self.config_dict = config_dict
        self.hw_dict = hw_dict
        self.config_dir = conf_dir
        self.data_dir = data_dir

        self.debug = False

    def setup_sign_manager(self):
        """
        Initialises the serial port for communicating with signs
        :return:
        """
        self.sign_manager = sign_manager.SignManagerConsole(self.config_dict, self.data_dir)

    def setup_console_task(self):
        """

        :return:
        """
        self.console_task = console_task.ConsoleTask(self.config_dict, self.hw_dict, self.config_dir, self.data_dir)

    def check_update_state(self):
        """

        """
        if self.console_task.update_flags["config"]:
            return 1
        else:
            return 0

    def run(self):
        self.setup_console_task()

        sign_speed = self.console_task.hano.get_parameter_value("SS")
        self.config_dict["SERIAL_baudrate_rs485"] = sign_speed

        self.setup_sign_manager()

        _thread.start_new_thread(self.console_task.run_sign_data_mode, ())
        _thread.start_new_thread(self.sign_manager.run, ())

        while 1:
            if self.debug:
                print("sign map: ", self.console_task.sign_mapping_table)
                print("sign_messages: ", self.console_task.sign_messages)

            if self.console_task.test_mode:
                self.sign_manager.test_mode = True
            else:
                self.sign_manager.test_mode = False

            #Grab route number for HMF6
            #TODO check this works
            self.sign_manager.hmf6_route_number = self.console_task.route_code

            sign_data = self.console_task.sign_messages
            self.sign_manager.update_data_from_console_task(sign_data)

            if self.check_update_state():
                return
            if self.console_task.stop:
                return

            time.sleep(1)

if __name__ == "__main__":
    pass
