package com.jasonette.seed.Core;

import android.content.Context;
import android.util.Log;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;
import com.jasonette.seed.Helper.JasonHelper;

import org.json.JSONObject;

public class JasonParser {
    static JSONObject res;
    private static Context context;

    private static JasonParser instance = null;

    private JasonParser(){
        this.listener = null;
    }

    public interface JasonParserListener {
        public void onFinished(JSONObject json);
    }
    private JasonParserListener listener;
    private V8 juice;
    public void setParserListener(JasonParserListener listener){
        this.listener = listener;
    }



    public static JasonParser getInstance(Context context){
        if(instance == null)
        {
            instance = new JasonParser();
            try {
                String js = JasonHelper.read_file("parser", context);
                instance.juice = V8.createV8Runtime();
                instance.juice.executeVoidScript(js);
                instance.juice.getLocker().release();
            } catch (Exception e){
                Log.d("Error", e.toString());
            }
        }
        return instance;
    }


    public void parse(final String data_type, final JSONObject data, final Object template, final Context context){

        try{
            new Thread(new Runnable(){
                @Override public void run() {

                    try {
                        // thread handling - acquire handle
                        juice.getLocker().acquire();

                        V8Object parser = juice.getObject("parser");

                        String templateJson = template.toString();
                        String dataJson = data.toString();
                        String val = "{}";

                        if(data_type.equalsIgnoreCase("json")) {
                            V8Array parameters = new V8Array(juice).push(templateJson);
                            parameters.push(dataJson);
                            parameters.push(true);
                            val = parser.executeStringFunction("json", parameters);
                            parameters.release();
                        } else {
                            String raw_data = data.getString("$jason");
                            V8Array parameters = new V8Array(juice).push(templateJson);
                            parameters.push(raw_data);
                            parameters.push(true);
                            val = parser.executeStringFunction("html", parameters);
                            parameters.release();
                        }
                        parser.release();


                        res = new JSONObject(val);
                        listener.onFinished(res);

                    } catch (Exception e){
                        Log.d("Error", e.toString());
                    }

                    // thread handling - release handle
                    juice.getLocker().release();
               }
            }).start();
        } catch (Exception e){
            Log.d("Error", e.toString());
        }
    }
}




@@ -1626,16 +1626,520 @@
 quire();
+%0A                        Console console = new Console();%0A                        V8Object v8Console = new V8Object(juice);%0A                        juice.add(%22console%22, v8Console);%0A                        v8Console.registerJavaMethod(console, %22log%22, %22log%22, new Class%3C?%3E%5B%5D %7B String.class %7D);%0A                        v8Console.registerJavaMethod(console, %22error%22, %22error%22, new Class%3C?%3E%5B%5D %7B String.class %7D);%0A                        v8Console.registerJavaMethod(console, %22trace%22, %22trace%22, new Class%3C?%3E%5B%5D %7B%7D);
 %0A%0A      
@@ -3266,16 +3266,61 @@
 ease();%0A
+                        v8Console.release();%0A
 %0A%0A      
@@ -3770,13 +3770,408 @@
 %7D%0A    %7D%0A%7D%0A%0A%0A
+/**%0A * Override for console to print javascript debug output in the Android Studio console%0A */%0Aclass Console %7B%0A    public void log(final String message) %7B%0A        Log.d(%22console.log%22, message);%0A    %7D%0A    public void error(final String message) %7B%0A        Log.e(%22console.error%22, message);%0A    %7D%0A    public void trace() %7B%0A        Log.e(%22console.trace%22, %22Unable to reproduce JS stacktrace%22);%0A    %7D%0A%7D
 %0A

enable usage of javascript console methods