package com.mixpanel.android.viewcrawler;

import android.view.View;
import android.widget.RelativeLayout;

import org.json.JSONArray;

public class LayoutCaller {
    public LayoutCaller(JSONArray args) {
        if (args.length() == 1) {
            mArgs = new int[]{args.optInt(RULE_INDEX), RelativeLayout.TRUE};
        } else {
            mArgs = new int[]{args.optInt(RULE_INDEX), args.optInt(ANCHOR_ID)};
        }
    }

    public int[] getArgs() {
        return mArgs;
    }

    public void applyMethod(View target) {
        applyMethodWithArguments(target, mArgs);
    }

    public void applyMethodWithArguments(View target, int[] args) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)target.getLayoutParams();
        params.addRule(args[RULE_INDEX], args[ANCHOR_ID]);
        target.setLayoutParams(params);
    }

    private final int[] mArgs;
    public static final int RULE_INDEX = 0;
    public static final int ANCHOR_ID = 1;
}

@@ -127,16 +127,75 @@
 ONArray;
+%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;
 %0A%0Apublic
@@ -260,118 +260,371 @@
 gs) 
-%7B%0A        if (args.length() == 1) %7B%0A            mArgs = new int%5B%5D%7Bargs.optInt(RULE_INDEX), RelativeLayout.TRUE
+throws JSONException %7B%0A        JSONObject layout_info = args.optJSONObject(0);%0A        int rule_id = layout_info.getInt(%22rule_id%22);%0A        if (layout_info.getString(%22operation%22).equals(%22remove%22)) %7B%0A            mArgs = new int%5B%5D%7Brule_id, 0%7D;%0A        %7D else if (layout_info.has(%22anchor_id%22)) %7B%0A            mArgs = new int%5B%5D%7Brule_id, layout_info.getInt(%22anchor_id%22)
 %7D;%0A 
@@ -673,55 +673,36 @@
 t%5B%5D%7B
-args.optInt(RULE_INDEX), args.optInt(ANCHOR_ID)
+rule_id, RelativeLayout.TRUE
 %7D;%0A 

add remove operation