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

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


# Implementing SelectFriends
class SelectFriendsDialog(ui.SelectFriends):
    """
    This dialog allows to add or remove
    friendship fo the current class
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, obj):
        """initialize dialog"""
        self._class = obj.inner_class
        super(SelectFriendsDialog, self).__init__(parent)
        # place icon
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("friend"))
        self.SetIcon(icon)

        # get current declared fields
        friends = self._class.friends
        # get all project classes
        project = self._class.project
        classes = project(cc.Class)
        #map classes to existing friendship
        c2fmap = dict([(x._target, x) for x in friends])
        # insert the classes with current relation
        pos = 0
        for cls in classes:
            # skip self class
            if cls is self._class:
                continue
            if cls in c2fmap:
                v = (cls, c2fmap[cls])
            else:
                v = (cls, None)
            self.m_checkList2.Insert(cls.scoped, pos, v)
            if v[1]:
                self.m_checkList2.Check(pos)
            pos = pos + 1

    def get_kwargs(self):
        """Returns kwargs dictionary suitable for objects creation"""
        kwargs_list = []
        for item in range(0, self.m_checkList2.GetCount()):
            v = self.m_checkList2.GetClientData(item)
            c = self.m_checkList2.IsChecked(item)
            if  (c and v[1]) or (not c and not v[1]):
                continue
            if c:
                kwargs = {}
                kwargs['parent'] = self._class
                kwargs['target'] = v[0]
                kwargs_list.append(kwargs)
            else:
                v[1].Delete()
        return kwargs_list
