# vim: ts=4 et sts=4 sw=4 autoindent

import gtk
import Pyro.core

from sndcs_common.PyroProxyMixin import PyroProxyMixin
from sndcs_client.Config import config

# Searching function
from Application import treeview_visible_function

class IndirectActivitiesTreeView(PyroProxyMixin):

    def __init__(self, tree_view, namespace=None):
        self.view = tree_view
        tree_column_1 = gtk.TreeViewColumn("Code")
        tree_column_2 = gtk.TreeViewColumn("Description")
        tree_column_1.set_sort_column_id(1)
        tree_column_2.set_sort_column_id(2)
        self.view.append_column(tree_column_1)
        self.view.append_column(tree_column_2)

        cell = gtk.CellRendererText()
        tree_column_1.pack_start(cell, True)
        tree_column_1.add_attribute(cell, "text", 1)
        tree_column_2.pack_start(cell, True)
        tree_column_2.add_attribute(cell, "text", 2)

        self.model = gtk.ListStore(long, str, str)
        self.view.set_model(self.model)

        self.column_number = 0
        self.search_criteria = ""

        if namespace:
            self.namespace = namespace
        else:
            # Get the Pyro namespace from the config
            self.namespace = config.get("pyro", "namespace", "sndcs")

    def populate(self, model=None):
        """Populate the model"""
        if model:
            self.model = model
        else:
            self.model = gtk.ListStore(long, str, str)

            active_indirects = Pyro.core.getProxyForURI(self.formatPyronameString(self.namespace, ["Indirect"]))

            indirects = active_indirects.getActiveIndirects()
            for indirect in indirects:
                self.model.append((indirect["serialNum"], indirect["code"], indirect["description"]))

        self.modelfilter = self.model.filter_new()
        self.modelfilter.set_visible_func(treeview_visible_function,self)

        self.modelsort = gtk.TreeModelSort(self.modelfilter)

        self.view.set_model(self.modelsort)
        self.modelsort.set_sort_column_id(1, gtk.SORT_ASCENDING) # Start off sorting by the first column ASCENDING

    def find_indirect(self, indirect_id):
        p = None # (p)arent
        i = 0
        for x in self.model:
            if int(x[0]) == int(indirect_id):
                p = i
                break
            i += 1
        return p
    
    def add_indirect(self, serialNum, code, description):
        self.model.append((int(serialNum),code, description))

    def delete_indirect(self, serialNum):
        p = self.find_indirect(serialNum)
        if p is not None:
            parent_iter = self.model.get_iter(p)

            self.model.remove(parent_iter)

    def edit_indirect(self, serialNum, code, description):
        self.delete_indirect(serialNum)
        self.add_indirect(serialNum, code, description)
        
    def update(self, event=False):
        """Update for search"""

        if event:
            serialNum = event.msg["serialNum"]
            code = event.msg["code"]
            description = event.msg["description"]
            
            if event.subject == self.namespace + "_indirect_delete":
                self.delete_indirect(serialNum)
            elif event.subject == self.namespace + "_indirect_add":
                self.add_indirect(serialNum, code, description)
            elif event.subject == self.namespace + "_indirect_edit":
                self.edit_indirect(serialNum, code, description)

        else:
            self.modelfilter.refilter()
