package uk.ac.ebi.embl.flatfile.reader.genomeassembly;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import uk.ac.ebi.embl.api.entry.genomeassembly.AssemblyInfoEntry;
import uk.ac.ebi.embl.api.validation.ValidationResult;

public class AssemblyInfoReader extends GCSEntryReader
{
	private final String MESSAGE_KEY_INVALID_FORMAT_ERROR = "invalidlineFormat";
	private final String MESSAGE_KEY_INVALID_VALUE_ERROR = "invalidfieldValue";
	private final String MESSAGE_KEY_INVALID_FIELD_ERROR = "invalidfieldName";

	private static Pattern pattern =Pattern.compile("^(\\w+)\\s+(.+)$");

	AssemblyInfoEntry assemblyInfoEntry = null;
	public AssemblyInfoReader(File file)
	{
		super();
		this.file=file;
	}
	@Override
	public ValidationResult read() throws NumberFormatException, IOException
	{
		assemblyInfoEntry= new AssemblyInfoEntry();
		int lineNumber = 1;
		try(BufferedReader reader = getBufferedReader(file))
		{
			String line;
			while ((line = reader.readLine()) != null)
			{
				line = line.trim();
				if (line.isEmpty()) // Skip empty lines
				{
					continue;
				}

				Matcher matcher = pattern.matcher(line);

				if(matcher.matches() && matcher.groupCount() == 2)
				{
					String field= StringUtils.deleteWhitespace(matcher.group(1).toUpperCase());
					field= field.replaceAll("_","").replaceAll("-","").replaceAll("\\.","");			
					String fieldValue= matcher.group(2).trim();
					switch(field)
					{
					case "ASSEMBLYNAME":
						assemblyInfoEntry.setName(fieldValue);
						break;
					case "COVERAGE":
						assemblyInfoEntry.setCoverage(fieldValue);
						break;
					case "PROGRAM":
						assemblyInfoEntry.setProgram(fieldValue);
						break;
					case "PLATFORM":
						assemblyInfoEntry.setPlatform(fieldValue);
						break;
					case "MINGAPLENGTH":
						if(isInteger(fieldValue))
						  assemblyInfoEntry.setMinGapLength(new Integer(fieldValue));
						else
						  error(lineNumber,MESSAGE_KEY_INVALID_VALUE_ERROR,field,fieldValue);
						break;
					case "MOLECULETYPE":
						if(isValidMoltype(fieldValue))
							assemblyInfoEntry.setMoleculeType(fieldValue);
						else
						    error(lineNumber,MESSAGE_KEY_INVALID_VALUE_ERROR,field,fieldValue);
						break;
					case "SAMPLE":
						 assemblyInfoEntry.setSampleId(fieldValue);
						 break;
					case "STUDY":
					  	 assemblyInfoEntry.setStudyId(fieldValue);
					  	 break;
					default :
						error(lineNumber,MESSAGE_KEY_INVALID_FIELD_ERROR,line);
						break;
					}
					
				} else {
					error(lineNumber,MESSAGE_KEY_INVALID_FORMAT_ERROR,line);
				}
				lineNumber++;

			}
		}
		return validationResult;
	}

	public static boolean isInteger(String s) {
	    try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    } catch(NullPointerException e) {
	        return false;
	    }
	    return true;
	}

	public static boolean isValidMoltype(String molType) {
		
		if (molType == null) {
			return false;
		}

		molType = StringUtils.deleteWhitespace(molType).toUpperCase();
		if (!molType.equals("GENOMICDNA") && !molType.equals("GENOMICRNA") && !molType.equals("VIRALCRNA"))
			return false;

		return true;

	}

	@Override
	public ValidationResult skip() throws IOException
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getEntry()
	{
		return assemblyInfoEntry;
	}

	@Override
	public boolean isEntry()
	{
		return validationResult.isValid();
	}

}

@@ -138,76 +138,8 @@
 n;%0D%0A
-import java.util.regex.Matcher;%0D%0Aimport java.util.regex.Pattern;%0D%0A%0D%0A
 impo
@@ -604,81 +604,8 @@
 %0D%0A%0D%0A
-%09private static Pattern pattern =Pattern.compile(%22%5E(%5C%5Cw+)%5C%5Cs+(.+)$%22);%0D%0A%0D%0A
 %09Ass
@@ -1120,111 +1120,137 @@
 %09%7D%0D%0A
+%09%09%09%09
 %0D%0A%09%09%09%09
-Matcher matcher = pattern.matcher(line);%0D%0A%0D%0A%09%09%09%09if(matcher.matches() && matcher.groupCount() =
+String%5B%5D fields = line.trim().split(%22%5C%5Cs+%22,2);%0D%0A%09%09%09%09int numberOfColumns = fields.length;%0D%0A%09%09%09%09if (numberOfColumns !
 = 2
+ 
 )%0D%0A%09
@@ -1251,24 +1251,111 @@
 2 )%0D%0A%09%09%09%09%7B%0D%0A
+%09%09%09%09%09error(lineNumber,MESSAGE_KEY_INVALID_FORMAT_ERROR,line);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09else%0D%0A%09%09%09%09%7B%0D%0A
 %09%09%09%09%09String 
@@ -1394,24 +1394,17 @@
 ace(
-matcher.group(1)
+fields%5B0%5D
 .toU
@@ -1527,31 +1527,17 @@
 ue= 
-matcher.group(2).trim()
+fields%5B1%5D
 ;%0D%0A%09
@@ -2104,32 +2104,36 @@
 ALUE_ERROR,field
+s%5B0%5D
 ,fieldValue);%0D%0A%09
@@ -2341,16 +2341,20 @@
 OR,field
+s%5B0%5D
 ,fieldVa
@@ -2662,85 +2662,8 @@
 %09%09%0D%0A
-%09%09%09%09%7D else %7B%0D%0A%09%09%09%09%09error(lineNumber,MESSAGE_KEY_INVALID_FORMAT_ERROR,line);%0D%0A
 %09%09%09%09

Revert "assembly info read by space"