package codenizer.sonarqube.typescript;

import java.util.Arrays;
import java.util.List;
import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;

public class TypeScriptSensor implements Sensor {
    private final FileSystem fileSystem;
    private AnalysisRunner analysisRunner;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public TypeScriptSensor(FileSystem fileSystem) {
        this.fileSystem = fileSystem;
        this.analysisRunner = new AnalysisRunnerImpl();
    }
    
    public void setAnalysisRunner(AnalysisRunner runner) {
        this.analysisRunner = runner;
    }
    
    public boolean shouldExecuteOnProject(Project project) {
        // this sensor is executed on any type of project
        return true;
    }
    
    public void analyse(Project project, SensorContext sensorContext) {
        File rootDir = fileSystem.baseDir();

        log.info("Analysing project root in search for TypeScript files: " + rootDir.getAbsolutePath());
        
        try {
            AnalysisResult[] result = this.analysisRunner.Execute(rootDir.getAbsolutePath());

            log.info("Analysis done, saving metrics");
            log.info("Got " + result.length + " metrics");
            
            for(AnalysisResult r: result) {
                saveCoreMetrics(sensorContext, r, rootDir.getAbsolutePath());
            }
            
            log.info("Metrics saved");
        } catch (Exception ae) {
            log.error("Error while running Analyzer: " + ae.getMessage(), ae);
        }
    }
    
    private void saveCoreMetrics(SensorContext sensorContext, AnalysisResult analysisResult, String rootDir) {
        InputFile file = new DefaultInputFile("someModule", analysisResult.getFileName());
         //log.info("found file: " + file.absolutePath());
         log.info("saving metrics for file " + analysisResult.getFileName());
         
         log.info("trying to save CoreMetrics.CLASSES");
        sensorContext.saveMeasure(file, CoreMetrics.CLASSES, (double)analysisResult.getNumberOfClasses());
        
         log.info("trying to save CoreMetrics.FUNCTIONS");
        sensorContext.saveMeasure(file, CoreMetrics.FUNCTIONS, (double)analysisResult.getNumberOfMethods());
        
         log.info("trying to save CoreMetrics.LINES");
        sensorContext.saveMeasure(file, CoreMetrics.LINES, (double)analysisResult.getNumberOfLines());
        
        log.debug("measures saved");
    }
    
    public String toString() {
        return "TypeScriptSensor";
    }
}
@@ -499,16 +499,57 @@
 Project;
+%0Aimport org.sonar.api.resources.Resource;
 %0A%0Apublic
@@ -2298,32 +2298,239 @@
 e());%0A         %0A
+         Resource resource = sensorContext.getResource(file);%0A         %0A         if(resource != null) %7B%0A             log.info(%22Found resource with language: %22 + resource.getLanguage());%0A         %7D%0A         %0A
          log.inf
@@ -2596,35 +2596,39 @@
 ext.saveMeasure(
-fil
+resourc
 e, CoreMetrics.C
@@ -2775,35 +2775,39 @@
 ext.saveMeasure(
-fil
+resourc
 e, CoreMetrics.F
@@ -2960,19 +2960,23 @@
 Measure(
-fil
+resourc
 e, CoreM
@@ -3034,32 +3034,202 @@
 es());%0A        %0A
+        log.info(%22trying to save CoreMetrics.NCLOC%22);%0A        sensorContext.saveMeasure(resource, CoreMetrics.NCLOC, (double)analysisResult.getNumberOfLines());%0A        %0A
         log.debu

Store the NCLOC as that's what Sonar is apparently using