import matplotlib.pyplot as plt
class PlaceFieldVisualizer:
    def __init__(self, fields):
        self.fields=fields
        self.callbacks={}






        fig=plt.figure()
        cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
        cid_k=fig.canvas.mpl_connect('key_press_event', self.onKeyPress)

        #layout_rows=2
        #layout_cols=max(len(self.arm.fields_sm),len(self.arm.fields_hidden)+1)
        #layout_cols=max(layout_cols,4)

        layout_rows=2
        layout_cols=2
        panel_layout=layout_rows*100+10*layout_cols
        
        self.subplot_activation={}
        for i,f in enumerate(fields):
            self.subplot_activation[f]=plt.subplot(panel_layout+i+1)
            self.subplot_activation[f].set_title(f.name)
            
            f.plot_activation('green')

        '''
        self.subplot_arm_schematic=plt.subplot(panel_layout+len(self.arm.fields_sm)+1)
        
        self.subplot_arm_schematic.set_xlim(-self.arm.field_ee.radius_max,self.arm.field_ee.radius_max)
        self.subplot_arm_schematic.set_ylim(-self.arm.field_ee.radius_max,self.arm.field_ee.radius_max)
        self.subplot_arm_target_line=plt.Line2D([0.0,0.0],[0.0,0.0])
        self.subplot_arm_schematic.add_line(self.subplot_arm_target_line)
        '''
        '''
        self.subplot_poses={}
        for i,hidden in enumerate(self.arm.fields_hidden):
            field_sum,field_summand0,field_summand1=hidden.fields_sm
            radius_max=field_sum.radius_max
            plot_id=len(self.arm.fields_sm)+2+i
            
            self.subplot_poses[hidden]=plt.subplot(layout_rows,layout_cols,plot_id)
            self.subplot_poses[hidden].set_xlim(-radius_max,radius_max)
            self.subplot_poses[hidden].set_ylim(-radius_max,radius_max)
            self.subplot_poses[hidden].set_title(hidden.name)
        '''

        plt.show()



    def onClick(self, event):
        pass

    def onKeyPress(self, event):
        if event.key in self.callbacks.keys():
            self.callbacks[event]()
        self.refreshPlot()


    def addCallback(self, key, callback):
        self.callbacks[key]=callback


    def refreshPlot(self):
        for field, subplot in self.subplot_activation.items():
            subplot.cla()
            subplot.set_title(field.name)


            field.plot_activation('green',subplot)
        plt.draw()
