/********************************
 *	프로젝트 : gargoyle-jython
 *	패키지   : com.kyj.fx.nashorn
 *	작성일   : 2019. 10. 4.
 *	작성자   : KYJ (callakrsos@naver.com)
 *******************************/
package com.kyj.fx.nashorn;

import java.io.File;
import java.io.IOException;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

import org.junit.Test;

import com.kyj.fx.jython.DefaultScriptEngine;

/**
 * @author KYJ (callakrsos@naver.com)
 *
 */
public class DefaultScriptEngineTest {

//	static {
//		System.setProperty("python.console.encoding", "UTF-8");
//	}

	@Test
	public void test() throws ScriptException {
		DefaultScriptEngine z = new DefaultScriptEngine();

		ScriptEngine engine = z.createEngine();

		// Using the eval() method on the engine causes a direct
		// interpretataion and execution of the code string passed into it
		engine.eval("import sys");
		engine.eval("print sys");

		// Using the put() method allows one to place values into
		// specified variables within the engine
		engine.put("a", "42");

		// As you can see, once the variable has been set with
		// a value by using the put() method, we an issue eval statements
		// to use it.
		engine.eval("print a");
		engine.eval("x = 2 + 2");

		// Using the get() method allows one to obtain the value
		// of a specified variable from the engine instance
		Object x = engine.get("x");
		System.out.println("x: " + x);
		
		
//		engine.eval(" print(\"한글\") ");
	}

	/**
	 * Hangul Print Test <br/>
	 * @throws ScriptException 
	 * @throws IOException 
	 * 
	 * @작성자 : KYJ (callakrsos@naver.com)
	 * @작성일 : 2019. 10. 7. 
	 */
	@Test
	public void test4() throws IOException, ScriptException {
//		System.setProperty("python.console.encoding", "EUC-KR");
		DefaultScriptEngine engine = new DefaultScriptEngine();
		StringBuffer sb = new StringBuffer();
		sb.append("import java.lang.String\n");
		sb.append("def Hangul(str, encoding='utf-8'):\n");
		sb.append("		return java.lang.String(str,encoding)\n");
		
		sb.append("x = '한글 깨짐' \n" );
		sb.append("print(x)\n");
		
		sb.append("x = u'한글 안깨짐' \n" );
		sb.append("print(x)\n");
		
		sb.append("print(java.lang.String('한글 안깨짐', 'utf-8'))\n");
		sb.append("print(Hangul('한글 안깨짐', 'utf-8'))\n");
		sb.append("\n");

////		sb.append("import sys");
//		sb.append("sys.stdout.write(u\"한글\")");
//		sb.toString();
		
		engine.execute(sb.toString());
	}
	
	/**
	 * read from file. <br/>
	 * @throws ScriptException 
	 * @throws IOException 
	 * @작성자 : KYJ (callakrsos@naver.com)
	 * @작성일 : 2019. 10. 7. 
	 */
	@Test
	public void test3() throws IOException, ScriptException {
		DefaultScriptEngine engine = new DefaultScriptEngine();
		engine.execute(new File("script", "Test.py"));
	}
	
	
	@Test
	public void test2() throws IOException, ScriptException {
		StringBuffer sb = new StringBuffer();
		sb.append("x = 100\n");
		sb.append("if x > 0:\n");
		sb.append("    print('wow this is elegant')\n");
		sb.append("else:\n");
		sb.append("    print('Oranization is the key.')\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("print('hello ' + ' ' + 'world')\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("def my_function(a='zz'):\n");
		sb.append("    print(a)\n");
		sb.append("\n");
		sb.append("my_function()\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("class my_class:\n");
		sb.append("    def __init__(self, x, y ):\n");
		sb.append("        self.x = x\n");
		sb.append("        self.y = y\n");
		sb.append("        print('초기화 함수 ')\n");
		sb.append("\n");
		sb.append("    def mul(self):\n");
		sb.append("        print(self.x * self.y)\n");
		sb.append("\n");
		sb.append("    def add(self):\n");
		sb.append("        print(self.x + self.y)\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("ob1 = my_class(7,8)\n");
		sb.append("ob1.mul()\n");
		sb.append("\n");
		sb.append("ob1.add()\n");
		sb.append("\n");
		sb.append("decimalValue = 10\n");
		sb.append("stringValue = \"10\"\n");
		sb.append("floatValue = 10.0\n");
		sb.append("\n");
		sb.append("print ('String of text goes here %d %s %f' % (decimalValue, stringValue, floatValue))\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("x = 3\n");
		sb.append("y = 0\n");
		sb.append("try:\n");
		sb.append("    print('로켓의 궤도 : %f' % (x/y))\n");
		sb.append("except Exception as ex:\n");
		sb.append("    print(ex.__str__() )\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("print(range(1,5))\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("items = [1,2,3,4,5, 10]\n");
		sb.append("for item in items:\n");
		sb.append("    print(item)\n");
		sb.append("items.append(11)\n");
		sb.append("\n");
		sb.append("print(items)\n");
		sb.append("\n");
		sb.append("items = [ y**2 for y in range(1,10)]\n");
		sb.append("print(items)\n");
		sb.append("\n");
		sb.append("pairs = []\n");
		sb.append("A = ['blue', 'yellow', 'red']\n");
		sb.append("B = ['red', 'green', 'blue']\n");
		sb.append("\n");
		sb.append("for a in A:\n");
		sb.append("    for b in B:\n");
		sb.append("        if a != b:\n");
		sb.append("            pairs.append((a,b))\n");
		sb.append("print (pairs)\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("pairs = []\n");
		sb.append("pairs = [(a,b) for a in A for b in B if a!=b]\n");
		sb.append("print(pairs)\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("a = {x for x in 'abracatabra' if x not in 'abc'}\n");
		sb.append("print(a)\n");
		sb.append("\n");
		sb.append("print(\"###############################################\")\n");
		sb.append("alpa = 2\n");
		sb.append("\n");
		sb.append("for a in range(1,10):\n");
		sb.append("    print(\"%d x %d = %d\" % (alpa , a , (alpa*a)) )\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("print('###############################################')\n");
		sb.append("print('abcdefg'.upper())\n");
		sb.append("print(\"%s %s %s\" % (ord('a'), ord('b'), ord('c')))\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("print(\"########################################\")\n");
		sb.append("str = \"abcde\"\n");
		sb.append("newStr = \"\"\n");
		sb.append("for a in range(len(str) - 1,  -1 , -1):\n");
		sb.append("    newStr += str[a]\n");
		sb.append("print(\"print newStr : %s \" %  (newStr))\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("books = [ {\n");
		sb.append("    \"제목\":\"개발자의 코드\",\n");
		sb.append("    \"출판연도\": \"2013.02.28\",\n");
		sb.append("    \"출판사\":\"A\",\n");
		sb.append("    \"쪽수\":200,\n");
		sb.append("    \"추천유무\":False\n");
		sb.append("},  {\n");
		sb.append("    \"제목\":\"클린코드\",\n");
		sb.append("    \"출판연도\": \"2010.03.04\",\n");
		sb.append("    \"출판사\":\"B\",\n");
		sb.append("    \"쪽수\":584,\n");
		sb.append("    \"추천유무\":True\n");
		sb.append("}, {\n");
		sb.append("    \"제목\":\"빅데이터 마케팅\",\n");
		sb.append("    \"출판연도\": \"2014.07.10\",\n");
		sb.append("    \"출판사\":\"A\",\n");
		sb.append("    \"쪽수\":296,\n");
		sb.append("    \"추천유무\":True\n");
		sb.append("},\n");
		sb.append("]\n");
		sb.append("\n");
		sb.append("print(\"############################################\")\n");
		sb.append("many_page = []\n");
		sb.append("my_recommand = []\n");
		sb.append("for book in books:\n");
		sb.append("    if book[\"쪽수\"] > 250:\n");
		sb.append("        many_page.append(book)\n");
		sb.append("\n");
		sb.append("    if book[\"추천유무\"]:\n");
		sb.append("        my_recommand.append(book)\n");
		sb.append("\n");
		sb.append("print(many_page)\n");
		sb.append("print(my_recommand)\n");
		
		
		DefaultScriptEngine engine = new DefaultScriptEngine();
		engine.execute(sb.toString());
	}

}
