import pyqtgraph as pg
from pyqtgraph import Qt
from pyqtgraph.Qt import QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

from .DataExtractorLyse import DataExtractorManager


class WidgetPlot(QSplitter):
    def __init__(self, title, **kwargs):
        super().__init__(**kwargs)

        # Orientation of plot and title (and other information)
        self.setOrientation(Qt.Vertical)

        # Add title
        self.title = QtGui.QLabel()
        self.title.setAlignment(QtCore.Qt.AlignCenter)
        self.title.setText('<h2>' + title + ' <\h2>')
        self.addWidget(self.title)

        # Claim bottom part (title)
        self.bottom = QSplitter()
        self.bottom.setOrientation(Qt.Vertical)
        self.addWidget(self.bottom)

        # Add plot
        self.plots = pg.GraphicsLayoutWidget()
        self.bottom.addWidget(self.plots)

        # Add warning information
        self.desciption = pg.LayoutWidget()
        self.warning = QtGui.QLabel()
        self.warning.setAlignment(QtCore.Qt.AlignCenter)
        self.desciption.nextRow()
        self.desciption.addWidget(self.warning)
        # self.bottom.addWidget(self.desciption)

        # Current hdf5 file path
        self.h5_path_shown = None

        # data extractor
        self.data_extractor_manager = DataExtractorManager()

    def update_from_h5(self, h5_path):
        h5_path = h5_path.replace('\n', '')
        if self.h5_path_shown != h5_path or self.data_extractor_manager.local_data_changed:
            self.h5_path_shown = h5_path
            self.update(h5_path)

    def update_warning(self, warning):
        if warning == '':
            self.warning.setStyleSheet("background-color: lightgreen")
            warning = 'all good'
        else:
            self.warning.setStyleSheet("background-color: red")

        self.warning.setText(warning)

