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

import wx
import wxx
from activity.models.ui import ui as ui
import model


# Implementing SelectClasses
class SelectClassesDialog(ui.SelectClasses):
    """
    This dialog allows to add or remove the classes shown
    in the class diagram
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, diagram):
        """Initialize dialog"""
        super(SelectClassesDialog, self).__init__(parent)
        self._pane = parent
        self._diag = diagram
        self.project = diagram.project

    # Handlers for SelectClasses events.
    def OnInitDialog(self, event):
        """Implements init dialog"""
        #obtenemos la lista de clases del projecto que no estan
        #en el diagrama
        already = [x._obj for x in self._diag._elements]
        pos = 0
        for cls in self.project.classes:
            self.m_classChecklist.Insert(cls.scoped, pos, cls)
            self.m_classChecklist.Check(pos, cls in already)
            pos += 1
        pass

    def OnOk(self, event):
        """Implementa metodo ok"""
        classes = self.project.classes
        already = [x for x in self._diag._elements
            if type(x) is model.ClassElement]
        oready = [x._obj for x in already]
        status = self.m_classChecklist.GetCheckedStrings()
        newitems = [x for x in classes
            if x.scoped in status and x not in oready]
        removed = [x for x in already if x._obj.scoped not in status]
        position = wx.Point(10, 10)
        for cls in newitems:
            self._diag.AddClass(cls, position)
            position.x += 50
            position.y += 50
        for obj in removed:
            self._diag.RemoveElement(obj)
        self._pane.Update()
        self._pane.Refresh()
        self.EndModal(wx.OK)

