package com.indigitall.cordova.utils;

import com.indigitall.android.commons.models.CorePushAction;
import com.indigitall.android.inbox.Inbox;
import com.indigitall.android.inbox.models.InboxCategory;
import com.indigitall.android.inbox.models.InboxCounters;
import com.indigitall.android.inbox.models.InboxNotification;
import com.indigitall.android.inbox.models.InboxPush;
import com.indigitall.android.inbox.models.InboxPushButton;

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

import java.util.ArrayList;

public class InboxParseUtils {


    public static JSONObject jsonFromMessagesCount(InboxCounters inboxCounters) {
        final String JSON_CLICK = "click";
        final String JSON_SENT = "sent";
        final String JSON_DELETED = "deleted";
        final String JSON_LAST_ACCESS = "lastAccess";
        final String JSON_COUNT = "count";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_CLICK, inboxCounters.getClick());
            json.put(JSON_SENT, inboxCounters.getSent());
            json.put(JSON_DELETED, inboxCounters.getDeleted());
            if (inboxCounters.getUnread() != null) {
                json.put(JSON_LAST_ACCESS, inboxCounters.getUnread().getLastAccess());
                json.put(JSON_COUNT, inboxCounters.getUnread().getCount());
            }
        } catch (JSONException | NullPointerException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    public static JSONObject jsonFromInbox(Inbox inbox, ArrayList<InboxNotification> newNotifications) {
        final String JSON_LAST_ACCESS = "lastAccess";
        final String JSON_COUNT = "count";
        final String JSON_PAGESIZE = "pageSize";
        final String JSON_PAGE = "numPage";
        final String JSON_NOTIFICATIONS = "notifications";
        final String JSON_NEW_NOTIFICAIONS ="newNotifications";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_LAST_ACCESS, inbox.getLastAccess());
            json.put(JSON_COUNT, inbox.getCount());
            json.put(JSON_PAGESIZE, inbox.getPageSize());
            json.put(JSON_PAGE, inbox.getPage());
            json.put(JSON_NOTIFICATIONS, jsonArrayFromInboxNotifications(inbox.getNotifications()));
            if (newNotifications != null) json.put(JSON_NEW_NOTIFICAIONS,jsonArrayFromInboxNotifications(newNotifications));
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    public static JSONArray jsonArrayFromInboxNotifications(ArrayList<InboxNotification> inboxNotifications) {
        final String JSON_ID = "id";
        final String JSON_SENTAT = "sentAt";
        final String JSON_STATUS = "status";
        final String JSON_SENDINGID = "sendingId";
        final String JSON_CAMPAIGNID = "campaignId";
        final String JSON_MESSAGE = "message";
        final String JSON_CATEGORY = "category";

        JSONArray array = new JSONArray();
        if (inboxNotifications != null) {
            try {
                for (InboxNotification inboxNotification : inboxNotifications) {
                    JSONObject json = new JSONObject();
                    json.put(JSON_ID, inboxNotification.getId());
                    json.put(JSON_SENTAT,inboxNotification.getSentAt());
                    json.put(JSON_STATUS,inboxNotification.getStatus());
                    json.put(JSON_SENDINGID,inboxNotification.getSendingId());
                    json.put(JSON_CAMPAIGNID,inboxNotification.getCampaignId());
                    if (inboxNotification.getCategory() != null) json.put(JSON_CATEGORY, jsonFromCategory(inboxNotification.getCategory()));
                    if (inboxNotification.getMessage() != null) json.put(JSON_MESSAGE,jsonFromPush(inboxNotification.getMessage()));
                    array.put(json);
                }
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }
        return array;
    }

    public static  JSONObject jsonFromInboxNotification(InboxNotification inboxNotification){
        final String JSON_ID = "id";
        final String JSON_SENTAT = "sentAt";
        final String JSON_STATUS = "status";
        final String JSON_SENDINGID = "sendingId";
        final String JSON_CAMPAIGNID = "campaignId";
        final String JSON_MESSAGE = "message";
        final String JSON_CATEGORY = "category";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_ID, inboxNotification.getId());
            json.put(JSON_SENTAT,inboxNotification.getSentAt());
            json.put(JSON_STATUS,inboxNotification.getStatus());
            json.put(JSON_SENDINGID,inboxNotification.getSendingId());
            json.put(JSON_CAMPAIGNID,inboxNotification.getCampaignId());
            if (inboxNotification.getCategory() != null) json.put(JSON_CATEGORY, jsonFromCategory(inboxNotification.getCategory()));
            if (inboxNotification.getMessage() != null) json.put(JSON_MESSAGE,jsonFromPush(inboxNotification.getMessage()));
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    public static JSONObject jsonFromCategory(InboxCategory category) {
        final String JSON_ID = "id";
        final String JSON_CODE = "code";
        final String JSON_NAME = "name";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_ID, category.id);
            json.put(JSON_CODE,category.code);
            json.put(JSON_NAME,category.name);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    public static  JSONObject jsonFromPush(InboxPush message){
        final String JSON_TITLE = "title";
        final String JSON_BODY = "body";
        final String JSON_IMAGE = "image";
        final String JSON_ACTION = "action";
        final String JSON_BUTTONS = "buttons";
        final String JSON_DATA = "data";
        final String JSON_SECURED_DATA = "securedData";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_TITLE, message.getTitle());
            json.put(JSON_BODY, message.getBody());
            json.put(JSON_IMAGE, message.getImage());
            if (message.getAction() != null) json.put(JSON_ACTION, jsonFromPushAction(message.getAction()));
            json.put(JSON_BUTTONS, jsonFromPushButton(message.getButtons()));
            json.put(JSON_DATA, message.getData());
            json.put(JSON_SECURED_DATA, message.getSecuredData());

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

    public static JSONObject jsonFromPushAction(CorePushAction action) {
        final String JSON_URL = "url";
        final String JSON_APP = "app";
        final String JSON_MARKET = "market";
        final String JSON_SHARE = "share";
        final String JSON_CALL = "call";
        final String JSON_NO_ACTION = "noAction";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_URL, action.getUrl());
            json.put(JSON_APP, action.getApp());
            json.put(JSON_MARKET, action.getMarket());
            json.put(JSON_SHARE, action.getShare());
            json.put(JSON_CALL, action.getCall());
            json.put(JSON_NO_ACTION, action.getNo_action());

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

    public static JSONArray jsonFromPushButton(ArrayList<InboxPushButton> buttons) {
        final String JSON_LABEL = "label";
        final String JSON_ACTION = "action";

        JSONArray array = new JSONArray();
        if (buttons != null) {
            try {
                for (InboxPushButton button : buttons) {
                    JSONObject json = new JSONObject();
                    json.put(JSON_LABEL, button.getLabel());
                    if (button.getAction() != null) json.put(JSON_ACTION,jsonFromPushAction(button.getAction()));

                    array.put(json);
                }
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }
        return array;
    }

}
