package com.amplitude.api;

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

import java.util.HashSet;
import java.util.Set;

/*
    Wrapper class for identify events and user properties operations
 */
public class Identify {

    public static final String TAG = "com.amplitude.api.Identify";

    protected JSONObject userPropertiesOperations = new JSONObject();
    protected Set<String> userProperties = new HashSet<String>();

    public Identify setOnce(String property, Object value) {
        addToUserProperties(Constants.AMP_OP_SET_ONCE, property, value);
        return this;
    }

    public Identify set(String property, Object value) {
        addToUserProperties(Constants.AMP_OP_SET, property, value);
        return this;
    }

    public Identify add(String property, float value) {
        addToUserProperties(Constants.AMP_OP_ADD, property, value);
        return this;
    }

    public Identify add(String property, int value) {
        addToUserProperties(Constants.AMP_OP_ADD, property, value);
        return this;
    }

    public Identify add(String property, long value) {
        addToUserProperties(Constants.AMP_OP_ADD, property, value);
        return this;
    }

    public Identify add(String property, double value) {
        addToUserProperties(Constants.AMP_OP_ADD, property, value);
        return this;
    }

    public Identify append(String property, Object value) {
        addToUserProperties(Constants.AMP_OP_APPEND, property, value);
        return this;
    }

    public Identify append(String property, float value) {
        addToUserProperties(Constants.AMP_OP_APPEND, property, value);
        return this;
    }

    public Identify append(String property, int value) {
        addToUserProperties(Constants.AMP_OP_APPEND, property, value);
        return this;
    }

    public Identify append(String property, long value) {
        addToUserProperties(Constants.AMP_OP_APPEND, property, value);
        return this;
    }

    public Identify append(String property, double value) {
        addToUserProperties(Constants.AMP_OP_APPEND, property, value);
        return this;
    }

    public Identify unset(String property) {
        addToUserProperties(Constants.AMP_OP_UNSET, property, "-");
        return this;
    }

    private void addToUserProperties(String operation, String property, Object value) {
        // check if property already used in previous operation
        if (userProperties.contains(property)) {
            AmplitudeLog.getLogger().w(TAG, String.format("Already used property %s in previous operation, ignoring operation %s", property, operation));
            return;
        }

        try {
            if (!userPropertiesOperations.has(operation)) {
                userPropertiesOperations.put(operation, new JSONObject());
            }
            userPropertiesOperations.getJSONObject(operation).put(property, value);
            userProperties.add(property);
        } catch (JSONException e) {
            AmplitudeLog.getLogger().e(TAG, e.toString());
        }
    }
}

@@ -98,22 +98,73 @@
 va.util.
-HashSe
+ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.Lis
 t;%0Aimpor
@@ -2189,32 +2189,1310 @@
 rn this;%0A    %7D%0A%0A
+    public Identify append(String property, float %5B%5D values) %7B%0A        List%3CFloat%3E valuesArray = new ArrayList%3CFloat%3E(values.length);%0A        for (float value : values) %7B%0A            valuesArray.add(value);%0A        %7D%0A        addToUserProperties(Constants.AMP_OP_ADD, property, valuesArray);%0A        return this;%0A    %7D%0A%0A    public Identify append(String property, int %5B%5D values) %7B%0A        List%3CInteger%3E valuesArray = new ArrayList%3CInteger%3E(values.length);%0A        for (int value : values) %7B%0A            valuesArray.add(value);%0A        %7D%0A        addToUserProperties(Constants.AMP_OP_ADD, property, valuesArray);%0A        return this;%0A    %7D%0A%0A    public Identify append(String property, long %5B%5D values) %7B%0A        List%3CLong%3E valuesArray = new ArrayList%3CLong%3E(values.length);%0A        for (long value : values) %7B%0A            valuesArray.add(value);%0A        %7D%0A        addToUserProperties(Constants.AMP_OP_ADD, property, valuesArray);%0A        return this;%0A    %7D%0A%0A    public Identify append(String property, double %5B%5D values) %7B%0A        List%3CDouble%3E valuesArray = new ArrayList%3CDouble%3E(values.length);%0A        for (double value : values) %7B%0A            valuesArray.add(value);%0A        %7D%0A        addToUserProperties(Constants.AMP_OP_ADD, property, valuesArray);%0A        return this;%0A    %7D%0A%0A%0A%0A
     public Ident

add support for primitive arrays