package com.uzen.slimapk.Parser;

import java.util.regex.*;
import java.nio.file.*;
import java.io.IOException;

public class FileNameParser implements NameParser {
	private String name;
	// default tempale
	private String patt;
	
	public FileNameParser(String spatt) {	
		if(spatt == null || spatt.isEmpty()){
			patt = "^(.+?)\\.apk";  //chrome.apk -> Chrome	
		} else patt = spatt;
	}
	
	public void setName(Path file) {
		this.name = file.getFileName().toString();
	}
	
	public void parseData() {
		String tmp_name = name;
		try{
			Pattern pattern = Pattern.compile(patt, Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(name);
			
			if (matcher.find()) {
				tmp_name = matcher.group(1);
			};
		} catch (PatternSyntaxException e) {
			System.out.printf("Invalid regular expression: %s\n", e.getMessage());
		} finally {
			if(!tmp_name.isEmpty())
			 name = tmp_name;
		}
		
		name = name.toLowerCase();
		name = name.substring(0, 1).toUpperCase() + name.substring(1);
	}
	
	public String getPattern() {
		return this.patt;
	}
	
	public String getName() {
		return this.name;
	}
}
@@ -494,16 +494,24 @@
 ata() %7B%0A
+%09%09try%7B%0A%09
 %09%09String
@@ -527,19 +527,12 @@
  = n
-ame;%0A%09%09try%7B
+ull;
 %0A%09%09%09
@@ -667,16 +667,44 @@
 r.find()
+ && matcher.groupCount() %3E 0
 ) %7B%0A%09%09%09%09
@@ -732,16 +732,37 @@
 oup(1);%0A
+%09%09%09%09name = tmp_name;%0A
 %09%09%09%7D;%0A%09%09
@@ -876,73 +876,12 @@
 ));%0A
+
 %09%09%7D 
-finally %7B%0A%09%09%09if(!tmp_name.isEmpty())%0A%09%09%09 name = tmp_name;%0A%09%09%7D
 %0A%09%09%0A

check the number of groups