"""Subclass of NewPyMethod, 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 NewPyMethod
class PyFunctionDialog(ui.NewPyMethod):
    """
    This dialog allows to setup non-member methods.
    This kind of elements may be declared inside
    a module.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Initialize dialog"""
        super(PyFunctionDialog, self).__init__(parent)
        self.container = container
        self.m_textCtrl5.SetFocus()
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("py_function"))
        self.SetIcon(icon)

    def Validate(self):
        """Validation"""
        self._name = self.m_textCtrl5.GetValue()
        if len(self._name) == 0:
            wx.MessageBox("Function 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("Function name contains invalid characters", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        self._implement = self.m_checkBox86.IsChecked()
        self._note = self.m_richText1.GetValue()
        return True

    def get_kwargs(self):
        """Returns kwargs dictionary suitable for object creation"""
        kwargs = {}
        kwargs['parent'] = self.container
        kwargs['name'] = self._name
        kwargs['implement'] = self._implement
        kwargs['note'] = self._note
        return kwargs

    def CopyAttributes(self, method):
        """Copy attributes to method"""
        method._name = self._name
        method._implement = method._implement
        method.note = self._note

    def SetAttributes(self, method):
        """Setup attributes for editing already method"""
        self.m_textCtrl5.SetValue(method._name)
        self.m_checkBox86.SetValue(method._implement)
        self.m_richText1.SetValue(method.note)
        self.SetTitle("Edit method")

    def OnCancel(self, event):
        """cancel event handler"""
        self.EndModal(wx.ID_CANCEL)

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


