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

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


# Implementing NewPynheritance
class PyInheritanceDialog(ui.NewPyInheritance):
    """
    This dialog allows to setup inheritance from
    another struct or class.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Initialize dialog"""
        super(PyInheritanceDialog, self).__init__(parent)
        self._name = ""
        self._original = ""
        self._element = container
        self.project = container.project
        self.__initClassBoxes__()
        self.m_choice11.SetFocus()
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("py_inheritance"))
        self.SetIcon(icon)

    def __initClassBoxes__(self):
        """Initialize class boxes"""
        classes = self.project.level_classes
        for clase in classes:
            if self._element == clase:
                continue
            if self._element.IsAncestor(clase):
                continue
            self.m_choice11.Append(clase._name, clase)
        if len(classes) > 0:
            self.m_choice11.SetSelection(0)

    def Validate(self):
        """Validation"""
        self._name = self.m_choice11.GetValue()
        iSel = self.m_choice11.FindString(self._name)
        if iSel == wx.NOT_FOUND:
            self._ancestor = None
        else:
            self._ancestor = self.m_choice11.GetClientData(iSel)
        self._note = self.m_richText3.GetValue()
        if self._name != self._original:
            inheritance = self._element(model.cc.Inheritance,
                filter=lambda x: x.inner_class == self._element, cut=True)
            if self._name in [x._name for x in inheritance]:
                wx.MessageBox("Inheritance from " + self._name +
                " already exists", "Error",
                    wx.OK | wx.CENTER | wx.ICON_ERROR, self)
                return False
        return True

    def CopyAttributes(self, inheritance):
        """Copy attributes to inheritance"""
        inheritance._name = self._name
        inheritance.SetAncestor(self._ancestor)
        inheritance.note = self._note

    def SetAttributes(self, inheritance):
        """Setup attributes for editing already inheritance"""
        self._original = inheritance._name
        self.m_choice11.SetValue(inheritance._name)
        #self.m_choice11.SetSelection(iSel)
        if inheritance.note:
            self.m_richText3.SetValue(inheritance.note)
        self.SetTitle("Edit inheritance")

    def get_kwargs(self):
        """returns arguments suitable for object instance"""
        return {'parent': self._element,
            'ancestor': self._ancestor,
            'name': self._name,
            'note': self._note}

    # Handlers for NewMethod events.
    def OnCancel(self, event):
        """cancel event handler"""
        self.EndModal(wx.ID_CANCEL)

    # Handlers for NewInheritance events.
    def OnOK(self, event):
        """Process OnOk event"""
        """ok event handler"""
        if self.Validate():
            self.EndModal(wx.ID_OK)
