import lyse
import numpy as np
from PyQt5.QtCore import QTimer

from HelperClasses.Plotting import WidgetFakeColorPlot, MainPlotPanel, Widget1DLivingPlot
from HelperClasses import DyTransition, Camera, AbsorptionImaging

import cProfile
import pstats


class FigureManager:
    def __init__(self):
        self.MainPlotPanel = None
        self.timer = None
        self.h5_paths = np.empty(0)

    def refresh_h5_path(self):
        with open(r'D:\Jianshun Gao\Simulations\DyLab\publishedPackage\dylab\h5_file_path.ini', 'r') as h5_path_file:
            h5_path_new = np.array(h5_path_file.readlines())

        if len(h5_path_new) == len(self.h5_paths) + 1:

            if len(h5_path_new) == 1 or (h5_path_new[:len(h5_path_new) - 1] == self.h5_paths).any():

                h5_path = h5_path_new[len(h5_path_new) - 1]
                self.MainPlotPanel.add_h5_path(h5_path)
                self.h5_paths = np.append(self.h5_paths, h5_path)
                self.MainPlotPanel.refresh(h5_path)

        elif len(h5_path_new) == len(self.h5_paths):

            if not (self.h5_paths == h5_path_new).any():

                self.h5_paths = h5_path_new
                self.MainPlotPanel.update_h5_paths(h5_path_new)
                self.MainPlotPanel.refresh(h5_path_new[len(h5_path_new) - 1])

        else:
            self.h5_paths = h5_path_new
            self.MainPlotPanel.update_h5_paths(h5_path_new)
            self.MainPlotPanel.refresh(h5_path_new[len(h5_path_new) - 1])

    def start_living_plot(self, h5_paths):

        self.MainPlotPanel = MainPlotPanel.MainPlotPanel(h5_paths)

        plot_name = '3D-MOT Absorption Imaging - With Atoms'
        if not plot_name in self.MainPlotPanel.plots:
            current_plotting = WidgetFakeColorPlot.WidgetFakeColorPlot(plot_name + '1',
                                                                       data_path=r"images/MOT_3D_Camera/in_situ_absorption/atoms",
                                                                       Roi=False, colorbarText=False)
            self.MainPlotPanel.add_plot_dock(plot_name + '1', current_plotting)

        plot_name = '3D-MOT Absorption Imaging - Without Atoms'
        if not plot_name in self.MainPlotPanel.plots:
            current_plotting = WidgetFakeColorPlot.WidgetFakeColorPlot(plot_name + '1',
                                                                       data_path=r"images/MOT_3D_Camera/in_situ_absorption/background",
                                                                       Roi=False, colorbarText=False)
            self.MainPlotPanel.add_plot_dock(plot_name + '1', current_plotting)

        plot_name = '3D-MOT Absorption Imaging - Dark'
        if not plot_name in self.MainPlotPanel.plots:
            current_plotting = WidgetFakeColorPlot.WidgetFakeColorPlot(plot_name + '1',
                                                                       data_path=r"images/MOT_3D_Camera/in_situ_absorption/dark",
                                                                       Roi=False, colorbarText=False)
            self.MainPlotPanel.add_plot_dock(plot_name + '1', current_plotting)

        plot_name = r'3D-MOT Absorption Imaging'
        if not plot_name in self.MainPlotPanel.plots:
            current_plotting = WidgetFakeColorPlot.WidgetFakeColorPlot(plot_name,
                                                                       data_path=r"results/absorption_imaging/absorption_imaging")

            self.MainPlotPanel.add_plot_dock(plot_name, current_plotting)

            absorption_imaging_transition = DyTransition.creat_Dy421()
            mot_3D_camera = Camera.c11440_36u(absorption_imaging_transition['wavelength'])
            absorption_image = AbsorptionImaging.absorption_imaging(None, 'MOT_3D_Camera', 'in_situ_absorption',
                                                                    absorption_imaging_transition, mot_3D_camera, 0, 0)
            self.MainPlotPanel.analyse_panel.add_absorption_imaging(absorption_image, current_plotting)

        # Add realtime plotting for atom number
        # plot_name = '3D-MOT Atom Number'
        # if not plot_name in self.MainPlotPanel.plots:
        #     current_plotting = Widget1DLivingPlot.Widget1DLivingPlot(plot_name, y_data_path=('absorption_imaging', 'atom_number'), y_in_results=True)
        #
        #     self.MainPlotPanel.add_plot_dock(plot_name, current_plotting)

        self.timer = QTimer()
        self.timer.timeout.connect(self.refresh_h5_path)
        self.timer.start(1000)

        return self
