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

import wx
import wxx
from activity.models.ui import ui as ui


# Implementing SelectContexts
class SelectContexts(ui.SelectContexts):
    """
    This dialog allows to apply or remove
    contexts for an element.
    Applied contexts may insert prefix and
    sufix code both in element declaration
    an element implementation.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, obj):
        """Initialize dialog"""
        self.obj = obj
        self.contexts = obj.project._contexts
        if hasattr(obj, '_contexts'):
            self.applied = obj._contexts
        else:
            self.applied = []
        super(SelectContexts, self).__init__(parent)
        self.m_listCtrl5.InsertColumn(0, "context", format=wx.LIST_FORMAT_LEFT, width=-1)
        self.m_listCtrl4.InsertColumn(0, "context", format=wx.LIST_FORMAT_LEFT, width=-1)
        for c in self.contexts:
            if c not in self.applied:
                self.m_listCtrl5.Append((c._name,))
        for c in self.applied:
            self.m_listCtrl4.Append((c._name,))

    def CopyAttributes(self, obj):
        """Transfer context changes"""
        self.applied = []
        for i in range(0, self.m_listCtrl4.GetItemCount()):
            self.applied += [x for x in self.contexts if x._name == self.m_listCtrl4.GetItemText(i)]
        obj._contexts = self.applied

    def OnAddContext(self, event):
        """Handler for OnAddContext events."""
        i = self.m_listCtrl5.GetFirstSelected()
        if i == wx.NOT_FOUND:
            return
        s = self.m_listCtrl5.GetItemText(i)
        self.m_listCtrl5.DeleteItem(i)
        self.m_listCtrl4.Append((s,))
        pass

    def OnRemoveContext(self, event):
        """Handler for OnRemoveContext events."""
        i = self.m_listCtrl4.GetFirstSelected()
        while i != wx.NOT_FOUND:
            s = self.m_listCtrl4.GetItemText(i)
            self.m_listCtrl4.DeleteItem(i)
            self.m_listCtrl5.Append((s,))
            i = self.m_listCtrl4.GetFirstSelected()
        self.m_button31.Enable(False)
        self.m_button32.Enable(False)

    def OnSelectAssigned(self, event):
        """Handler for OnSelectAssigned events."""
        i = self.m_listCtrl4.GetFirstSelected()
        if i == wx.NOT_FOUND:
            self.m_button31.Enable(False)
            self.m_button32.Enable(False)
        else:
            if i + 1 < self.m_listCtrl4.GetItemCount():
                self.m_button32.Enable()
            else:
                self.m_button32.Enable(False)
            if i > 0:
                self.m_button31.Enable()
            else:
                self.m_button31.Enable(False)

    def OnMoveUp(self, event):
        """Move context up"""
        i = self.m_listCtrl4.GetFirstSelected()
        s = self.m_listCtrl4.GetItemText(i)
        self.m_listCtrl4.DeleteItem(i)
        self.m_listCtrl4.InsertStringItem(i - 1, s)

    def OnMoveDown(self, event):
        """Move context down"""
        i = self.m_listCtrl4.GetFirstSelected()
        s = self.m_listCtrl4.GetItemText(i)
        self.m_listCtrl4.DeleteItem(i)
        self.m_listCtrl4.InsertStringItem(i + 1, s)


