"""Subclass of NewPyClass, which is generated by wxFormBuilder."""
import re
import wx
import wxx
import app.resources as rc
from activity.models.ui import ui as ui


# Implementing NewClass
class PyClassDialog(ui.NewPyClass):
    """This dialog allows to specify the python class
    attributes."""
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Initialice dialog"""
        super(PyClassDialog, self).__init__(parent)
        self._container = container
        self.m_textCtrl2.SetFocus()
        self.project = container.project
        self._original = ""
        #if the content is inside package, you can export it
        if not container.inner_module:
            self.m_checkBox94.SetValue(False)
            self.m_checkBox94.Enable(False)
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("py_class"))
        self.SetIcon(icon)

    def Validate(self):
        """implement OnOK button"""
        self._name = self.m_textCtrl2.GetValue()
        if len(self._name) == 0:
            wx.MessageBox("Class name must be non empty", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        if re.match("^[A-Za-z_][0-9A-Za-z_]*$", self._name) is None:
            wx.MessageBox("class name contains invalid characters", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        if not self._original == self._name:
            if self._name in [x._name for x in self.project.level_classes]:
                wx.MessageBox("Class already exist", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        self._memberPrefix = self.m_textPrefix.GetValue()
        self._export = self.m_checkBox94.IsChecked()
        self._note = self.m_richText3.GetValue()
        return True

    def CopyAttributes(self, cls):
        """Copy dialog selections to object"""
        cls._name = self._name
        cls.note = self._note
        cls._memberPrefix = self._memberPrefix
        cls._export = self._export

    def SetAttributes(self, clase):
        """Copy object attributes to dialog"""
        self.SetTitle("Edit class")
        self._original = clase._name
        self._name = clase._name
        self._note = clase._note
        self._memberPrefix = clase._memberPrefix
        self._export = clase._export

        self.m_textCtrl2.SetValue(self._name)
        self.m_textPrefix.SetValue(self._memberPrefix)
        self.m_richText3.SetValue(self._note)
        self.m_checkBox94.SetValue(self._export)

    def get_kwargs(self):
        """Returns suitable arguments for creating object"""
        kwargs = {}
        kwargs['parent'] = self._container
        kwargs['name'] = self._name
        kwargs['prefix'] = self._memberPrefix
        kwargs['note'] = self._note
        kwargs['export'] = self._export
        return kwargs

    # Handlers for NewPyClass events.
    def OnOK(self, event):
            """implement OnOK button"""
            if self.Validate():
                self.EndModal(wx.ID_OK)

