"""Subclass of NewVariable, 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 NewVariable
class PyVariableDialog(ui.NewPyVariable):
    """
    This dialog allows to setup a non-member
    variable.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Dialog initialization"""
        super(PyVariableDialog, self).__init__(parent)
        self._container = container
        self.m_textCtrl6.SetFocus()
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("py_variable"))
        self.SetIcon(icon)

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

    def SetAttributes(self, member):
        """Set the attributes"""
        self.m_textCtrl6.SetValue(member._name)
        self.m_textCtrl8.SetValue(member._value)
        self.m_richText1.SetValue(member._note)
        self.SetTitle("Edit variable")

    def Validate(self):
        """Dialog validation"""
        self._name = self.m_textCtrl6.GetValue()
        if len(self._name) == 0:
            wx.MessageBox("Variable 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("Variable name contains invalid characters", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        self._value = self.m_textCtrl8.GetValue()
        self._note = self.m_richText1.GetValue()
        return True

    def get_kwargs(self):
        """return arguments for object instance"""
        return  {'parent': self._container, 'name': self._name,
            'value': self._value, '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)

