package o2mc.io.dimmldependency.Datastreams;

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

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by nickyromeijn on 12/02/16.
 * Data tuple used as storage for gathered track data
 */
public class DataContainer {

    private String elementId;
    private String Activity;
    private String eventType;
    private String viewType;
    private String value;
    private Long timestamp;
    private String packagename;
    private String alias;
    private Integer indexWithinActivity;
    private JSONObject valueAsJson = null;

    public Integer getIndexWithinActivity() { return indexWithinActivity;  }

    public void setIndexWithinActivity(Integer indexWithinActivity) { this.indexWithinActivity = indexWithinActivity; }

    public String getEventType() {
        return eventType;
    }

    public void setEventType(String eventType) {
        this.eventType = eventType;
    }

    public String getViewType() {
        return viewType;
    }

    public void setViewType(String viewType) {
        this.viewType = viewType;
    }

    public String getValue() {
        return value;
    }

    public JSONObject getValueAsJson() {
        return valueAsJson;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setValue(JSONObject value){
        this.valueAsJson = value;
    }

    public String getActivity() {
        return Activity;
    }

    public void setActivity(String activity) {
        Activity = activity;
    }

    public String getElementId() {
        return elementId;
    }

    public void setElementId(String elementId) {
        this.elementId = elementId;
    }

    public String asString(){
        return asJsonString();
    }

    public String asJsonString(){
        return asJson().toString();
    }

    public JSONObject asJson(){
        JSONObject json = new JSONObject();
        try {
            json.put("alias",getAlias());
            json.put("activity", getActivity());
            json.put("eventType", getEventType());
            json.put("eventValue", getValue());
            json.put("elementID", getElementId());
            json.put("trackingDateTime", getTimestamp());
            json.put("indexWithinActivity", getIndexWithinActivity());
            if(valueAsJson != null){
                json.put("eventValue", getValueAsJson());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }

    public String getTimestamp() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // Quoted "Z" to indicate UTC, no timezone offset
        return df.format(new Date());
    }

    public void setTimestamp() {
        this.timestamp = System.currentTimeMillis()/1000l;
    }

    public String getPackagename() {
        return packagename;
    }

    public void setPackagename(String packagename) {
        this.packagename = packagename;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }
}
