/*
 * Copyright 2015 Jean-Luc Desroches
 * https://github.com/jean1880
 * http://www.jeanlucdesroches.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


package com.jean1880.CookieJar;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.webkit.CookieManager;

import java.lang.String;

import java.io.IOException;
import java.util.Map;

/**
 * Handles interaction with cookies for Cordova
 * @class Cookies
 * @uses CordovaPlugin
 */
public class CookieJar extends CordovaPlugin {

    // Plugin Tag
	private final String TAG = "CookieJar";
    private CallbackContext callback;
    private enum ACTION {
        CLEAR,
        GET,
        SET
    }

    /**
     * Handles the class execution call
     * @param action  Action to perform in the plugin
     * @param args JSONArray object passed innto the function
     * @param callbackContext
     * @return
     * @throws JSONException
     */
	@Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        // set callback
        this.callback = callbackContext;
        boolean success = false;

        // Check action call
        if(action == "clear") {
            this.clear();
            this.callback.success();
            success = true;
        }else if(action == "get") {
            JSONObject Options = args.getJSONObject(0);
            String URL = null;
            String target = null;
            try {
                URL = Options.get("URL").toString();
                try {
                    target = Options.get("target").toString();
                } catch (JSONException er) {
                }

                // return string
                String result = null;
                if (target != null) {
                    result = get(URL, target);
                } else {
                    result = get(URL);
                }

                // check return
                if (result != "ERROR") {
                    success = true;
                    this.callback.success(result);
                } else {
                    success = false;
                }
            } catch (JSONException er) {
                this.callback.error("Expected 'URL' object in options");
                success = false;
            }
        }else {
                this.callback.error("Unknown action");
                success = false;
        }

        return success;
    }

    /**
     * Clears all local cookies
     */
	public void clear() {
		Log.v(TAG, "Clearing cookies...");
        try{
            CookieManager.getInstance().removeAllCookie();
        }catch (Exception er){
            this.callback.error(er.getMessage());
        }
    }

    /**
     * Get all cookies for specified URL
     * @param URL
     */
    public String get(String URL){
		Log.v(TAG, "Fetching cookies for " + URL +"...");
        try{
            return CookieManager.getInstance().getCookie(URL);
        }catch (Exception er){
            this.callback.error(er.getMessage());
            return "ERROR";
        }
    }

    /**
     * Get target cookie for specified URL
     * @param URL
     * @param target
     */
    public String get(String URL, String target){
        Log.v(TAG, "Fetching cookies for " + URL +" by key " + target + "...");
        try{
            return CookieManager.getInstance().getCookie(URL);
        }catch (Exception er){
            this.callback.error(er.getMessage());
            return "ERROR";
        }
    }
	

}

@@ -2365,20 +2365,34 @@
 tion
- == 
+.equalsIgnoreCase(
 %22clear%22)
  %7B%0A 
@@ -2387,16 +2387,17 @@
 %22clear%22)
+)
  %7B%0A     
@@ -2509,18 +2509,33 @@
 tion
- == 
+.equalsIgnoreCase(
 %22get%22)
+)
  %7B%0A 

fix string error