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

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


class TypeDialog(ui.NewType):
    """
    This dialog allows to create a base type
    in a very liberal way.
    Be careful!
    This method of declaring a type, is planned
    to remove as soon as the support of the C++
    syntax be complete.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Init dialog"""
        super(TypeDialog, self).__init__(parent)
        self._container = container
        self.original = ""
        self.m_textCtrl2.SetFocus()
        if container.inner_class:
            self.m_access.Enable()
            self.m_accessLb.Enable()
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("type"))
        self.SetIcon(icon)

    def Validate(self):
        """Process OnOk event"""
        self._name = self.m_textCtrl2.GetValue()
        if len(self._name) == 0:
            wx.MessageBox("Type name must be non empty", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        s = self._name.replace('::', '__')
        if re.match("^[A-Za-z_][0-9A-Za-z_]*$", s) is None:
            wx.MessageBox("Type name contains invalid characters", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        if self.original != self._name:
            types = self._container(model.cc.Type)
            if self._name in [x._name for x in types]:
                wx.MessageBox("Type already exist", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        if  self.m_checkBoxTemplate.GetValue():
            self._template = self.m_template.GetValue()
        else:
            self._template = None
        self._definition = self.m_richText2.GetValue()
        self._note = self.m_richText3.GetValue()
        iSel = self.m_access.GetCurrentSelection()
        if iSel == wx.NOT_FOUND:
            wx.MessageBox("Invalid access", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        self._access = self.m_access.GetString(iSel)
        return True

    def CopyAttributes(self, _type):
        """Copy dialog attributes to object"""
        _type._name = self._name
        _type._definition = self._definition
        _type._note = self._note
        _type._access = self._access
        if  self.m_checkBoxTemplate.GetValue():
            _type._template = self.m_template.GetValue()
        else:
            _type._template = None

    def SetAttributes(self, _type):
        """Transfer attributes from existing type"""
        self._container = _type.parent
        self._name = _type._name
        self.original = _type._name
        self._definition = _type._definition
        self._note = _type._note
        self._access = _type._access
        iSel = self.m_access.FindString(_type._access)
        self.m_access.SetSelection(iSel)
        self.m_textCtrl2.SetValue(self._name)
        self.m_richText2.SetValue(self._definition)
        self.m_richText3.SetValue(self._note)
        if not _type._template is None:
            self.m_checkBoxTemplate.SetValue(True)
            self.m_stStartTemplate.Enable(True)
            self.m_template.Enable(True)
            self.m_stEndTemplate.Enable(True)
            self.m_template.SetValue(_type._template)
        else:
            self.m_checkBoxTemplate.SetValue(False)
            self.m_stStartTemplate.Enable(False)
            self.m_template.Enable(False)
            self.m_stEndTemplate.Enable(False)
            self.m_template.SetValue('')
        self.SetTitle("Edit type")

    def OnToggleTemplate(self, event):
        """Actualiza el dialogo al hacer toggle del checkbox de template"""
        if self.m_checkBoxTemplate.GetValue():
            self.m_stStartTemplate.Enable(True)
            self.m_template.Enable(True)
            self.m_stEndTemplate.Enable(True)
        else:
            self.m_stStartTemplate.Enable(False)
            self.m_template.Enable(False)
            self.m_stEndTemplate.Enable(False)
        event.Skip()

    def get_kwargs(self):
        """Returns arguments suitable for object creation"""
        kwargs = {}
        kwargs['parent'] = self._container
        kwargs['name'] = self._name
        kwargs['definition'] = self._definition
        kwargs['note'] = self._note
        kwargs['template'] = self._template
        kwargs['access'] = self._access
        return kwargs

    def OnOK(self, event):
        """Handle OnOk command"""
        if self.Validate():
            self.EndModal(wx.ID_OK)


