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

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


# Implementing NewModule
class ModuleDialog(ui.NewModule):
    """
    This is the module setup dialog.
    Individual methods and variables not belonging to any class,
    must be linked to some declaration and/or implementation files.
    Some atributes,like static for variables, need a filereference
    to make sense.
    Modules apply to represent this reference. Modules may be used
    also for overriding the container files for classes, namespaces
    and structures, by simply attach they as childs of the module.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Initialize dialog"""
        super(ModuleDialog, self).__init__(parent)
        self._container = container
        # inicializamos el modulo con la lista
        # de headers y fuentes existentes
        self.m_sdbSizer9OK.SetDefault()
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("module"))
        self._origin = None
        self.SetIcon(icon)

    def SetAttributes(self, obj):
        """Transfer attributes to dialog"""
        self.SetTitle("Edit module")
        self._origin = obj
        self.m_choiceHeader.SetValue(obj._header)
        self.m_choiceSource.SetValue(obj._source)
        self.m_moduleName.SetValue(obj._name)

    def CopyAttributes(self, module):
        """Transfer attributes to module"""
        module._name = self._name
        module._header = self._header
        module._source = self._source

    def Validate(self):
        """Process OnOk event"""
        self._name = self.m_moduleName.GetValue().strip()
        if len(self._name) == 0:
            wx.MessageBox("Module name must be non empty", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        self._header = self.m_choiceHeader.GetValue().strip()
        if len(self._header) == 0:
            wx.MessageBox("Module header must be non empty", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        project = self._container.project
        #check header file coincidence
        classes = project(model.cc.Class, filter=lambda x: x.outer_class == x, cut=True)
        for cls in classes:
            if '{}.h'.format(cls._name) == self._header:
                wx.MessageBox("The header file is already used by a project class.", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        modules = project(model.cc.Module, filter=lambda x: x != self._origin, cut=True)
        for mod in modules:
            if mod._header == self._header:
                wx.MessageBox("The header file is already used by another project module.", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        #check header file name
        #fname = os.path.realpath(os.path.join(project.headers_dir, self._header))
        #if not os.path.exists(fname) and not os.access(fname, os.W_OK):
        #    wx.MessageBox("Module header file cannot be created. Please check name.", "Error",
        #        wx.OK | wx.CENTER | wx.ICON_ERROR, self)
        #    return False
        self._source = self.m_choiceSource.GetValue().strip()
        if len(self._source) == 0:
            wx.MessageBox("Module source must be non empty", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        #check source file coincidence
        for cls in classes:
            if '{}.cpp'.format(cls._name) == self._source:
                wx.MessageBox("The source file is already used by a project class.", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        for mod in modules:
            if mod._source == self._source:
                wx.MessageBox("The source file is already used by another project module.", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        #check source file name
        #fname = os.path.realpath(os.path.join(project.sources_dir, self._source))
        #if not os.path.exists(fname) and not os.access(fname, os.W_OK):
        #    wx.MessageBox("Module source file cannot be created. Please check name.", "Error",
        #        wx.OK | wx.CENTER | wx.ICON_ERROR, self)
        #    return False
        return True

    def get_kwargs(self):
        """returns arguments for instance"""
        return {'parent': self._container, 'name': self._name,
            'source': self._source, 'header': self._header}

    def OnOK(self, event):
        """On Ok"""
        if self.Validate():
            self.EndModal(wx.ID_OK)
