package com.indigitall.cordova.utils;

import androidx.annotation.Nullable;

import com.indigitall.android.inapp.models.InApp;
import com.indigitall.android.inapp.models.InAppAction;
import com.indigitall.android.inapp.models.InAppErrorModel;
import com.indigitall.android.inapp.models.InAppPageTopic;
import com.indigitall.android.inapp.models.InAppProperties;
import com.indigitall.android.inapp.models.InAppSchema;
import com.indigitall.android.inapp.models.InAppShow;
import com.indigitall.android.inapp.models.InAppTopic;
import com.indigitall.android.push.models.Topic;

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

public class InAppParseUtils {

    public static JSONObject getInAppActionCallback(InApp inApp, InAppAction action) {
        JSONObject json = new JSONObject();
        try {
            json.put("inApp", jsonFromInApp(inApp));
            json.put("action", jsonFromAction(action));
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return json;
    }

    public static  JSONObject getInAppFormSubmitCallback(InApp inApp, JSONObject jsonForm) {
        JSONObject json = new JSONObject();
        try {
            json.put("inApp", jsonFromInApp(inApp));
            json.put("jsonForm", jsonForm);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return json;
    }
    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 jsonFromInApp(InApp inApp) {
        JSONObject json = new JSONObject();
        final String JSON_INAPP_ID = "inAppId";
        final String JSON_LAST_VERSION_ID = "lastVersionId";
        final String JSON_SHOW_ONCE = "showOnce";
        final String JSON_CACHE_TTL = "cacheTtl";
        final String JSON_CREATION_DATE = "creationDate";
        final String JSON_EXPIRED_DATE = "expiredDate";
        final String JSON_PROPERTIES = "properties";
        final String JSON_SCHEMA = "schema";
        final String JSON_VERSION = "version";
        final String JSON_CUSTOM_DATA = "customData";
        final String JSON_INAPP_SHOW = "inAppShow";
        final String JSON_INAPP_FILTERS = "filters";
        final String JSON_INAPP_NAME = "name";

        try {
            json.put(JSON_INAPP_ID, inApp.getInAppId());
            json.put(JSON_LAST_VERSION_ID, inApp.getLastVersionId());
            json.put(JSON_SHOW_ONCE, inApp.isShowOnce());
            if (inApp.getCacheTtl() != null)  json.put(JSON_CACHE_TTL, inApp.getCacheTtl());
            if (inApp.getCreationDate() != null) json.put(JSON_CREATION_DATE, inApp.getCreationDate());
            if (inApp.getExpiredDate() != null) json.put(JSON_EXPIRED_DATE, inApp.getExpiredDate());
            if (inApp.getProperties() != null) json.put(JSON_PROPERTIES, jsonFromProperties(inApp.getProperties()));
            if (inApp.getSchema() != null) json.put(JSON_SCHEMA, jsonFromSchema(inApp.getSchema()));
            json.put(JSON_VERSION, inApp.getVersion());
            if (inApp.getCustomData() != null) json.put(JSON_CUSTOM_DATA, new JSONObject(inApp.getCustomData()));
            if (inApp.getInAppShow() != null) json.put(JSON_INAPP_SHOW, jsonFromInAppShow(inApp.getInAppShow()));
            if (inApp.getName() != null) json.put(JSON_INAPP_NAME, inApp.getName());
            if (inApp.getFilters() != null) json.put(JSON_INAPP_FILTERS, inApp.getFilters().toJSON());
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    public static JSONObject jsonFromSchema(InAppSchema schema) {
        JSONObject json = new JSONObject();
        final String JSON_CODE = "code";
        final String JSON_WIDTH = "width";
        final String JSON_HEIGHT = "height";

        try {
            json.put(JSON_CODE, schema.getCode());
            json.put(JSON_WIDTH, schema.getWidth());
            json.put(JSON_HEIGHT, schema.getHeight());
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    public static JSONObject jsonFromInAppShow(InAppShow show) {
        JSONObject json = new JSONObject();
        final String JSON_TIMES_SHOWED = "timesShowed";
        final String JSON_TIMES_CLICKED = "timesClicked";
        final String JSON_WAS_DISMISSED = "wasDismissed";

        try {
            json.put(JSON_TIMES_CLICKED, show.getTimesClicked());
            json.put(JSON_TIMES_SHOWED, show.getTimesShowed());
            json.put(JSON_WAS_DISMISSED, show.getWasDismissed());

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

    public static JSONObject jsonFromProperties(InAppProperties properties) {
        JSONObject json = new JSONObject();
        final String JSON_ACTION = "action";
        final String JSON_CONTENT_URL = "contentUrl";
        final String JSON_SHOWTIME = "showTime";
        final String JSON_NUMBER_OF_SHOWS = "numberOfShows";
        final String JSON_NUMBER_OF_CLIKS = "numberOfClicks";
        final String JSON_DISMISS_FOREVER = "dismissForever";
        final String JSON_BORDER_RADIUS = "borderRadius";

        try {
            if (properties.getAction() != null) json.put(JSON_ACTION, jsonFromAction(properties.getAction()));
            if (properties.getContentUrl() != null) json.put(JSON_CONTENT_URL, properties.getContentUrl());
            json.put(JSON_SHOWTIME, properties.getShowTime());
            if (properties.getLayout() != null) json.put(JSON_BORDER_RADIUS, properties.getLayout().getBorderRadius());
            json.put(JSON_NUMBER_OF_SHOWS, properties.getNumberOfShows());
            json.put(JSON_NUMBER_OF_CLIKS, properties.getNumberOfClicks());
            json.put(JSON_DISMISS_FOREVER, properties.getDismissForever());
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    public static JSONObject jsonFromAction(InAppAction 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";

        JSONObject json = new JSONObject();
        try {
            if (pushAction.isDestroy()) json.put(JSON_DESTROY, pushAction.isDestroy() );
            if (pushAction.getTopics() != null) json.put(JSON_TOPICS, pushAction.getTopics() );
            if (pushAction.getType() != null) json.put(JSON_TYPE, pushAction.getType() );
            if (pushAction.getApp() != null) json.put(JSON_TYPE_APP, pushAction.getApp() );
            if (pushAction.getUrl() != null) json.put(JSON_TYPE_URL, pushAction.getUrl() );
            if (pushAction.getCall() != null) json.put(JSON_TYPE_CALL, pushAction.getCall() );
            if (pushAction.getMarket() != null) json.put(JSON_TYPE_MARKET, pushAction.getMarket() );
            if (pushAction.getShare() != null) json.put(JSON_TYPE_SHARE, pushAction.getShare() );

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

        return json;
    }

    public static JSONObject jsonFromInAppPageTopic(InAppPageTopic pageTopic) {
        JSONObject json = new JSONObject();
        final String JSON_TOPICS = "topics";

        try {
            if (pageTopic.getTopics() != null) {
                JSONArray topicsArray = new JSONArray();
                for (InAppTopic topic : pageTopic.getTopics()) {
                    topicsArray.put(jsonFromTopic(topic));
                }
                json.put(JSON_TOPICS, topicsArray);
            }

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

        return json;
    }

    public static JSONObject jsonFromTopic(InAppTopic 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";

        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());
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        return json;
    }

    public static JSONObject jsonFromInAppShowError(InApp inApp, InAppErrorModel inAppError) {

        int code = 0;
        if (inAppError.getErrorCode().getErrorId() != null) code = inAppError.getErrorCode().getErrorId();
        return jsonFromInAppErrors(inApp, code, inAppError.getErrorMessage().getErrorMessage(), inAppError.getDescriptionMessage());
    }

    public static JSONObject jsonFromInAppErrors(@Nullable InApp inApp, int i, String s, String s1) {
        JSONObject json = new JSONObject();
        try {
            if (inApp != null) json.put("inApp", inApp.toString());
            json.put("errorCode", i);
            json.put("errorMessage", s +" descriptionMessage: " + s1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }
}
