package utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import qora.account.Account;
import qora.naming.Name;
import api.ApiErrorFactory;
import controller.Controller;
import database.DBSet;
import database.NameMap;

public class NameUtils {

	public enum NameResult
	{
		OK("OK", "OK", 0), 
		NAME_NOT_REGISTERED("The name is not registered", 
				"Invalid address or name not registered!", 
				ApiErrorFactory.ERROR_NAME_NOT_REGISTERED), 
		NAME_WITH_SPACE("For security purposes sending payments to a name that starts or ends with spaces is forbidden.", 
				"Name Payments with trailing or leading spaces are not allowed!",
				ApiErrorFactory.ERROR_NAME_WITH_SPACE), 
		NAME_FOR_SALE("For security purposes sending payments to a name that can be purchased through name exchange is disabled.", 
				"Payments with names that are for sale are not allowed!",
				ApiErrorFactory.ERROR_NAME_FOR_SALE);
		
		private String statusMessage;
		private String shortStatusMessage;
		private int errorcode;

		private NameResult(String statusMessage, String shortStatusMessage, int errorcode)
		{
			this.statusMessage = statusMessage;
			this.shortStatusMessage = shortStatusMessage;
			this.errorcode = errorcode;
			
		}

		public String getStatusMessage() {
			return statusMessage;
		}
		
		public String getShortStatusMessage() {
			return shortStatusMessage;
		}
		
		public int getErrorCode() {
			return errorcode;
		}

	}
	
	public static Pair<Account, NameResult> nameToAdress(String name)
	{
		NameMap names = DBSet.getInstance().getNameMap();
		//NAME NOT REGISTERED?
		if( !names.contains(name))
		{
			
			return new Pair<Account, NameUtils.NameResult>(null, NameResult.NAME_NOT_REGISTERED);
			
		}
			
		//NAME STARTS OR ENDS WITH SPACE?
		if(name.startsWith(" ") || name.endsWith(" "))
		{
			return new Pair<Account, NameUtils.NameResult>(null, NameResult.NAME_WITH_SPACE);
		}
		
		//NAME FOR SALE?
		if(	DBSet.getInstance().getNameExchangeMap().contains(name))
		{
			return new Pair<Account, NameUtils.NameResult>(null, NameResult.NAME_FOR_SALE);
		}
		
		
		//LOOKUP ADDRESS FOR NAME
		Name lookupName = names.get(name);
		String recipientAddress = lookupName.getOwner().getAddress();
		Account recipient = new Account(recipientAddress);
		return new Pair<Account, NameUtils.NameResult>(recipient, NameResult.OK);
	}
	
	public static List<String> getNamesContainingWebsites(boolean removeInjected) {
		List<String> websites = new ArrayList<String>();
		List<String> injected = new ArrayList<String>();

		NameMap nameMap = DBSet.getInstance().getNameMap();

		Set<String> keys = nameMap.getKeys();
		Pattern pattern = Pattern.compile("(?i)<inj>(.*?)</inj>");

		for (String string : keys) {
			String value = nameMap.get(string).getValue();

			value = GZIP.webDecompress(value);

			// PROCESSING TAG INJ
			Matcher matcher = pattern.matcher(value);
			while (matcher.find()) {
				String group = matcher.group(1);
				Name nameinj = Controller.getInstance().getName(group);
				injected.add(group);
				value = value.replace(matcher.group(),
						GZIP.webDecompress(nameinj.getValue().toString()));
			}

			if (value.toLowerCase().contains("html")
					|| value.toLowerCase().contains("iframe")
					|| value.toLowerCase().contains("<a href=")
					|| value.toLowerCase().contains("<script>")
					|| value.toLowerCase().contains("<table>")
					|| value.toLowerCase().contains("<b>")
					|| value.toLowerCase().contains("<font>")
					|| value.toLowerCase().contains("<pre>")) {
				websites.add(string);
			}

		}

		if(removeInjected)
		{
			websites.removeAll(injected);
		}

		for (String string : websites) {
			System.out.println(string);
		}
		
		return websites;
	}
}

@@ -22,258 +22,83 @@
 ort 
-java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Set;%0D%0Aimport java.util.regex.Matcher;%0D%0Aimport java.util.regex.Pattern;%0D%0A%0D%0Aimport qora.account.Account;%0D%0Aimport qora.naming.Name;%0D%0Aimport api.ApiErrorFactory;%0D%0Aimport controller.Controller
+api.ApiErrorFactory;%0D%0Aimport qora.account.Account;%0D%0Aimport qora.naming.Name
 ;%0D%0Ai
@@ -2340,1419 +2340,7 @@
 %09%7D%0D%0A
-%09%0D%0A%09public static List%3CString%3E getNamesContainingWebsites(boolean removeInjected) %7B%0D%0A%09%09List%3CString%3E websites = new ArrayList%3CString%3E();%0D%0A%09%09List%3CString%3E injected = new ArrayList%3CString%3E();%0D%0A%0D%0A%09%09NameMap nameMap = DBSet.getInstance().getNameMap();%0D%0A%0D%0A%09%09Set%3CString%3E keys = nameMap.getKeys();%0D%0A%09%09Pattern pattern = Pattern.compile(%22(?i)%3Cinj%3E(.*?)%3C/inj%3E%22);%0D%0A%0D%0A%09%09for (String string : keys) %7B%0D%0A%09%09%09String value = nameMap.get(string).getValue();%0D%0A%0D%0A%09%09%09value = GZIP.webDecompress(value);%0D%0A%0D%0A%09%09%09// PROCESSING TAG INJ%0D%0A%09%09%09Matcher matcher = pattern.matcher(value);%0D%0A%09%09%09while (matcher.find()) %7B%0D%0A%09%09%09%09String group = matcher.group(1);%0D%0A%09%09%09%09Name nameinj = Controller.getInstance().getName(group);%0D%0A%09%09%09%09injected.add(group);%0D%0A%09%09%09%09value = value.replace(matcher.group(),%0D%0A%09%09%09%09%09%09GZIP.webDecompress(nameinj.getValue().toString()));%0D%0A%09%09%09%7D%0D%0A%0D%0A%09%09%09if (value.toLowerCase().contains(%22html%22)%0D%0A%09%09%09%09%09%7C%7C value.toLowerCase().contains(%22iframe%22)%0D%0A%09%09%09%09%09%7C%7C value.toLowerCase().contains(%22%3Ca href=%22)%0D%0A%09%09%09%09%09%7C%7C value.toLowerCase().contains(%22%3Cscript%3E%22)%0D%0A%09%09%09%09%09%7C%7C value.toLowerCase().contains(%22%3Ctable%3E%22)%0D%0A%09%09%09%09%09%7C%7C value.toLowerCase().contains(%22%3Cb%3E%22)%0D%0A%09%09%09%09%09%7C%7C value.toLowerCase().contains(%22%3Cfont%3E%22)%0D%0A%09%09%09%09%09%7C%7C value.toLowerCase().contains(%22%3Cpre%3E%22)) %7B%0D%0A%09%09%09%09websites.add(string);%0D%0A%09%09%09%7D%0D%0A%0D%0A%09%09%7D%0D%0A%0D%0A%09%09if(removeInjected)%0D%0A%09%09%7B%0D%0A%09%09%09websites.removeAll(injected);%0D%0A%09%09%7D%0D%0A%0D%0A%09%09for (String string : websites) %7B%0D%0A%09%09%09System.out.println(string);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09return websites;%0D%0A%09%7D%0D%0A
 %7D%0D%0A

Revert "added method to search for websites"