"""Subclass of DrawFrame, which is generated by wxFormBuilder."""

# noinspection PyPackageRequirements
import wx
from . import appui
from .. import models


# Implementing DrawFrame
class MyDrawFrame(appui.DrawFrame):
    def __init__(self, parent, lattice_model, aspect=1):
        """ lattice visualization panel 
            :param lattice_model: beamline.Models instance
        """
        appui.DrawFrame.__init__(self, parent)
        self.parent = parent
        self.lattice_model = lattice_model
        self._aspect = aspect
        self._artist_flag = self.mode_rb.GetStringSelection()
        self._init()

    def _init(self):
        self.bl_info_st_val.SetLabel(self.parent.use_beamline)
        self.ax = self.drawing_panel.axes
        self.canvas = self.drawing_panel.canvas
        self._draw()
        self.drawing_panel.anote_list = self._anotes
        self.drawing_panel.x_pos_list = [i['xypos'][0] for i in self._anotes]
        self.anote_list_quad = None
        self.anote_list_bend = None
        self.anote_list_rf = None

        #print self.lattice_model.getElementsByName('prf06l3')
        
    def mode_rbOnRadioBox(self, event):
        self._artist_flag = event.GetString()
        self._draw()
    
    def quad_ckbOnCheckBox(self, event):
        if self.quad_ckb.IsChecked():
            if self.anote_list_quad is None:
                self.anote_list_quad = models.Models.anoteElements(self.ax, 
                        self._anotes, efilter='QUAD', 
                        textypos=0.6, color='m', rotation=60,
                        fontsize='x-small')
            else:
                [anote_obj.set_visible(True) for anote_obj in self.anote_list_quad]
            self.canvas.draw()
        else:
            if self.anote_list_quad is not None:
                [anote_obj.set_visible(False) for anote_obj in self.anote_list_quad]
                self.canvas.draw()
            
    def bend_ckbOnCheckBox(self, event):
        if self.bend_ckb.IsChecked():
            if self.anote_list_bend is None:
                self.anote_list_bend = models.Models.anoteElements(self.ax, 
                        self._anotes, efilter='CSRCSBEN',
                        textypos=-0.6, color='b', rotation=60, 
                        fontsize='x-small')
            else:
                [anote_obj.set_visible(True) for anote_obj in self.anote_list_bend]
            self.canvas.draw()
        else:
            if self.anote_list_bend is not None:
                [anote_obj.set_visible(False) for anote_obj in self.anote_list_bend]
                self.canvas.draw()

    def rf_ckbOnCheckBox(self, event):
        if self.rf_ckb.IsChecked():
            if self.anote_list_rf is None:
                self.anote_list_rf = models.Models.anoteElements(self.ax, 
                        self._anotes, efilter=('RFCW','RFDF'),
                        textypos=None, arrowprops=None, color='k', rotation=0, 
                        fontsize='small', fontweight='bold')
            else:
                [anote_obj.set_visible(True) for anote_obj in self.anote_list_rf]
            self.canvas.draw()
        else:
            if self.anote_list_rf is not None:
                [anote_obj.set_visible(False) for anote_obj in self.anote_list_rf]
                self.canvas.draw()
    
    def _draw_model(self, mode):
        _ptches, _anotes, _xr, _yr = self.lattice_model.draw(mode=mode,
                showfig=False)
        return _ptches, _anotes, _xr, _yr

    def _draw(self):
        self.ax.clear()
        self._ptches, self._anotes, self._xr, self._yr = self._draw_model(self._artist_flag)
        models.Models.plotElements(self.ax, self._ptches)
        self.ax.set_xlim(self._xr[0], self._xr[1])
        self.ax.set_ylim(self._yr[0], self._yr[1])
        self.ax.set_yticks([])
        self.ax.set_xlabel('$s\,\mathrm{[m]}$', fontsize=20)
        self.ax.set_aspect(self._aspect, 'datalim')
        self.canvas.draw()
