package staticResources;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.lang.reflect.Field;
import java.util.logging.Logger;

import utilities.FileUtility;
import core.languageHandler.Language;


public class PythonResources extends AbstractNativeBootstrapResource {

	@Override
	protected boolean correctExtension(String name) {
		return name.endsWith(".py");
	}

	@Override
	protected boolean generateKeyCode() {
		StringBuilder sb = new StringBuilder();
		Field[] fields = KeyEvent.class.getFields();

		for (Field f : fields) {
			String name = f.getName();
			if (!name.startsWith("VK_")) {
				continue;
			}

			addCodeFromField(sb, f);
		}

		fields = InputEvent.class.getFields();
		for (Field f : fields) {
			String name = f.getName();
			if (!name.startsWith("BUTTON") || !name.endsWith("MASK")) {
				continue;
			}

			addCodeFromField(sb, f);
		}

		String keyCodeFilePath = FileUtility.joinPath(getExtractingDest().getAbsolutePath(), "key_code.py");
		return FileUtility.writeToFile(sb.toString().trim(), new File(keyCodeFilePath), false);
	}

	/**
	 * Add a new line of code that defines the field constant.
	 * The new line of code will be appended to an input string builder.
	 *
	 * @param sb string builder to append the code to.
	 * @param f field containing the information about the constant to add.
	 */
	private void addCodeFromField(StringBuilder sb, Field f) {
		try {
			sb.append(f.getName() + " = " + f.getInt(KeyEvent.class));
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		sb.append("\n");
	}

	@Override
	public Logger getLogger() {
		return Logger.getLogger(PythonResources.class.getName());
	}

	@Override
	protected String getRelativeSourcePath() {
		return "natives/python";
	}

	@Override
	protected File getExtractingDest() {
		return new File(FileUtility.joinPath("resources", "python"));
	}

	@Override
	protected Language getName() {
		return Language.PYTHON;
	}

	@Override
	public File getIPCClient() {
		return new File("resources/python/repeat_lib.py");
	}
}

@@ -183,29 +183,31 @@
 ort 
-utilities.FileUtility
+core.keyChain.KeyStroke
 ;%0Aim
@@ -242,16 +242,46 @@
 nguage;%0A
+import utilities.FileUtility;%0A
 %0A%0Apublic
@@ -941,16 +941,143 @@
 );%0A%09%09%7D%0A%0A
+%09%09for (KeyStroke.Modifier modifier : KeyStroke.Modifier.values()) %7B%0A%09%09%09addCode(sb, modifier.name(), modifier.getValue());%0A%09%09%7D%0A%0A
 %09%09String
@@ -1265,16 +1265,250 @@
 e);%0A%09%7D%0A%0A
+%09/**%0A%09 * Add a new line of code that defines the field constant with field%0A%09 * name and its integer value.%0A%09 */%0A%09private void addCode(StringBuilder sb, String name, int code) %7B%0A%09%09sb.append(name + %22 = %22 + code);%0A%09%09sb.append(%22%5Cn%22);%0A%09%7D%0A%0A
 %09/**%0A%09 *

Update python code generator to include key modifier