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

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

# Implementing NewConstructor
class ConstructorDialog(ui.NewConstructor):
    """
    This dialog allows to create a new class constructor and
    his attributes. The preferred constructor means that this
    constructor will be chosen by default for the list of
    the required arguments of derived classes and creating the
    init sections of these classes.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Initialize dialog"""
        super(ConstructorDialog, self).__init__(parent)
        self._container = container
        self._class = container.inner_class
        self.m_choice2.SetFocus()
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("constructor"))
        self.SetIcon(icon)

    def CopyAttributes(self, ctor):
        """Transfer attributes to ctor"""
        ctor._name = self._name
        ctor._access = self._access
        ctor._explicit = self._explicit
        ctor._inline = self._inline
        ctor._declare = self._declare
        ctor._implement = self._implement
        ctor._calling = self._calling
        if self._calling:
            ctor._callingText = self._callingText
        ctor.note = self._note
        ctor.SetPreferred(self._preferred)

    def SetAttributes(self, ctor):
        """Transfer attributes from existing constructor"""
        self._container = ctor.parent
        self._class = ctor.inner_class
        self._name = ctor._name
        self._access = ctor._access
        iSel = self.m_choice2.FindString(self._access)
        self.m_choice2.Select(iSel)
        self._declare = ctor._declare
        self.m_checkBox92.SetValue(self._declare)
        self._implement = ctor._implement
        self.m_checkBox93.SetValue(self._implement)
        self._inline = ctor._inline
        self.m_checkBox31.SetValue(self._inline)
        self._calling = ctor._calling
        self.m_checkBox6.SetValue(self._calling)
        self.m_comboBox1.Enable(self._calling)
        if self._calling:
            self._callingText = ctor._callingText
            iSel = self.m_comboBox1.FindString(self._callingText)
            self.m_comboBox1.Select(iSel)
        self._note = ctor.note
        self.m_richText1.SetValue(self._note)
        self._preferred = ctor.IsPreferred()
        pref = self._class.GetPreferredConstructor()
        if pref is None or pref is ctor:
            self.m_checkBoxPreferred.Enable(True)
        self.m_checkBoxPreferred.SetValue(self._preferred)
        self.SetTitle("Edit constructor")

    def Validate(self):
        """Validation"""
        self._name = self._class._name
        iSel = self.m_choice2.GetSelection()
        self._access = self.m_choice2.GetString(iSel)
        self._declare = self.m_checkBox92.IsChecked()
        self._explicit = self.m_checkBox41.IsChecked()
        self._inline = self.m_checkBox31.IsChecked()
        self._implement = self.m_checkBox93.IsChecked()
        self._calling = self.m_checkBox6.IsChecked()
        if self._calling:
            iSel = self.m_comboBox1.GetSelection()
            self._callspec = self.m_comboBox1.GetString(iSel)
        else:
            self._callspec = ''
        self._note = self.m_richText1.GetValue()
        self._preferred = self.m_checkBoxPreferred.IsChecked()
        return True

    def get_kwargs(self):
        """return arguments suitable for creation"""
        return {'parent': self._container, 'name': self._name,
            'access': self._access, 'explicit': self._explicit,
            'inline': self._inline, 'declare': self._declare,
            'implement': self._implement, 'calling': self._calling,
            'callspec': self._callspec, 'note': self._note,
            'preferred': self._preferred, 'autoargs': True}

    def OnCallingToggle(self, event):
        """Implements calling toggle"""
        self.m_comboBox1.Enable(self.m_checkBox6.IsChecked())

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

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


