"""Subclass of NewPyArgument, 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 NewArgument
class PyArgumentDialog(ui.NewPyArgument):
    """
    This dialog allows to specify name, and initialization for a method argument.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, method):
        """Initialization. The parent argument represents the mainWindow """
        super(PyArgumentDialog, self).__init__(parent)
        self.m_textCtrl6.SetFocus()
        self._container = method
        self._origin = None
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("py_argument"))
        self.SetIcon(icon)

    def CopyAttributes(self, argument):
        """Get the atributes"""
        argument._name = self._name
        argument._default = self._default
        argument.note = self._note

    def SetAttributes(self, argument):
        """Set the attributes"""
        self._origin = argument._name
        self.m_textCtrl6.SetValue(argument._name)
        self.m_textCtrl8.SetValue(argument._default)
        self.m_richText1.SetValue(argument.note)
        self.SetTitle("Edit argument")

    def Validate(self):
        """Validation"""
        self._name = self.m_textCtrl6.GetValue()
        if len(self._name) == 0:
            wx.MessageBox("Argument name must not be empty", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        if self._name in ['args', 'kwargs']:
            wx.MessageBox("args and kwargs are special argument names", "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("Argument name contains invalid characters", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        if self._origin != self._name:
            if self._container.ExistArgumentNamed(self._name):
                wx.MessageBox("Argument name already exists", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        self._default = self.m_textCtrl8.GetValue()
        self._note = self.m_richText1.GetValue()
        return True

    def get_kwargs(self):
        """returns arguments suitable for creating new object"""
        return {'parent': self._container, 'name': self._name,
            'default': self._default, 'note': self._note}

    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)


