package com.vanniktech.lintrules.android;

import com.android.annotations.NonNull;
import com.android.resources.ResourceFolderType;
import com.android.tools.lint.detector.api.Implementation;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.LayoutDetector;
import com.android.tools.lint.detector.api.XmlContext;
import java.util.Collection;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import static com.android.resources.ResourceFolderType.LAYOUT;
import static com.android.tools.lint.detector.api.Category.CORRECTNESS;
import static com.android.tools.lint.detector.api.Scope.RESOURCE_FILE_SCOPE;
import static com.android.tools.lint.detector.api.Severity.ERROR;

public class WrongConstraintLayoutUsageDetector extends LayoutDetector {
  static final Issue ISSUE_WRONG_CONSTRAINT_LAYOUT_USAGE = Issue.create("WrongConstraintLayoutUsage", "Wrong usage of the Constraint Layout.",
      "Wrong usage of the Constraint Layout.", CORRECTNESS, 8, ERROR,
      new Implementation(WrongConstraintLayoutUsageDetector.class, RESOURCE_FILE_SCOPE));

  @Override public boolean appliesTo(@NonNull final ResourceFolderType folderType) {
    return folderType == LAYOUT;
  }

  @Override public Collection<String> getApplicableElements() {
    return ALL;
  }

  @Override public void visitElement(final XmlContext context, final Element element) {
    final NamedNodeMap attributes = element.getAttributes();

    for (int i = 0; i < attributes.getLength(); i++) {
      final Node item = attributes.item(i);

      final String localName = item.getLocalName();
      final String properLocalName = localName.replace("Left", "Start").replace("Right", "End");

      final boolean isSuppressed = context.getDriver().isSuppressed(context, ISSUE_WRONG_CONSTRAINT_LAYOUT_USAGE, item);
      final boolean isConstraint = localName.contains("layout_constraint");
      final boolean hasLeft = localName.contains("Left");
      final boolean hasRight = localName.contains("Right");

      final boolean isAnIssue = isConstraint && (hasLeft || hasRight);

      if (!isSuppressed && isAnIssue) {
        context.report(ISSUE_WRONG_CONSTRAINT_LAYOUT_USAGE, context.getNameLocation(item), "This attribute won't work with RTL. Please use " + properLocalName + " instead.");
      }
    }
  }
}

@@ -1630,16 +1630,50 @@
 Name();%0A
+%0A      if (localName != null) %7B%0A  
       fi
@@ -1758,24 +1758,26 @@
 %22, %22End%22);%0A%0A
+  
       final 
@@ -1883,32 +1883,34 @@
 E, item);%0A      
+ 
+ 
 final boolean is
@@ -1961,32 +1961,34 @@
 traint%22);%0A      
+  
 final boolean ha
@@ -2025,24 +2025,26 @@
 ft%22);%0A      
+ 
+ 
 final boolea
@@ -2082,24 +2082,26 @@
 (%22Right%22);%0A%0A
+  
       final 
@@ -2162,24 +2162,26 @@
 ht);%0A%0A      
+  
 if (!isSuppr
@@ -2198,24 +2198,26 @@
 sAnIssue) %7B%0A
+  
         cont
@@ -2375,24 +2375,34 @@
 instead.%22);%0A
+        %7D%0A
       %7D%0A    

Fix NPE in WrongConstraintLayoutUsageDetector. (#57)