
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import (QWidget, QLabel, QHBoxLayout, QVBoxLayout, QApplication, QMainWindow, QGridLayout,
                             QDialog, QFrame, QTabWidget, QScrollArea)

import numpy as np

import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolBar
from matplotlib.colors import LogNorm, BoundaryNorm

import settings as stg


class PlotNoiseWindow(QDialog):

    def __init__(self, parent=None):

        super(PlotNoiseWindow, self).__init__(parent)

        self.setGeometry(400, 200, 700, 500)

        self.setWindowTitle("Noise field")

        self.verticalLayout_Main = QVBoxLayout()
        self.setLayout(self.verticalLayout_Main)

        self.tab = QTabWidget()
        self.verticalLayout_Main.addWidget(self.tab)

        for i in range(len(stg.filename_BS_raw_data)):

            exec("self.tab" + str(i) + "= QWidget()")
            exec("self.tab.addTab(self.tab" + str(i) + ", stg.filename_BS_raw_data[" + str(i) + "])")

            exec("self.verticalLayout_tab" + str(i) + "= QVBoxLayout()")
            exec("self.tab" + str(i) + ".setLayout(self.verticalLayout_tab" + str(i) + ")")

            exec("self.fig" + str(i) + ", self.ax" + str(i) +
                 "= plt.subplots(nrows=stg.freq[" + str(i) + "].shape[0], ncols=1, layout='constrained')")

            if stg.BS_noise_raw_data[i].shape != (0,):

                for freq_ind, freq_val in enumerate(stg.freq[i]):

                    eval("self.ax" + str(i) + "[" + str(freq_ind) + "]" + ".cla()")

                    val_min = np.nanmin(stg.BS_noise_raw_data[i][freq_ind, :, :])
                    val_max = np.nanmax(stg.BS_noise_raw_data[i][freq_ind, :, :])

                    if val_min == val_max:
                        exec("pcm = self.ax" + str(i) + "[" + str(freq_ind) + "]" + ".pcolormesh(" +
                             "stg.time_noise[" + str(i) + "][" + str(freq_ind) + ", :]," +
                             "-stg.depth_noise[" + str(i) + "][" + str(freq_ind) + ", :]," +
                             "stg.BS_noise_raw_data[" + str(i) + "][" + str(freq_ind) + ", :, :]," +
                             "cmap='hsv')")
                    else:
                        val_min = 0
                        val_max = 1e-5
                        exec("pcm = self.ax" + str(i) + "[" + str(freq_ind) + "]" + ".pcolormesh(" +
                             "stg.time_noise[" + str(i) + "][" + str(freq_ind) + ", :]," +
                             "-stg.depth_noise[" + str(i) + "][" + str(freq_ind) + ", :]," +
                             "stg.BS_noise_raw_data[" + str(i) + "][" + str(freq_ind) + ", :, :]," +
                             "cmap='hsv')")

                    eval("self.ax" + str(i) + "[" + str(freq_ind) + "]" + ".text(1, .70, stg.freq_text[" + str(i) +
                                        "][" + str(freq_ind) + "]," +
                                      "fontsize=10, fontweight='bold', fontname='Ubuntu', c='black', alpha=0.5," +
                                      "horizontalalignment='right', verticalalignment='bottom'," +
                                      "transform=self.ax" + str(i) + "[" + str(freq_ind) + "].transAxes)")

                exec("self.fig" + str(i) + ".supxlabel('Time (sec)', fontsize=10)")
                exec("self.fig" + str(i) + ".supylabel('Depth (m)', fontsize=10)")

                exec("cbar = self.fig" + str(i) + ".colorbar(pcm, ax=self.ax" + str(i) + "[:] ," +
                                                                "shrink=1, location='right')")
                eval("cbar.set_label(label='Noise signal (V)', rotation=270, labelpad=8)")

                exec("self.fig" + str(i) + ".canvas.draw_idle()")

            else:

                pass

            exec("self.canvas" + str(i) + "= FigureCanvas(self.fig" + str(i) + ")")
            exec("self.toolbar" + str(i) + "= NavigationToolBar(self.canvas" + str(i) + ", self)")

            exec("self.scroll" + str(i) + "= QScrollArea()")
            exec("self.scroll" + str(i) + ".setWidget(self.canvas" + str(i) + ")")

            exec("self.verticalLayout_tab" + str(i) + ".addWidget(self.toolbar" + str(i) + ")")
            exec("self.verticalLayout_tab" + str(i) + ".addWidget(self.scroll" + str(i) + ")")

