package com.indigitall.cordova.utils;

import com.indigitall.android.commons.models.Channel;
import com.indigitall.android.customer.models.Customer;
import com.indigitall.android.customer.models.CustomerField;

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

import java.util.ArrayList;

public class CustomerParseUtils {

    public static JSONObject jsonFromCustomer(Customer customer) {
        final String JSON_ID = "id";
        final String JSON_CUSTOMER_ID = "customerId";
        final String JSON_APPLICATION_ID = "applicationId";
        final String JSON_CREATED_AT = "createdAt";
        final String JSON_UPDATED_AT = "updatedAt";

        JSONObject json = new JSONObject();
        try {
            json.put(JSON_ID, customer.getId());
            json.put(JSON_CUSTOMER_ID, customer.getCustomerId());
            json.put(JSON_APPLICATION_ID, customer.getApplicationId());
            json.put(JSON_CREATED_AT, customer.getCreatedAt());
            json.put(JSON_UPDATED_AT, customer.getUpdatedAt());

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

    public static JSONObject jsonFromCustomerField(ArrayList<CustomerField> customerFields) {
        JSONObject json = new JSONObject();
        try {
            for (int i = 0; i < customerFields.size(); i++) {
                String key = customerFields.get(i).getCustomerFieldKey();
                String value = customerFields.get(i).getCustomerFieldValue();
                if (key != null && value != null) {
                    json.put(key, value);
                }
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    public static ArrayList<String> parseArrayList(JSONArray params) {
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            if (params != null && params.length() > 0) {
                for (int i = 0; i < params.length(); i++) {
                    arrayList.add(params.getString(i));
                }
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return arrayList;
    }

    public static JSONObject parseCustomData(JSONArray params) throws JSONException {
        JSONObject jsonParams = params.getJSONArray(0).getJSONObject(0);
        return jsonParams.getJSONObject("customData");
    }

    public static String parseEventCode(JSONArray params) throws JSONException {
        JSONObject jsonParams = params.getJSONArray(0).getJSONObject(0);
        return jsonParams.getString("eventCode");
    }

    public static String parseEventId(JSONArray params) throws JSONException {
        JSONObject jsonParams = params.getJSONArray(0).getJSONObject(0);
        return jsonParams.getString("eventId");
    }

    public static String parseEventAt(JSONArray params) throws JSONException {
        JSONObject jsonParams = params.getJSONArray(0).getJSONObject(0);
        return jsonParams.getString("eventAt");
    }

    public static Channel parseChannel(String channelString) {
        switch (channelString) {
            case "chat":
                return Channel.CHAT;
            case "inapp":
                return Channel.INAPP;
            default:
                return Channel.PUSH;
        }
    }
}
