import gtk
import string
import gobject

from sndcs.EmployeeFactory import EmployeeFactory

# Searching function
from Application import treeview_visible_function

class EmployeeAdminTreeView(object):

    def __init__(self, tree_view=False):
        if tree_view:
            # If tree_view is False then self.populate() is calling it
            self.view = tree_view
        
        self.model = gtk.TreeStore(object, str, gobject.TYPE_STRING)

        # Populate the model
        for employee in EmployeeFactory().getEmployees():
            self.model.append(None, (employee, employee.properName(), employee.status()))

        statusList = gtk.ListStore(str)
        from sndcs.Employee import Employee
        statusEnums = Employee().klass().lookupAttr('status')
        for entry in statusEnums.enums():
            statusList.append([entry])
      
        self.view.set_model(self.model)
        
        if tree_view:

            tree_column_1 = gtk.TreeViewColumn("Name")
            tree_column_2 = gtk.TreeViewColumn("Status")
            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()
            cell.set_property("editable", True)
            cell.connect("edited", self.tmp_cell_edited_callback, 1)
            tree_column_1.pack_start(cell, True)
            tree_column_1.add_attribute(cell, "text", 1)
        
            cell = gtk.CellRendererCombo()
            cell.set_property("model", statusList)
            cell.set_property('text-column', 0)
            cell.set_property("editable", True)
            cell.connect("edited", self.tmp_cell_edited_callback, 2)
            tree_column_2.pack_start(cell, True)
            tree_column_2.add_attribute(cell, "text", 2)
        
        #self.view.set_search_column(1)
        #self.view.set_reorderable(True)
        self.column_number = 0
        self.search_criteria = ""
    
        self.template_model=self.model

    def tmp_cell_edited_callback(self, cellrenderer, path, new_text, column):
        iter = self.model.get_iter(path)
        from sndcs.Employee import Employee
        instance = self.model.get_value(iter, 0)
        from sndcs.Store import store
        if isinstance(instance, Employee):
            if column is 1:
                deconstructProperName = string.split(new_text, ", ")
                instance.setLastName(deconstructProperName[0])
                instance.setFirstName(deconstructProperName[1])
                store.saveChanges()
                self.model.set_value(iter, column, new_text)
            if column is 2:
                if new_text:
                    instance.setStatus(new_text)
                    from datetime import datetime
                    t=datetime.today()
                    currentdate=t.strftime("%m/%d/%Y") 
                    if new_text=="Terminated":
                            instance.setTermDate(currentdate)
                    else:
                            instance.setTermDate(None)
                    store.saveChanges()
                    self.model.set_value(iter, column, new_text)
    

    # 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(modelfilter)

    def populate(self):
        self.__init__()
