
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import numpy as np
import matplotlib.pyplot as plt
import skimage


class ZoomPlotWidget(FigureCanvas):
    """It is used to display the roi of the selected cell across days with each zoom being a different day. 
    The roi is centered on the median coordinates of the selected cell. The mean image of the recording is used to create the zooms. 
    The probability of the cell being a cell and the index of the cell in the suite2p files associated with each recording (day) are also displayed under the zooms."""
    
    def __init__(self,  all_ops=None, all_stat_t2p=None, colors=None, all_iscell_t2p=None,t2p_match_mat_allday =None,track_ops=None, imgs=None):
        nb_plot=len(all_iscell_t2p)
        self.fig, self.ax_zoom = plt.subplots(1, nb_plot, figsize = (10*nb_plot, nb_plot), gridspec_kw={'width_ratios': [1] * nb_plot},facecolor='black')
        super().__init__(self.fig)
        self.track_ops=track_ops
        self.all_ops = all_ops
        self.all_stat_t2p = all_stat_t2p
        self.colors = colors
        self.all_is_cell=all_iscell_t2p
        self.t2p_match_mat_allday =t2p_match_mat_allday 
        self.imgs= imgs
        self.coord_dict={} 

    def display_zooms(self, selected_cell_index):
        """It is called when the application is opened and a cell is selected."""
#it is used to store the roi and the median coordinates of the selected cell for each recording (day)
        self.coord_dict={}
        if self.all_ops is not None and self.all_stat_t2p is not None and self.all_is_cell is not None:
            for i in range(len(self.imgs)):
                wind = 20
                #mean_img = self.all_ops[i]['meanImg']
                mean_img=self.imgs[i]
                stat_t2p = self.all_stat_t2p[i]
                median_coord = stat_t2p[selected_cell_index]['med']
                print(f'median_coord : ', median_coord)

                print(mean_img.shape)
                # Définir la taille de la marge
                margin = 20

                # Obtenir les dimensions de l'image originale
                Ly, Lx = mean_img.shape

                # Créer une nouvelle image avec des dimensions augmentées
                new_Ly = Ly + 2 * margin
                new_Lx = Lx + 2 * margin
                range_img = np.zeros((new_Ly, new_Lx))
             
                
                # Copier mean_img au centre de la nouvelle image
                range_img[margin:margin + Ly, margin:margin + Lx] = mean_img
                
                # Ajouter la marge aux coordonnées médianes
                median_x = int(median_coord[1]) + margin
                median_y = int(median_coord[0]) + margin

                # Calculer les nouvelles coordonnées de la ROI
                x_start = median_x - wind
                x_end = median_x + wind
                y_start = median_y - wind
                y_end = median_y + wind

                # Extraire la ROI de l'image avec la marge
                roi = range_img[y_start:y_end, x_start:x_end]

                print(range_img.shape)
                print(roi.shape)

            
                #prob and index 
                iscell=self.all_is_cell[i]
                if self.track_ops.iscell_thr==None:
                    indices_lignes_1 = np.where(iscell[:,0]==1)[0]
                    match_index=self.t2p_match_mat_allday[selected_cell_index,i]
                    true_index=indices_lignes_1[match_index]
                else:
                    indices_lignes_1= np.where(iscell[:,1]>self.track_ops.iscell_thr)[0]
                    match_index=self.t2p_match_mat_allday[selected_cell_index,i]
                    true_index=indices_lignes_1[match_index]
                prob=round(iscell[true_index,1],2)

                ypix=stat_t2p[selected_cell_index]['ypix']
                xpix=stat_t2p[selected_cell_index]['xpix']


                ax = self.ax_zoom[i]
                color = self.colors[selected_cell_index]

                mask=np.zeros((2*wind,2*wind))
                mask[ypix-median_coord[0]+wind,xpix-median_coord[1]+wind]=1
                ax.clear()
                ax.contour(mask,levels=[0.5], colors=[color],linewidths=2)

                #last_img=list(self.coord_dict.values())[-1][0]
                #match_roi=skimage.exposure.match_histograms(list[0], last_img, channel_axis=None)
                
                ax.imshow(roi, cmap='gray')
                ax.set_title(f'Day {i + 1}', color='white', fontsize=10)
                ax.text(0.5, -0.2, f'i: {true_index}', color='white', fontsize=10, ha='center', va='center', transform=ax.transAxes)
                ax.text(0.5, -0.4, f'p: {prob}', color='white', fontsize=10, ha='center', va='center', transform=ax.transAxes)

                ax.axis('off')
        
        self.draw()
                    