import FreeCAD as App
import FreeCADGui as Gui

# Code copied from Assembly4 to select the top-most link instead of a part or sub-object when clicking in the 3D view
class DocLinkObserver:
    select_link_mode = False
    
    def __init__(self):
        Gui.Selection.addObserver(self, 0) # 0 forces to resolve the links
    
    def addSelection(self, doc, obj, sub, pnt):
        # Since both 3D view clicks and manual tree selection gets into the same callback
        # we will determine by clicked coordinates, for manual tree selections the coordinates are (0,0,0)
        if self.select_link_mode and pnt != (0,0,0):
            # 3D view click
            objList = App.getDocument(doc).getObject(obj).getSubObjectList(sub)
            # Build the name of the selected sub-object for multiple sub-assembly levels
            subObjName = ''
            # look for the link to the selected entity.
            # We select the first link we find (removing the "break" statement would instead select the bottom-most sub-sub-...-sub-assembly)
            for subObj in objList:
                if subObj.TypeId=='App::Link':
                    subObjName = subObjName + subObj.Name + '.'
                    break
                elif subObj.TypeId == 'Part::FeaturePython' and hasattr(subObj, 'LinkedObject'): # variant link
                    subObjName = subObjName + subObj.Name + '.'
                    break
            # if no App::Link found, let's look for other things:
            if subObjName == '':
                for subObj in objList:
                    if subObj.TypeId=='App::Part' or subObj.TypeId=='PartDesign::Body'or subObj.isDerivedFrom('Part::Feature'):
                        # the objList contains also the top-level object, don't count it twice
                        if subObj.Name != obj:
                            subObjName = subObjName + subObj.Name + '.'
                            break
            # if we found something, make it the selection
            #if subObjName != '':
            Gui.Selection.removeSelection(doc, obj, sub)
            Gui.Selection.addSelection(doc, obj, subObjName)
            #FCC.PrintMessage("*"+doc+"*"+obj+"*"+subObjName+"*\n")

# The main document observer (also observes selections)
class DocObserver:
    changed_object_by_type = {}
    selection_by_type = {}
    doc_callbacks = {}
    
    was_selected = []
    
    def __init__(self):
        Gui.Selection.addObserver(self)
        
    def slotActivateDocument(self, doc):
        #print('slotActivateDocument', doc.Name)
        self._triggerDocumentEvent(doc, 'activate')
        
    def slotCreatedDocument(self, doc):
        #print('slotCreatedDocument', doc.Name)
        self._triggerDocumentEvent(doc, 'created')
        
    def slotDeletedDocument(self, doc):
        #print('slotDeletedDocument', doc.Name)
        self._triggerDocumentEvent(doc, 'deleted')
        
    def _triggerDocumentEvent(self, doc, event):
        for callback in self.doc_callbacks.values():
            callback(doc, event)
    
    def slotChangedObject(self, obj, prop):
        #print("object changed: " + str(obj).replace('<', '').replace(' object>', '') + " " + obj.Name + " : " + str(prop))
        callbacks = self.changed_object_by_type.get(obj.TypeId, None)
        if callbacks is not None:
            for callback in callbacks.values():
                callback(obj, prop)
        
    def addSelection(self, doc, obj_name, sub, pnt):
        self.was_selected = Gui.Selection.getSelection()
        obj = App.getDocument(doc).getObject(obj_name)
        callbacks = self.selection_by_type.get(obj.TypeId, None)
        if callbacks is not None:
            for callback in callbacks.values():
                callback('added', obj, sub, pnt)
                
    def removeSelection(self, doc, obj_name, sub):
        self.was_selected = Gui.Selection.getSelection()
        obj = App.getDocument(doc).getObject(obj_name)
        callbacks = self.selection_by_type.get(obj.TypeId, None)
        if callbacks is not None:
            for callback in callbacks.values():
                callback('removed', obj, sub, None)
    
    def clearSelection(self, p):
        was_selected = self.was_selected
        self.was_selected = []
        
        for obj in was_selected:
            callbacks = self.selection_by_type.get(obj.TypeId, None)
            if callbacks is not None:
                for callback in callbacks.values():
                    callback('removed', obj, None, None)

    def onObjectTypeChanged(self, callback_id: str, type_id: str, callback):
        callbacks = self.changed_object_by_type.get(type_id, None)
        if callbacks is None:
            callbacks = {}
            self.changed_object_by_type[type_id] = callbacks
        callbacks[callback_id] = callback
        
    def onObjectTypeSelected(self, callback_id: str, type_id: str, callback):
        callbacks = self.selection_by_type.get(type_id, None)
        if callbacks is None:
            callbacks = {}
            self.selection_by_type[type_id] = callbacks
        callbacks[callback_id] = callback
        
    def onDocumentEvent(self, callback_id: str, callback):
        self.doc_callbacks[callback_id] = callback