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


# Implementing NewPyMember
class PyMemberDialog(ui.NewPyMember):
    """This dialog allows to specify the python class
    member attributes."""
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Initialice dialog"""
        super(PyMemberDialog, self).__init__(parent)
        self._container = container
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("py_member"))
        self.SetIcon(icon)

    # Handlers for NewPyMember events.
    def OnCancel(self, event):
        """Cancel dialog"""
        self.EndModal(wx.ID_CANCEL)

    def Validate(self):
        """Dialog validation"""
        self._name = self.m_textCtrl6.GetValue().strip()
        if len(self._name) == 0:
            wx.MessageBox("Member name must not be 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("Member name contains invalid characters", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        iscope = self.m_radioBox1.GetSelection()
        if iscope == 0:
            self._scope = 'class'
        else:
            self._scope = 'instance'
        self._value = self.m_textCtrl8.GetValue().strip()
        if len(self._value) == 0:
            self._value = "None"
        self._note = self.m_richText1.GetValue()
        return True

    def get_kwargs(self):
        """return arguments for object instance"""
        return  {'parent': self._container, 'name': self._name,
            'scope': self._scope, 'value': self._value,
            'note': self._note}

    def SetAttributes(self, member):
        """Setup attributes for editing already method"""
        self.SetTitle("Edit member")
        self.m_textCtrl6.SetValue(member._name)
        if member._scope == 'class':
            self.m_radioBox1.SetSelection(0)
        else:
            self.m_radioBox1.SetSelection(1)
        self.m_textCtrl8.SetValue(member._value)
        self.m_richText1.SetValue(member.note)

    def CopyAttributes(self, member):
        """Setup attributes"""
        member._name = self._name
        member._scope = self._scope
        member._value = self._value
        member.note = self._note

    def OnOK(self, event):
        """ok event handler"""
        if self.Validate():
            self.EndModal(wx.ID_OK)
