/*
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.typescript;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.google.common.collect.Lists;
import com.palantir.typescript.bridge.TypeScriptBridge;
import com.palantir.typescript.bridge.language.LanguageService;

/**
 * The activator class controls the plug-in life cycle.
 *
 * @author tyleradams
 */
public final class Activator extends AbstractUIPlugin {

    private static Activator PLUGIN;

    private TypeScriptBridge bridge;

    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);

        this.bridge = new TypeScriptBridge();

        this.intializeWorkspace();

        PLUGIN = this;
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        PLUGIN = null;

        this.bridge.stop();
        this.bridge = null;

        super.stop(context);
    }

    public static TypeScriptBridge getBridge() {
        return PLUGIN.bridge;
    }

    /**
     * Returns the shared instance.
     *
     * @return the shared instance
     */
    public static Activator getDefault() {
        return PLUGIN;
    }

    /**
     * Returns an image descriptor for the image file at the given plug-in relative path.
     *
     * @param path
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin("com.palantir.typescript", path);
    }

    private void intializeWorkspace() throws CoreException {
        final List<String> files = Lists.newArrayList();

        // add all the TypeScript files in the workspace
        for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
            project.accept(new IResourceVisitor() {
                @Override
                public boolean visit(IResource resource) throws CoreException {
                    if (isTypeScriptFile(resource)) {
                        String file = resource.getRawLocation().toOSString();

                        files.add(file);
                    }

                    return true;
                }
            });
        }
        this.bridge.getLanguageService().addFiles(files);

        // listen to the resource deltas for additional TypeScript files
        this.listenToResourceDeltas();
    }

    private void listenToResourceDeltas() {
        ResourcesPlugin.getWorkspace().addResourceChangeListener(new IResourceChangeListener() {
            @Override
            public void resourceChanged(IResourceChangeEvent event) {
                if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
                    IResource resource = event.getResource();

                    if (isTypeScriptFile(resource)) {
                        String file = resource.getRawLocation().toOSString();
                        LanguageService languageService = Activator.getBridge().getLanguageService();

                        switch (event.getDelta().getKind()) {
                            case IResourceDelta.ADDED:
                                languageService.addFile(file);
                                break;
                            case IResourceDelta.CHANGED:
                                languageService.updateFile(file);
                                break;
                            case IResourceDelta.REMOVED:
                                languageService.removeFile(file);
                                break;
                        }
                    }
                }
            }
        });
    }

    private static boolean isTypeScriptFile(IResource resource) {
        if (resource.getType() != IResource.FILE) {
            return false;
        }

        String name = resource.getName();
        if (name != null && name.endsWith(".ts")) {
            return true;
        }

        return false;
    }
}

@@ -917,16 +917,73 @@
 eDelta;%0A
+import org.eclipse.core.resources.IResourceDeltaVisitor;%0A
 import o
@@ -3842,34 +3842,282 @@
     
-IResource resource = event
+try %7B%0A                        event.getDelta().accept(new IResourceDeltaVisitor() %7B%0A                            @Override%0A                            public boolean visit(IResourceDelta delta) throws CoreException %7B%0A                                IResource resource = delta
 .get
@@ -4125,24 +4125,36 @@
 esource();%0A%0A
+            
             
@@ -4211,32 +4211,44 @@
                 
+            
 String file = re
@@ -4281,24 +4281,36 @@
 OSString();%0A
+            
             
@@ -4428,32 +4428,33 @@
     
-switch (event.getD
+            switch (d
 elta
-()
 .get
@@ -4483,32 +4483,44 @@
                 
+   
+         
 case IResourceDe
@@ -4526,24 +4526,36 @@
 elta.ADDED:%0A
+            
             
@@ -4629,39 +4629,63 @@
                 
+      
+      
 break;%0A
+            
                 
@@ -4749,32 +4749,44 @@
                 
+        
+    
 languageService.
@@ -4827,39 +4827,61 @@
                 
-break;%0A
+            break;%0A          
                 
@@ -4884,32 +4884,34 @@
                 
+  
 case IResourceDe
@@ -4919,24 +4919,36 @@
 ta.REMOVED:%0A
+            
             
@@ -4993,32 +4993,44 @@
 moveFile(file);%0A
+            
                 
@@ -5068,33 +5068,286 @@
                 
-%7D
+            %7D%0A                                %7D%0A%0A                                return true;%0A                            %7D%0A                        %7D);%0A                    %7D catch (CoreException e) %7B%0A                        throw new RuntimeException(e);
 %0A               
@@ -5478,20 +5478,23 @@
         
-if (
+return 
 resource
@@ -5504,17 +5504,17 @@
 tType() 
-!
+=
 = IResou
@@ -5525,199 +5525,46 @@
 FILE
-) %7B%0A            return false;%0A        %7D%0A%0A        String name = resource.getName();%0A        if (name != null && name.endsWith(%22.ts%22)) %7B%0A            return true;%0A        %7D%0A%0A        return false
+ && resource.getName().endsWith(%22.ts%22)
 ;%0A  

Fix broken logic (my bad)