package com.jasonette.seed.Action;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.jasonette.seed.Helper.JasonHelper;

import org.json.JSONObject;

public class JasonReturnAction {


    /*

    -------------------------------------------

    Throughout every action call chain, the original caller action is passed down inside the 'event' object.

    1. When we reach the end via `$return.success`, we look into  what event.success contains.
    Then we execute $lambda on it.

    2. When we reach the end via `$return.success`, we look into  what event.success contains.
    Then we execute $lambda on it.



    -------------------------------------------

    {
        "$jason": {
            "head": {
                "actions": {
                    "$load": {
                        "type": "$trigger",
                        "options": {
                            "name": "sync"
                        },
                        "success": {
                            "type": "$trigger",
                                "options": {
                                "name": "process"
                            },
                            "success": {
                                "type": "$render"
                            }
                        },
                        "error": {
                            "trigger": "err"
                        }
                    },
                    "sync": {
                        "type": "$network.request",
                        "options": {
                            "url": "https://www.jasonbase.com/things/4nf.json"
                        },
                        "success": {
                            "type": "$return.success"
                        }
                    },
                    "err": {
                        "type": "$util.banner",
                        "options": {
                            "title": "error",
                            "description": "Something went wrong."
                        }
                    }
                },
                "templates": {
                    ...
                }
            }
        }
    }


    whenever triggering something,
    attach the original action as event
    when reaching $return.success or $return.error, just replace it with $event.success
    */



    public void success(final JSONObject action, JSONObject data, final JSONObject event, Context context) {
        Intent intent = new Intent("call");
        if(event.has("success")){
            try{
                intent.putExtra("action", event.getJSONObject("success").toString());
                intent.putExtra("data", data.toString());
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            } catch (Exception e){
                Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
            }
        } else {
            JasonHelper.next("success", new JSONObject(), data, event, context);
        }
    }
    public void error(final JSONObject action, JSONObject data, final JSONObject event, Context context) {
        Intent intent = new Intent("call");
        if(event.has("error")){
            try{
                intent.putExtra("action", event.getJSONObject("error").toString());
                intent.putExtra("data", data.toString());
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            } catch (Exception e){
                Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
            }
        } else {
            JasonHelper.next("error", new JSONObject(), data, event, context);
        }
    }
}

@@ -175,16 +175,60 @@
 l.Log;%0A%0A
+import com.jasonette.seed.Core.JasonParser;%0A
 import c
@@ -2495,35 +2495,49 @@
  */%0A
-%0A%0A%0A
     p
-ublic void success(
+rivate void next(final String type, 
 fina
@@ -2589,32 +2589,38 @@
 SONObject event,
+ final
  Context context
@@ -2635,78 +2635,335 @@
     
-Intent intent = new Intent(%22call%22);%0A        if(event.has(%22success%22))%7B%0A
+if(event.has(type))%7B%0A            try%7B%0A                JSONObject options = action.getJSONObject(%22options%22);%0A                JasonParser.getInstance(context).setParserListener(new JasonParser.JasonParserListener() %7B%0A                    @Override%0A                    public void onFinished(JSONObject parsed_options) %7B%0A          
     
@@ -2962,37 +2962,116 @@
                 
+  
 try
+ 
 %7B%0A
+                            Intent intent = new Intent(%22call%22);%0A            
                 
@@ -3116,25 +3116,20 @@
 NObject(
-%22success%22
+type
 ).toStri
@@ -3127,32 +3127,44 @@
 e).toString());%0A
+            
                 
@@ -3179,36 +3179,46 @@
 utExtra(%22data%22, 
-data
+parsed_options
 .toString());%0A  
@@ -3207,32 +3207,44 @@
 ns.toString());%0A
+            
                 
@@ -3301,32 +3301,44 @@
 adcast(intent);%0A
+            
             %7D ca
@@ -3346,32 +3346,33 @@
 ch (Exception e)
+ 
 %7B%0A              
@@ -3365,32 +3365,44 @@
                 
+            
 Log.d(%22Warning%22,
@@ -3472,34 +3472,32 @@
             
-%7D%0A
         
 %7D else %7B%0A   
@@ -3480,32 +3480,29 @@
             
-%7D else %7B
+    %7D
 %0A           
@@ -3506,77 +3506,8 @@
     
-JasonHelper.next(%22success%22, new JSONObject(), data, event, context);%0A
     
@@ -3520,430 +3520,108 @@
     
-%7D%0A
     
-public void error(final JSONObject action, JSONObject data, final JSONObject event, Context context) %7B%0A        Intent intent = new Intent(%22call%22);%0A        if(event.has(%22error%22))%7B%0A            try%7B%0A                intent.putExtra(%22action%22, event.getJSONObject(%22error%22).toString());%0A                intent.putExtra(%22data%22, data.toString());%0A                LocalBroadcastManager.getInstance(context).sendBroadcast(inten
+        %7D);%0A                JasonParser.getInstance(context).parse(%22json%22, data, options, contex
 t);%0A
@@ -3810,23 +3810,20 @@
 er.next(
-%22error%22
+type
 , new JS
@@ -3873,10 +3873,360 @@
 %7D%0A    %7D%0A
+%0A%0A    public void success(final JSONObject action, JSONObject data, final JSONObject event, final Context context) %7B%0A        next(%22success%22, action, data, event, context);%0A    %7D%0A    public void error(final JSONObject action, JSONObject data, final JSONObject event, final Context context) %7B%0A        next(%22error%22, action, data, event, context);%0A    %7D%0A
 %7D%0A

Fix $return action