package com.seleniumtests.controller;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

import org.apache.log4j.Logger;

@SuppressWarnings("rawtypes")
public interface KeywordProvider {

	public final class PropertiesKeywordProvider implements KeywordProvider {
		private static Set<String> s_loadedKeywordsPackageSet = new HashSet<String>();
		private static Map<String, String> s_keywordsMap = new HashMap<String, String>();
		private static final String DEFAULT_KEYWORD_KEY_PREFIX = "default";
		private String m_resourceBaseName = null;

		public static final String DEFAULT_RESOURCE_BASE_NAME = "keywords";

		public PropertiesKeywordProvider() {
			this.m_resourceBaseName = DEFAULT_RESOURCE_BASE_NAME;
		}

		public PropertiesKeywordProvider(String resourceBaseName) {
			this.m_resourceBaseName = (null == resourceBaseName || "".equals(resourceBaseName)) ? DEFAULT_RESOURCE_BASE_NAME : resourceBaseName;
		}

		/**
		 * Load keywords for a domain flow.
		 * 
		 * @param flowClass
		 * @throws Exception
		 */
		private void _loadKeywords(String site, Class stepClass) {
			synchronized (PropertiesKeywordProvider.class) {
				// to load default properties
				String packagePath = stepClass.getPackage().getName();

				if (!s_loadedKeywordsPackageSet.contains(m_resourceBaseName + "." + DEFAULT_KEYWORD_KEY_PREFIX + "." + packagePath + ".keywords")) {
					ResourceBundle defaultBundle = _pickResourceBundle(stepClass);
					if (null == defaultBundle)
						logger.warn("WARN: resource \"" + packagePath + "." + m_resourceBaseName + ".properties\" not found.");
					else {
						Enumeration<String> keys = defaultBundle.getKeys();
						String identifier = null;
						String value = null;
						while (keys.hasMoreElements()) {
							identifier = keys.nextElement();
							value = defaultBundle.getString(identifier);

							if (!identifier.startsWith(packagePath))
								identifier = packagePath + "." + identifier;

							s_keywordsMap.put(m_resourceBaseName + "." + DEFAULT_KEYWORD_KEY_PREFIX + "." + identifier, value);
						}
					}
					s_loadedKeywordsPackageSet.add(m_resourceBaseName + "." + DEFAULT_KEYWORD_KEY_PREFIX + "." + packagePath + ".keywords");
				}

				// to load the particular site properties
				ResourceBundle localizationalBundle = _pickResourceBundle(stepClass, site);
				if (null == localizationalBundle)
					logger.warn("WARN: resource \"" + packagePath + "." + m_resourceBaseName + "_" + site.toLowerCase() + ".properties\" not found.");
				else {
					Enumeration<String> keys = localizationalBundle.getKeys();
					String identifier = null;
					String value = null;
					while (keys.hasMoreElements()) {
						identifier = keys.nextElement();
						value = localizationalBundle.getString(identifier);
//						if (site.equalsIgnoreCase("RU")||site.equalsIgnoreCase("HK")) {
							try {
								value = new String(value.getBytes("ISO8859-1"), "UTF-8");
							} catch (UnsupportedEncodingException e) {
							}
//						}

						if (!identifier.startsWith(packagePath))
							identifier = packagePath + "." + identifier;

						s_keywordsMap.put(m_resourceBaseName + "." + site + "." + identifier, value);
					}
				}
				s_loadedKeywordsPackageSet.add(m_resourceBaseName + "." + site + "." + packagePath + ".keywords");
			}
		}

		private ResourceBundle _pickResourceBundle(Class clz) {
			String packagePath = clz.getPackage().getName();
			try {
				return ResourceBundle.getBundle(packagePath + "." + m_resourceBaseName, new Locale("", "", ""));
			} catch (MissingResourceException e) {
				return null;
			}
		}

		private ResourceBundle _pickResourceBundle(Class clz, String site) {
			String packagePath = clz.getPackage().getName();
			try {
				return ResourceBundle.getBundle(packagePath + "." + m_resourceBaseName + ((null == site || "".equals(site)) ? "" : "_" + site.toLowerCase()));
			} catch (MissingResourceException e) {
				return null;
			}
		}

		private String getLocalizedSite() {
			String site = null;
			if (ContextManager.getThreadContext().getGBHSite() != null) {
				site = ContextManager.getThreadContext().getGBHSite();
			} else {
				site = ContextManager.getThreadContext().getSite();
			}
			return site;
		}

		public String getKeyword(Class stepClass, String identifier) {
			return getKeyword(getLocalizedSite(), stepClass, identifier);
		}

		public String getKeyword(String site, Class stepClass, String identifier) {
			if (!s_loadedKeywordsPackageSet.contains(m_resourceBaseName + "." + site + "." + stepClass.getPackage().getName() + ".keywords")) {
				_loadKeywords(site, stepClass);
			}

			boolean localizedBuild = Boolean.parseBoolean((String) ContextManager.getGlobalContext().getAttribute(Context.LOCALIZED_BUILD));
			if (localizedBuild) {// false to skip the site related picking
				if (s_keywordsMap.containsKey(m_resourceBaseName + "." + site + "." + stepClass.getCanonicalName() + "." + identifier))
					return s_keywordsMap.get(m_resourceBaseName + "." + site + "." + stepClass.getCanonicalName() + "." + identifier);
			}

			if (s_keywordsMap.containsKey(m_resourceBaseName + "." + DEFAULT_KEYWORD_KEY_PREFIX + "." + stepClass.getCanonicalName() + "." + identifier))
				return s_keywordsMap.get(m_resourceBaseName + "." + DEFAULT_KEYWORD_KEY_PREFIX + "." + stepClass.getCanonicalName() + "." + identifier);

			// throw new
			// RuntimeException("No mappings found in the resource(base-name: "
			// + m_resourceBaseName + ") for keyword " +
			// stepClass.getCanonicalName()
			// + "." + identifier + " and site=" + site);
			return null;
		}

		public String getResourceBaseName() {
			return m_resourceBaseName;
		}

	}

	public Logger logger = Logging.getLogger(KeywordProvider.class);

	public String getKeyword(Class stepClass, String identifier);

	public String getKeyword(String site, Class<?> stepClass, String identifier);

}

@@ -1188,30 +1188,8 @@
  * %0A
-%09%09 * @param flowClass%0A
 %09%09 *
@@ -4185,144 +4185,8 @@
 ll;%0A
-%09%09%09if (ContextManager.getThreadContext().getGBHSite() != null) %7B%0A%09%09%09%09site = ContextManager.getThreadContext().getGBHSite();%0A%09%09%09%7D else %7B%0A
 %09%09%09%09
@@ -4237,21 +4237,16 @@
 Site();%0A
-%09%09%09%7D%0A
 %09%09%09retur
@@ -4652,456 +4652,8 @@
 %09%7D%0A%0A
-%09%09%09boolean localizedBuild = Boolean.parseBoolean((String) ContextManager.getGlobalContext().getAttribute(Context.LOCALIZED_BUILD));%0A%09%09%09if (localizedBuild) %7B// false to skip the site related picking%0A%09%09%09%09if (s_keywordsMap.containsKey(m_resourceBaseName + %22.%22 + site + %22.%22 + stepClass.getCanonicalName() + %22.%22 + identifier))%0A%09%09%09%09%09return s_keywordsMap.get(m_resourceBaseName + %22.%22 + site + %22.%22 + stepClass.getCanonicalName() + %22.%22 + identifier);%0A%09%09%09%7D%0A%0A
 %09%09%09i
@@ -4939,227 +4939,8 @@
 );%0A%0A
-%09%09%09// throw new%0A%09%09%09// RuntimeException(%22No mappings found in the resource(base-name: %22%0A%09%09%09// + m_resourceBaseName + %22) for keyword %22 +%0A%09%09%09// stepClass.getCanonicalName()%0A%09%09%09// + %22.%22 + identifier + %22 and site=%22 + site);%0A
 %09%09%09r

remove unused code