import pyqtgraph as pg

from src.utils import read_json, DiagramParser
from src.uml import Request, UMLCreator
from src.OptAlgorithm import OptAlgorithm
from src.gui import Plotter, settingsWindow


class tabWidgetGenerator:
    def __init__(self, directory_to_save):
        self.UMLgenerator = Request(server_url='http://www.plantuml.com/plantuml/svg/')
        self.uml_creator = UMLCreator(request_generator=self.UMLgenerator, path_to_save=directory_to_save)
        self.operator_params = read_json("params/operator_params.json")
        self.system_params = read_json("params/system_params.json")
        self.operSettings = settingsWindow("params\operator_params.json", 'Operator', self._update)
        self.sysSettings = settingsWindow("params\system_params.json", 'System', self._update)

        self.paths = []
        self.plotters = []
        self.bool_dicts = []
        self.float_dicts = []
        self.timings_dicts = []
        self.modes = []
        self.names = []

    def get_widget(self, path):
        self.paths.append(path) 
        self.plotters.append(Plotter(show_settings_func=self._show_settings)) 
        self._getParsedData(path)
        self._update()
        return self.plotters[-1].widget
    
    def _get_ideal_timings(self, opt: OptAlgorithm) -> list[float]:
        data = opt.Ts
        ideal_time = [data['tclose'], data['tgrow'], opt.getMarkOpen(), data["tmovement"]]
        return ideal_time

    def _getParsedData(self, path):
        self.names.append(path)
        parser = DiagramParser(system_config=self.system_params)
        parser.setData(path)
        self.bool_dicts.append(parser.getBoolDict())
        self.float_dicts.append(parser.getFloatDict())
        self.timings_dicts.append(parser.getRealTimings())
        self.modes.append(parser.getMode())
        

    def _update(self, _ = None):
        self.operator_params = self.operSettings.getParams()
        self.system_params = self.sysSettings.getParams()

        opt_algorithm = OptAlgorithm(operator_config=self.operator_params, system_config=self.system_params)
        ideal_times = self._get_ideal_timings(opt_algorithm)
        
        for i in range (len(self.plotters)):
            self.plotters[i].update_data(operator_config=self.operator_params,
                        system_config=self.system_params,
                        opt=opt_algorithm,
                        bool_dict=self.bool_dicts[i],
                        ideal_time=ideal_times,
                        float_dict=self.float_dicts[i],
                        mode = self.modes[i],
                        timings_dict=self.timings_dicts[i])

        self.uml_creator.update_uml(operator_config=self.operator_params,
                                    system_config=self.system_params,
                                ideal_time=ideal_times,
                                bool_dict=self.bool_dicts[i],
                                float_dict=self.float_dicts[i],
                                mode = self.modes[i], 
                                timings_dict=self.timings_dicts[i],
                                name = self.names[i])
        
        
    def _show_settings(self):
        self.operSettings.show()
        self.sysSettings.show()


if __name__ == '__main__':
    pg.mkQApp("Plotting")
    temp = tabWidgetGenerator()
    widget = temp.get_widget("trace_samples/2024_11_08-19_30_49.csv")
    widget.show()
    pg.exec()