"""
Name: sl_mqtt
Title: 
Author: Cooper
Date: 04/10/2023

Desc: ITxPT config and support for Storstockholms Lokaltrafik, SL (Stockholm PTA) who have created their own variant of
MQTT along with Keolis and Nobina

This is tempoary to get the demo done, but at some point would want to integrate it with the main itxpt module.

"""
import _thread

from hanip.itxpt import mqtt_client
from hanip.itxpt import ecoMonitor
from hanip.itxpt import mqtt_payload_parser
from hanip.itxpt import module_inventory_service
from hanip.onionip.sign import sign_task

class SLMQTT():
    def __init__(self, config_dict, hw_dict, conf_dir, data_dir):
        self.config_dict = config_dict
        self.hw_dict = hw_dict
        self.conf_dir = conf_dir
        self.data_dir = data_dir

        self.mqtt_parser = mqtt_payload_parser.MQTTPayloadParser(self.config_dict)

    def start_module_inventory_service(self):
        """
        Starts the module inventory service
        """
        self.mis = module_inventory_service.ModuleInventoryService(self.config_dict, self.hw_dict)
        self.mis.run()

    def start_eco_monitor(self):
        """

        """

    def start_sign_task(self):
        """
        Initiates sign task and runs it
        """
        self.sign_task = sign_task.SignTask(self.config_dict, self.hw_dict, self.conf_dir, self.data_dir)
        _thread.start_new_thread(self.sign_task.run, ())

    def setup_mqtt_client(self):
        """
        Sets up the MQTT client
        """
        mqtt_conn_dict = {
            "discover": self.config_dict.get("MQTT_discover", True),
            "timeout": self.config_dict.get("MQTT_timeout", 3),
            "service_type": self.config_dict.get("MQTT_service_type", "_mqtt._tcp.local."),
            "hostname": self.config_dict.get("MQTT_hostname", "Han_con"),
            "fallback_address": self.config_dict.get("MQTT_fallback_address", "192.168.1.2"),
            "broker_topic": self.config_dict.get("MQTT_broker_topic", "dpi/destination_display_text/v1"),
            "reply_topic": self.config_dict.get("MQTT_reply_topic", "infohub/dpi/sign/response/#/json"),
            "status_topic": self.config_dict.get("MQTT_status_topic", "infohub/dpi/sign/status/#/json"),
            "address": self.hw_dict["address"],
            "serial": self.hw_dict["serial_number"],
            "mac": self.hw_dict["unit_MAC"].replace(":", "")
        }

        self.mqttc = mqtt_client.MQTTConnectionHandler("SL", mqtt_conn_dict)
        _thread.start_new_thread(self.mqttc.run, ())

    def parse_payload(self, payload):
        """
        Parses the payload from CRDI, what does it mean?  I think Tom Day knows.
        """
        sign_dict = self.mqtt_parser.parse_storstockholms_payload(payload)

        return sign_dict

    def update_sign_task(self, display_dict):
        """

        """
        self.sign_task.update_data_dict(display_dict)


    def run(self):
        """
        Main loop
        """
        self.start_sign_task()
        self.setup_mqtt_client()

        while 1:
            #TODO check ecomon

            if self.mqttc.new_payload:
                self.mqttc.new_payload = False

                parsed_payload = self.parse_payload(self.mqttc.current_payload)
                self.update_sign_task(parsed_payload[0])



if __name__ == "__main__":
    pass
