import sys
import os

from PySide2.QtCore import Qt, QObject, Signal, Slot, Property
from PySide2.QtWidgets import QApplication, QSystemTrayIcon, QStyle, QAction, QMenu, QMessageBox
from PySide2.QtQml import QQmlApplicationEngine

my_list = ['here','is','my','list']

class Manager(QObject):
    stationsChanged = Signal()
    def __init__(self):
        QObject.__init__(self)
        self.m_stations =[]

    @Property('QVariantList', notify=stationsChanged)
    def stations(self):
        return self.m_stations

    @stations.setter
    def set_stations(self, val):
        if self.m_stations == val:
            return
        self.m_stations = val[:]
        self.stationsChanged.emit()   

    def list_fill(self):
        self.stations = my_list

class SystemTrayIconManager(QObject):
    def __init__(self, window, parent=None):
        QObject.__init__(self, parent)
        self.window = window
        self.window.closing.connect(self.onClosing)
        self.tray_icon = QSystemTrayIcon(self)
        self.tray_icon.setIcon(qApp.style().standardIcon(QStyle.SP_MediaPlay))
        show_action = QAction("Show", self)
        quit_action = QAction("Exit", self)
        hide_action = QAction("Hide", self)
        minimize_action = QAction("Minimize", self)
        show_action.triggered.connect(self.window.show)
        hide_action.triggered.connect(self.window.hide)
        quit_action.triggered.connect(qApp.quit)
        minimize_action.triggered.connect(self.minimize)
        tray_menu = QMenu()
        tray_menu.addAction(show_action)
        tray_menu.addAction(hide_action)
        tray_menu.addAction(quit_action)
        tray_menu.addAction(minimize_action)
        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()

    def onClosing(self):
        if self.tray_icon.isVisible():
            QMessageBox.information(None, "Systray",
                    "The program will keep running in the system tray. To "
                    "terminate the program, choose <b>Quit</b> in the "
                    "context menu of the system tray entry.")
            self.window.hide()

    def minimize(self):
        self.window.hide()
        self.tray_icon.showMessage(
                "Tray Program",
                "Application was minimized to Tray",
                QSystemTrayIcon.Information,
                2000
            ) 


if __name__ == "__main__":
    os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
    app = QApplication(sys.argv)

    if not QSystemTrayIcon.isSystemTrayAvailable():
        QMessageBox.critical(None, "Systray",
                "I couldn't detect any system tray on this system.")
        sys.exit(1)

    QApplication.setQuitOnLastWindowClosed(False)
    engine = QQmlApplicationEngine()
    manager = Manager()
    ctx = engine.rootContext()
    ctx.setContextProperty("Manager", manager)
    engine.load('main.qml')

    if not engine.rootObjects():
        sys.exit(-1)
    m = SystemTrayIconManager(engine.rootObjects()[0])
    manager.list_fill()
    sys.exit(app.exec_())