from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from mqtt import *
import json

class SubWindow(QWidget):
    def __init__(self, parent = None):
        super(SubWindow, self).__init__(parent)
        self.setMinimumSize(QSize(300, 200))
        label = QLabel("Motorized galvo wheel",  self)
        
        self.Button = QtWidgets.QPushButton("ON",self)        
        self.Button.clicked.connect(self.on)        
        self.Button.setGeometry(QtCore.QRect(10, 40, 81, 23))

        self.Button2 = QtWidgets.QPushButton("OFF",self)  
        self.Button2.clicked.connect(self.off)              
        self.Button2.setGeometry(QtCore.QRect(10, 40, 81, 23))
        self.Button2.move(120,40)

        self.fileName_UI = "motorized galvo wheel"

    def on(self):
        client = device("Motorized galvo wheel")
        client.run()

        client.loop_start()
        uninitialised_state = "Uninitialised"
        time.sleep(2)
        print("Subscribing to topic", "microscope/light_sheet_microscope/UI/state")        
        client.subscribe("microscope/light_sheet_microscope/UI/state")
        print("Publishing message to topic", "microscope/light_sheet_microscope/UI/state")  
        client.publish("microscope/light_sheet_microscope/UI/state", json.dumps({"type": "device", "payload":{"name": "motorized galvo wheel", "state": uninitialised_state}}))
        time.sleep(1)
        client.loop_stop()  # stop the loop
        print("Current state = " + uninitialised_state + "\n")

        client.loop_start()
        initialising_state = "Initialising"
        time.sleep(2)
        print("Subscribing to topic", "microscope/light_sheet_microscope/UI/state")        
        client.subscribe("microscope/light_sheet_microscope/UI/state")
        print("Publishing message to topic", "microscope/light_sheet_microscope/UI/state") 
        client.publish("microscope/light_sheet_microscope/UI/state", json.dumps({"type": "device", "payload":{"name": "motorized galvo wheel", "state": initialising_state}}))  
        time.sleep(1)
        client.loop_stop()  # stop the loop
        print("Current state = " + initialising_state + "\n")

        client.loop_start()
        initialised_state = "Initialised"
        time.sleep(2)
        print("Subscribing to topic", "microscope/light_sheet_microscope/UI/state")        
        client.subscribe("microscope/light_sheet_microscope/UI/state")
        print("Publishing message to topic", "microscope/light_sheet_microscope/UI/state", 
        client.publish("microscope/light_sheet_microscope/UI/state", json.dumps({"type": "device", "payload":{"name": "motorized galvo wheel", "state": initialised_state}})))  
        time.sleep(1)
        client.loop_stop()  # stop the loop
        print("Current state = " + initialised_state + "\n")

        client.loop_start()
        configuring_state = "Configuring"
        time.sleep(2)
        print("Subscribing to topic", "microscope/light_sheet_microscope/UI/state")        
        client.subscribe("microscope/light_sheet_microscope/UI/state")
        print("Publishing message to topic", "microscope/light_sheet_microscope/UI/state") 
        client.publish("microscope/light_sheet_microscope/UI/state", json.dumps({"type": "device", "payload":{"name": "motorized galvo wheel", "state": configuring_state}}))  
        time.sleep(1)
        client.loop_stop()  # stop the loop
        print("Current state = " + configuring_state + "\n")

        client.loop_start()
        configured_state = "Configured"
        time.sleep(2)
        print("Subscribing to topic", "microscope/light_sheet_microscope/UI/state")        
        client.subscribe("microscope/light_sheet_microscope/UI/state")
        print("Publishing message to topic", "microscope/light_sheet_microscope/UI/state") 
        client.publish("microscope/light_sheet_microscope/UI/state", json.dumps({"type": "device", "payload":{"name": "motorized galvo wheel", "state": configured_state}}))  
        time.sleep(1)
        client.loop_stop()  # stop the loop
        print("Current state = " + configured_state + "\n")

        client.loop_start()
        run_state = "run"
        time.sleep(2)
        print("Subscribing to topic", "microscope/light_sheet_microscope/UI/state")        
        client.subscribe("microscope/light_sheet_microscope/UI/state")
        print("Publishing message to topic", "microscope/light_sheet_microscope/UI/state") 
        client.publish("microscope/light_sheet_microscope/UI/state", json.dumps({"type": "device", "payload":{"name": "motorized galvo wheel", "state": run_state}}))  
        time.sleep(1)
        client.loop_stop()  # stop the loop
        print("Current state = " + run_state + "\n")
        
        self.Button.setStyleSheet("background-color: green")
        print("Hello World! Im a motorized galvo wheel")
        
        client.loop_start()
        time.sleep(1)
        print("Subscribing to topic", "microscope/light_sheet_microscope/UI/list_of_devices")
        client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices")
        print("Publishing message to topic", "microscope/light_sheet_microscope/UI/list_of_devices")
        client.publish("microscope/light_sheet_microscope/UI/list_of_devices", self.fileName_UI + " added to device list")
        time.sleep(1)
        client.loop_stop()

        listofdevices = []
        listofdevices.append(self.fileName_UI)
        with open("list_of_devices_currently_active.txt", "a+") as myfile:
            for item in listofdevices:
                myfile.write(item + ",")
        print(item + " added to device list" + "\n" + "\n" + "List of devices currently active")

        def readFile(fname):
            try:
                with open(fname, "r") as f:
                    for item in f:
                        print("List of devices currently active: " + item + "\n" )
            except:
                print("No devices added yet")
        readFile("list_of_devices_currently_active.txt")

    def off(self):
        client = device("Motorized galvo wheel")
        client.run()

        client.loop_start()
        time.sleep(2)
        print("Subscribing to topic", "microscope/light_sheet_microscope/UI/state")        
        client.subscribe("microscope/light_sheet_microscope/UI/state")
        print("Publishing message to topic", "microscope/light_sheet_microscope/UI/state", json.dumps({"type": "device", "payload":{"name": "motorized galvo wheel", "state": "turning off"}}))
        print("Device turned off")
        self.Button.setStyleSheet("background-color:")
        
        with open("list_of_devices_currently_active.txt", "r") as f:
            lines = f.readlines()

        with open("list_of_devices_currently_active.txt", "w") as f:
            for line in lines:
                if line.strip("\n") != "motorized galvo wheel":
                    f.write(line)
        print("Motorized galvo wheel removed from device list" "\n\n" + "List of devices currently active")

        def readFile(fname):
            try:
                with open(fname, "r") as f:
                    for item in f:
                        print(item)
            except:
                print("No devices added yet")
        readFile("list_of_devices_currently_active.txt")

    def closeEvent(self, event):
        self.close()