/**
 * AET
 *
 * Copyright (C) 2013 Cognifide Limited
 *
 * 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.cognifide.aet.job.common.collectors.accessibility;

import com.cognifide.aet.communication.api.metadata.CollectorStepResult;
import com.cognifide.aet.job.api.collector.CollectorJob;
import com.cognifide.aet.job.api.collector.CollectorProperties;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.cognifide.aet.job.api.exceptions.ProcessingException;
import com.cognifide.aet.job.common.utils.javascript.JavaScriptJobExecutor;
import com.cognifide.aet.vs.ArtifactsDAO;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.osgi.framework.BundleContext;

public class AccessibilityCollector implements CollectorJob {

  public static final String NAME = "accessibility";

  private static final String DOCUMENT_OUTER_HTML_SCRIPT = "return document.documentElement.outerHTML;";
  private static final String PARAM_STANDARD = "standard";
  private static final String DEFAULT_STANDARD = "WCAG2AA";

  private final ArtifactsDAO artifactsDAO;
  private final BundleContext context;
  private final CollectorProperties properties;
  private final JavaScriptJobExecutor jsExecutor;

  private String standard = DEFAULT_STANDARD;

  AccessibilityCollector(ArtifactsDAO artifactsDAO, CollectorProperties collectorProperties,
      JavaScriptJobExecutor jsExecutor, BundleContext context) {
    this.artifactsDAO = artifactsDAO;
    this.context = context;
    this.properties = collectorProperties;
    this.jsExecutor = jsExecutor;
  }

  @Override
  public CollectorStepResult collect() throws ProcessingException {
    String script = getScriptFromFile();
    final String html = jsExecutor.execute(DOCUMENT_OUTER_HTML_SCRIPT)
        .getExecutionResultAsString();
    final String json = jsExecutor.execute(script, standard).getExecutionResultAsString();
    List<AccessibilityIssue> issues = parseIssues(json);
    getElementsPositions(issues, html);

    String resultId = artifactsDAO.saveArtifactInJsonFormat(properties, issues);

    return CollectorStepResult.newCollectedResult(resultId);
  }

  private String getScriptFromFile() throws ProcessingException {
    String script;
    try {
      URL entry = context.getBundle().getEntry("/collectors/accessibility/htmlcs.min.js");
      if (entry != null) {
        script = IOUtils.toString(entry.openStream(), StandardCharsets.UTF_8);
      } else {
        throw new ProcessingException("Can't find accessibility htmlcs scripts in bundle context!");
      }
    } catch (Exception e) {
      throw new ProcessingException(e.getMessage(), e);
    }
    return script;
  }

  @Override
  public void setParameters(Map<String, String> params) throws ParametersException {
    if (params.containsKey(PARAM_STANDARD)) {
      standard = params.get(PARAM_STANDARD);
    }
  }

  private List<AccessibilityIssue> parseIssues(String json) {
    Gson gson = new GsonBuilder()
        .registerTypeAdapter(AccessibilityIssue.class, new AccessibilityIssueDeserializer())
        .create();
    Type list = new TypeToken<List<AccessibilityIssue>>() {
    }.getType();
    return gson.fromJson(json, list);
  }

  private void getElementsPositions(List<AccessibilityIssue> issues, final String html) {
    for (AccessibilityIssue issue : issues) {
      int indexOfElement = html.indexOf(issue.getElementString());
      if (indexOfElement >= 0) {
        String beforeOccurrence = html.substring(0, indexOfElement);
        int lineBreaks = StringUtils.countMatches(beforeOccurrence, "\n");
        int columnNumber;
        if (lineBreaks > 0) {
          int indexOfLastLineBreak = beforeOccurrence.lastIndexOf('\n');
          columnNumber = beforeOccurrence.substring(indexOfLastLineBreak).length();
        } else {
          columnNumber = beforeOccurrence.length();
        }
        issue.setLineNumber(lineBreaks + 1);
        issue.setColumnNumber(columnNumber);
      }
    }
  }

}

@@ -1306,16 +1306,74 @@
 arsets;%0A
+import java.util.HashMap;%0Aimport java.util.LinkedHashMap;%0A
 import j
@@ -1387,16 +1387,16 @@
 l.List;%0A
-
 import j
@@ -2789,16 +2789,97 @@
 (json);%0A
+    HashMap%3CAccessibilityIssue, Integer%3E issuesCount = fetchIssuesCount(issues);%0A
     getE
@@ -4069,24 +4069,24 @@
 .getType();%0A
-
     return g
@@ -4108,32 +4108,299 @@
 on, list);%0A  %7D%0A%0A
+  private HashMap%3CAccessibilityIssue, Integer%3E fetchIssuesCount(List%3CAccessibilityIssue%3E issues) %7B%0A    HashMap%3CAccessibilityIssue, Integer%3E map = new LinkedHashMap%3C%3E();%0A    issues.forEach(issue -%3E map.put(issue, map.getOrDefault(issue, 0) + 1));%0A    return map;%0A  %7D%0A%0A
   private void g

Add counting issue occurances