package com.indigitall.cordova.utils;

import android.webkit.WebView;

import com.indigitall.android.commons.models.CorePushAction;
import com.indigitall.android.push.models.Device;
import com.indigitall.android.push.models.ExternalApp;
import com.indigitall.android.push.models.Push;
import com.indigitall.android.push.models.PushButton;
import com.indigitall.android.push.models.Topic;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class PushParseUtils {


    public static JSONObject jsonFromDevice(Device device) {
        final String JSON_ENABLE = "enable";
        final String JSON_DEVICE_ID = "deviceId";
        final String JSON_PUSH_TOKEN = "pushToken";
        final String JSON_PLATFORM = "platform";
        final String JSON_VERSION = "version";
        final String JSON_PRODUCT_NAME = "productName";
        final String JSON_PRODUCT_VERSION = "productVersion";
        final String JSON_OS_NAME = "osName";
        final String JSON_OS_VERSION = "osVersion";
        final String JSON_DEVICE_BRAND = "deviceBrand";
        final String JSON_DEVICE_MODEL = "deviceModel";
        final String JSON_OPERATOR = "operator";
        final String JSON_DEVICE_TYPE = "deviceType";
        final String JSON_APP_VERSION = "appVersion";
        final String JSON_LOCALE = "locale";
        final String JSON_TIME_ZONE = "timeZone";
        final String JSON_TIME_OFFSET = "timeOffset";
        final String JSON_EXTERNAL_APPS = "externalApps";
        final String JSON_EXTERNAL_CODE = "externalCode";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_ENABLE, device.getEnabled());
            json.put(JSON_DEVICE_ID, device.getDeviceId());
            json.put(JSON_PUSH_TOKEN, device.getPushToken());
            json.put(JSON_PLATFORM, device.getPlatform());
            json.put(JSON_VERSION, device.getVersion());
            json.put(JSON_PRODUCT_NAME, device.getProductName());
            json.put(JSON_PRODUCT_VERSION, device.getProductVersion());
            json.put(JSON_OS_NAME, device.getOsName());
            json.put(JSON_OS_VERSION, device.getOsVersion());
            json.put(JSON_DEVICE_BRAND, device.getDeviceBrand());
            json.put(JSON_DEVICE_MODEL, device.getDeviceModel());
            json.put(JSON_OPERATOR, device.getOperator());
            json.put(JSON_DEVICE_TYPE, device.getDeviceType());
            json.put(JSON_APP_VERSION, device.getAppVersion());
            json.put(JSON_LOCALE, device.getLocale());
            json.put(JSON_TIME_ZONE, device.getTimeZone());
            json.put(JSON_TIME_OFFSET, device.getTimeOffset());
            json.put(JSON_EXTERNAL_APPS, jsonArrayFromExternalApps(device.getExternalApps()));
            json.put(JSON_EXTERNAL_CODE, device.getExternalCode());
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }


    public static JSONArray jsonArrayFromExternalApps(ExternalApp[] externalApps) {
        final String JSON_ID = "id";
        final String JSON_NAME = "name";
        final String JSON_CODE = "code";

        JSONArray array = new JSONArray();
        if (externalApps != null) {
            try {
                for (ExternalApp app : externalApps) {
                    JSONObject json = new JSONObject();
                    json.put(JSON_ID, app.getId());
                    json.put(JSON_NAME, app.getName());
                    json.put(JSON_CODE, app.getAndroidCode());
                    array.put(json);
                }
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }
        return array;
    }

    public static String[] parseTopicArray(JSONArray params) {

        String[] topics = new String[]{};
        try {
            if (params != null && params.length() > 0) {
                topics = new String[params.length()];
                for (int i = 0; i < params.length(); i++) {
                    topics[i] = params.getString(i);
                }
            }

        } catch(JSONException ex) {
            ex.printStackTrace();
        }

        return topics;
    }

    public static JSONObject jsonFromTopic(Topic topic) {
        final String JSON_CODE = "code";
        final String JSON_NAME = "name";
        final String JSON_VISIBLE = "visible";
        final String JSON_SUBSCRIBED = "subscribed";
        final String JSON_PARENT_CODE = "parentCode";
        //final String JSON_CHANNEL = "channel";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_CODE, topic.getCode());
            json.put(JSON_NAME, topic.getName());
            json.put(JSON_VISIBLE, topic.isVisible());
            json.put(JSON_SUBSCRIBED, topic.isSubscribed());
            json.put(JSON_PARENT_CODE, topic.getParentCode());
            //json.put(JSON_PARENT_CODE, topic.getChannel());
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        return json;
    }

    public static JSONArray jsonArrayFromTopicArray(Topic[] topics) {
        JSONArray json = new JSONArray();
        for (Topic topic : topics) {
            json.put(jsonFromTopic(topic));
        }

        return json;
    }

    public static ArrayList<WebView> parseWebviewsObjects(JSONArray params) {
        ArrayList<WebView>  webViews = new ArrayList<WebView> ();
        try {
            if (params != null && params.length() > 0) {
                for (int i = 0; i < params.length(); i++) {
                    webViews.set(i, (WebView) params.get(i));
                }
            }

        } catch(JSONException ex) {
            ex.printStackTrace();
        }

        return webViews;
    }

    public static ArrayList<String> parseTagsArray(JSONArray params) {
        ArrayList<String> tags = new ArrayList<>();
        try {
            if (params != null && params.length() > 0) {
                for (int i = 0; i < params.length(); i++) {
                    tags.set(i, params.getString(i));
                }
            }

        } catch(JSONException ex) {
            ex.printStackTrace();
        }
        return tags;
    }

    public static JSONObject jsonFromPush(Push push) {
        final String JSON_ID = "id";
        final String JSON_APP_KEY = "appKey";
        final String JSON_TITLE = "title";
        final String JSON_BODY = "body";
        final String JSON_ICON = "icon";
        final String JSON_IMAGE = "image";
        final String JSON_GIF = "gif";
        final String JSON_LAYOUT = "layout";
        final String JSON_DATA = "data";
        final String JSON_SILENT = "silent";
        final String JSON_SECURE_DATA = "securedData";
        final String JSON_ACTION = "action";
        final String JSON_BUTTONS = "buttons";
        final String JSON_SENDING_ID = "sendingId";
        final String JSON_CAMPAIGN_ID = "CampaignId";
        final String JSON_PUSH_ID = "pushId";
        final String JSON_APPLICATION_ID = "applicationId";
        final String JSON_SEND_EVENT_ACK = "sendEventAck";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_ID, push.getId());
            json.put(JSON_APP_KEY, push.getAppKey());
            json.put(JSON_TITLE, push.getTitle());
            json.put(JSON_BODY, push.getBody());
            json.put(JSON_ICON, push.getIcon());
            json.put(JSON_IMAGE, push.getImage());
            json.put(JSON_GIF, push.getGif());
            json.put(JSON_LAYOUT, push.getLayout());
            json.put(JSON_DATA, push.getData());
            json.put(JSON_SILENT, push.getSilent());
            if (push.getAction() != null) json.put(JSON_ACTION, jsonFromAction(push.getAction()));
            json.put(JSON_SECURE_DATA, push.getSecuredData());
            json.put(JSON_SENDING_ID, push.getSendingId());
            json.put(JSON_CAMPAIGN_ID, push.getCampaignId());
            json.put(JSON_PUSH_ID, push.getId());
            json.put(JSON_APPLICATION_ID, push.getApplicationId());
            json.put(JSON_SEND_EVENT_ACK, push.getSendEventAck());
            if (push.getButtons()!= null){
                for(int i = 0; i < push.getButtons().size(); i++){
                    PushButton pushButton = push.getButtons().get(i);
                    json.put(JSON_BUTTONS, jsonFromPushButtons(pushButton));
                }
            }

        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        return json;
    }

    public static JSONObject jsonFromAction(CorePushAction pushAction) {
        final String JSON_DESTROY = "destroy";
        final String JSON_TOPICS = "topics";
        final String JSON_TYPE = "type";
        final String JSON_TYPE_APP = "app";
        final String JSON_TYPE_URL = "url";
        final String JSON_TYPE_CALL = "call";
        final String JSON_TYPE_MARKET = "market";
        final String JSON_TYPE_SHARE = "share";
        final String JSON_CLICKED_BUTTON = "clickedButton";

        JSONObject json = new JSONObject();
        try {

            json.put(JSON_DESTROY, pushAction.isDestroy() );
            json.put(JSON_TOPICS, pushAction.getTopics() );
            json.put(JSON_TYPE, pushAction.getType() );
            json.put(JSON_TYPE_APP, pushAction.getApp() );
            json.put(JSON_TYPE_URL, pushAction.getUrl() );
            json.put(JSON_TYPE_CALL, pushAction.getCall() );
            json.put(JSON_TYPE_MARKET, pushAction.getMarket() );
            json.put(JSON_TYPE_SHARE, pushAction.getShare() );               
            if(pushAction.getClickedButton() != -1) {
                json.put(JSON_CLICKED_BUTTON, pushAction.getClickedButton());
            }

        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        return json;
    }

    public static JSONObject jsonFromPushButtons(PushButton pushButton) {
        final String JSON_LABEL = "label";
        final String JSON_ACTION = "action";
        final String JSON_TOPICS = "topics";

        JSONObject json = new JSONObject();
        try {

            json.put(JSON_LABEL, pushButton.getLabel() );
            json.put(JSON_ACTION, jsonFromAction(pushButton.getAction()));
            json.put(JSON_TOPICS, pushButton.getTopics() );


        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        return json;
    }
}
