package uk.ac.ebi.embl.api.validation.fixer.entry;

import uk.ac.ebi.embl.api.entry.Entry;
import uk.ac.ebi.embl.api.entry.Text;
import uk.ac.ebi.embl.api.entry.feature.Feature;
import uk.ac.ebi.embl.api.entry.qualifier.Qualifier;
import uk.ac.ebi.embl.api.entry.reference.Person;
import uk.ac.ebi.embl.api.entry.reference.Reference;
import uk.ac.ebi.embl.api.validation.Severity;
import uk.ac.ebi.embl.api.validation.ValidationResult;
import uk.ac.ebi.embl.api.validation.ValidationScope;
import uk.ac.ebi.embl.api.validation.annotation.Description;
import uk.ac.ebi.embl.api.validation.annotation.ExcludeScope;
import uk.ac.ebi.embl.api.validation.check.entry.EntryValidationCheck;
import uk.ac.ebi.embl.api.validation.helper.Utils;

/**
 * Fix works for certain non-ascii characters only. Check Utils.removeAccents limitations.
 * If it is not possible to transliterate certain chars, it will be caught in and rejected
 * by AsciiCharacterCheck.
 */
@Description("Non-ascii characters fixed from \"{0}\" to \"{1}\".")
@ExcludeScope(validationScope = {ValidationScope.NCBI, ValidationScope.NCBI_MASTER})
public class NonAsciiCharacterFix extends EntryValidationCheck {
    private static final String ASCII_CHARACTER_FIX = "AsciiCharacterFix_1";

    public ValidationResult check(Entry entry) {
        result = new ValidationResult();

        if (entry == null)
            return result;

        attemptFix(entry.getComment());
        attemptFix(entry.getDescription());

        for (Reference reference : entry.getReferences()) {
            String pubTitle = reference.getPublication().getTitle();
            String fixedPubTitle = fixedStr(pubTitle);
            if (!fixedPubTitle.equals(pubTitle)) {
                reference.getPublication().setTitle(fixedPubTitle);
                reportMessage(Severity.FIX, reference.getOrigin(), ASCII_CHARACTER_FIX, pubTitle, fixedPubTitle);
            }

            for (Person author : reference.getPublication().getAuthors()) {
                String firstName = author.getFirstName();
                String fixedFirstName = fixedStr(firstName);
                if (!fixedFirstName.equals(firstName)) {
                    author.setFirstName(fixedFirstName);
                    reportMessage(Severity.FIX, reference.getOrigin(), ASCII_CHARACTER_FIX, firstName, fixedFirstName);
                }

                String surname = author.getSurname();
                String fixedSurname = fixedStr(surname);
                if (!fixedSurname.equals(surname)) {
                    author.setSurname(fixedSurname);
                    reportMessage(Severity.FIX, reference.getOrigin(), ASCII_CHARACTER_FIX, surname, fixedSurname);
                }
            }

        }
        for (Feature feature : entry.getFeatures()) {
            for (Qualifier qualifier : feature.getQualifiers()) {
                if (qualifier.getName().equals(Qualifier.COUNTRY_QUALIFIER_NAME)) {

                    String countryQualifierValue = qualifier.getValue();
                    String fixedVal = fixedStr(countryQualifierValue);

                    if (!fixedVal.equals(countryQualifierValue)) {
                        qualifier.setValue(fixedVal);
                        reportMessage(Severity.FIX, qualifier.getOrigin(), ASCII_CHARACTER_FIX, countryQualifierValue, fixedVal);
                    }
                }
            }
        }
        return result;
    }

    private void attemptFix(Text text) {
        if (text != null && text.getText() != null) {
            if (Utils.hasNonAscii(text.getText())) {
                String fixed = Utils.removeAccents(text.getText());
                if (!fixed.equals(text.getText())) {
                    text.setText(fixed);
                    reportMessage(Severity.FIX, text.getOrigin(), ASCII_CHARACTER_FIX, text.getText(), fixed);
                }
            }
        }
    }

    private String fixedStr(String str) {
        if (Utils.hasNonAscii(str)) {
            return Utils.removeAccents(str);
        }
        return str;
    }
}
@@ -1525,32 +1525,90 @@
 References()) %7B%0A
+            if (reference.getPublication() != null) %7B%0A    
             Stri
@@ -1656,24 +1656,72 @@
 getTitle();%0A
+                if (pubTitle != null) %7B%0A        
             
@@ -1755,32 +1755,40 @@
 dStr(pubTitle);%0A
+        
             if (
@@ -1838,16 +1838,24 @@
         
+        
 referenc
@@ -1906,32 +1906,40 @@
                 
+ 
+       
 reportMessage(Se
@@ -2024,35 +2024,140 @@
 e);%0A            
-%7D%0A%0A
+        %7D%0A                %7D%0A%0A                if (reference.getPublication().getAuthors() != null) %7B%0A        
             for 
@@ -2224,32 +2224,40 @@
                 
+        
 String firstName
@@ -2278,24 +2278,85 @@
 irstName();%0A
+                        if (firstName != null) %7B%0A            
             
@@ -2412,32 +2412,44 @@
                 
+           
+ 
 if (!fixedFirstN
@@ -2485,32 +2485,44 @@
                 
+            
 author.setFirstN
@@ -2554,32 +2554,44 @@
                 
+     
+       
 reportMessage(Se
@@ -2666,32 +2666,70 @@
 ixedFirstName);%0A
+                            %7D%0A        
                 
@@ -2743,24 +2743,32 @@
             
+       
+ 
 String surna
@@ -2789,24 +2789,83 @@
 tSurname();%0A
+                        if (surname != null) %7B%0A            
             
@@ -2917,32 +2917,44 @@
                 
+          
+  
 if (!fixedSurnam
@@ -2986,32 +2986,44 @@
                 
+            
 author.setSurnam
@@ -3051,32 +3051,44 @@
                 
+    
+        
 reportMessage(Se
@@ -3159,32 +3159,110 @@
  fixedSurname);%0A
+                            %7D%0A                        %7D%0A                    %7D%0A
                 
@@ -3269,33 +3269,32 @@
 %7D%0A            %7D%0A
-%0A
         %7D%0A      
@@ -3561,24 +3561,85 @@
 getValue();%0A
+                    if (countryQualifierValue != null) %7B%0A    
             
@@ -3697,17 +3697,20 @@
 Value);%0A
-%0A
+    
         
@@ -3792,16 +3792,20 @@
         
+   
+ 
 qualifie
@@ -3818,32 +3818,36 @@
 alue(fixedVal);%0A
+    
                 
@@ -3952,32 +3952,58 @@
 lue, fixedVal);%0A
+                        %7D%0A
                 
@@ -4712,8 +4712,9 @@
 %0A    %7D%0A%7D
+%0A

fix null pointer exception causing test failures