import gtk

from sndcs.JobHedFactory import JobHedFactory

# Searching function
from Application import treeview_visible_function

class JobAdminTreeView(object):

    def __init__(self, tree_view):
        self.view = tree_view

        tree_column_1 = gtk.TreeViewColumn("Number")
        tree_column_2 = gtk.TreeViewColumn("Item")
        tree_column_3 = gtk.TreeViewColumn("Description")
        tree_column_4 = gtk.TreeViewColumn("Priority")
        tree_column_1.set_sort_column_id(1)
        tree_column_2.set_sort_column_id(2)
        tree_column_3.set_sort_column_id(3)
        tree_column_4.set_sort_column_id(4)
        self.view.append_column(tree_column_1)
        self.view.append_column(tree_column_2)
        self.view.append_column(tree_column_3)
        self.view.append_column(tree_column_4)
        
        cell = gtk.CellRendererText()
        cell.set_property("editable", True)
        cell.connect("edited", self.on_cell_edited, 1)
        tree_column_1.pack_start(cell, True)
        tree_column_1.add_attribute(cell, "text", 1)
        
        cell = gtk.CellRendererText()
        cell.set_property("editable", True)
        cell.connect("edited", self.on_cell_edited, 2)
        tree_column_2.pack_start(cell, True)
        tree_column_2.add_attribute(cell, "text", 2)

        cell = gtk.CellRendererText()
        cell.set_property("editable", True)
        cell.connect("edited", self.on_cell_edited, 3)
        tree_column_3.pack_start(cell, True)
        tree_column_3.add_attribute(cell, "text", 3)

        cell = gtk.CellRendererText()
        cell.set_property("editable", True)
        cell.connect("edited", self.on_cell_edited, 4)
        tree_column_4.pack_start(cell, True)
        tree_column_4.add_attribute(cell, "text", 4)
        
        self.view.connect("row-expanded", self.on_row_expanded)
        self.view.set_search_column(1)
        #self.view.set_reorderable(True)

        self.column_number = 0
        self.search_criteria = ""

    def on_cell_edited(self, cellrenderer, path, new_text, column):
        model = self.view.get_model()
        iter = model.get_iter(path)
        from sndcs.JobHed import JobHed
        from sndcs.Assembly import Assembly
        from sndcs.Operation import Operation
        instance = model.get_value(iter, 0)
        from sndcs.Store import store
        if isinstance(instance, JobHed):
            if column is 1:
                instance.setNumber(new_text)
                store.saveChanges()
                model.set_value(iter, column, new_text)
            elif column is 2:
                instance.setItemNumber(new_text)
                store.saveChanges()
                model.set_value(iter, column, new_text)
            elif column is 3:
                instance.setItemDescription(new_text)
                store.saveChanges()
                model.set_value(iter, column, new_text)
            elif column is 4:
                instance.setPriority(new_text)
                store.saveChanges()
                model.set_value(iter, column, new_text)
        elif isinstance(instance, Assembly):
            if column is 1:
                instance.setNumber(new_text)
                store.saveChanges()
                model.set_value(iter, column, new_text)
            if column is 3:
                instance.setDescription(new_text)
                store.saveChanges()
                model.set_value(iter, column, new_text)
        elif isinstance(instance, Operation):
            if column is 1:
                instance.setNumber(new_text)
                store.saveChanges()
                model.set_value(iter, column, new_text)
            if column is 3:
                instance.setDescription(new_text)
                store.saveChanges()
                model.set_value(iter, column, new_text)

    def on_row_expanded(self, view, iter, path):
        model = self.view.get_model()
        child = model.iter_children(iter)
        if model.get_value(child, 0) is None:
            # Populate it
            from sndcs.JobHed import JobHed
            from sndcs.Assembly import Assembly
            parent = model.get_value(iter, 0)
            if isinstance(parent, JobHed):
                for assembly in parent.assemblies():
                    assembly_parent = model.append(iter, (assembly, assembly.number(), None, assembly.description(), None))
                    #Append a dummy operation
                    model.append(assembly_parent, (None, None, None, None, None))
            elif isinstance(parent, Assembly):
                for operation in parent.operations():
                    model.append(iter, (operation, operation.number(), None, operation.description(), None))
            model.remove(child)

    def populate(self):
        """Populate the model"""
        model = gtk.TreeStore(object, str, str, str, str)
        self.template_model = model
        available_activities = JobHedFactory().getActiveJobHeds()
        for jobhed in available_activities:
            job_parent = model.append(None, (jobhed, jobhed.number(), jobhed.itemNumber(), jobhed.itemDescription(), jobhed.priority()))
            assembly_parent = model.append(job_parent, (None, None, None, None, None)) # Dummy assembly for lazy treeview
            model.append(assembly_parent, (None, None, None, None, None)) # Dummy operation for lazy treeview
        self.view.set_model(gtk.TreeModelSort(self.modelfilter))

    # Event is not used yet
    def update(self,event=False):
        """Update for search"""
        
        model = self.template_model
        modelfilter = model.filter_new()
        modelfilter.set_visible_func(treeview_visible_function,self)

        self.view.set_model(gtk.TreeModelSort(self.modelfilter))

    def restore(self):
        self.view.set_model(self.template_model)
