import sys

import pyqtgraph as pg
from PyQt5 import QtWidgets

from gui.main_gui import MainWindow
from controller.file_manager import DirectoryMonitor, FileManager
from controller.mediator import Mediator
from controller.converter import DataConverter
from gui.plotter import PlotWidget
from controller.controller import Controller
from controller.passport_former import PassportFormer
from performance.roboter import TraceProcessor


def main():
    pg.setConfigOptions(useOpenGL=False, antialias=False)
    app = QtWidgets.QApplication(sys.argv)
    monitor = DirectoryMonitor()
    file_manager = FileManager(monitor=monitor)
    data_converter = DataConverter()
    controller = Controller(file_manager=file_manager)
    plot_widget_builder = PlotWidget(controller=controller)
    passport_former = PassportFormer()
    window = MainWindow()
    trace_processor = TraceProcessor()
    mediator = Mediator(data_converter, passport_former, plot_widget_builder, controller, file_manager, trace_processor)
    window.show()

    window.signal_mode.connect(controller.set_working_mode)
    window.signal_settings.connect(controller.update_settings)
    window.signal_replot_all.connect(controller.update_plots)
    window.signal_open_file.connect(controller.open_file)
    window.signal_open_dir.connect(controller.open_dir)
    window.signal_save_file.connect(controller.save_file)
    window.signal_TCW_for_client.connect(controller.build_TCW_for_client)

    controller.signal_widgets.connect(window.show_plot_tabs)
    controller.signal_progress_bar.connect(window.status_widget.set_progress)
    controller.signal_status_text.connect(window.status_widget.set_note)

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
