"""Subclass of NewPyPackage, which is generated by wxFormBuilder."""

import re
import wx
import wxx
from activity.models.ui import ui as ui
import app.resources as rc


class PyPackageDialog(ui.NewPyPackage):
    """
    This dialog allows to create or edit
    a python package.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """initialice dialog"""
        self.container = container
        super(PyPackageDialog, self).__init__(parent)
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("py_package"))
        self.SetIcon(icon)

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

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

    def SetAttributes(self, package):
        """Setup attributes for editing already method"""
        self.m_comboBox5.SetValue(package.name)
        self.m_richText3.SetValue(package.note)
        self.SetTitle("Edit package")

    def Validate(self):
        """Validation"""
        self._name = self.m_package_name.GetValue().strip()
        if re.match("^[A-Za-z_][0-9A-Za-z_]*$", self._name) is None:
            wx.MessageBox("package name contains invalid characters", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        self._note = self.m_richText3.GetValue()
        return True

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

    # Handlers for NewPyPackage events.
    def OnOK(self, event):
        """Handle ok method"""
        if self.Validate():
            self.EndModal(wx.ID_OK)
