package com.oracle.maf.sample.mcs.apis.analytics;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * Analytics in Mobile Cloud Service (MCS) are event based. Events contain key/value properties that describe
 * what the client wants to log for analysis. "system" Events are generated by the SDK, whereas "custom" Event
 * are generated by the custom MAF application code. System events for example define the start and end of a
 * session.
 *
 * @author   Frank Nimphius
 * @copyright Copyright (c) 2015 Oracle. All rights reserved.
 */
public class Event {

    private Map<String, String> mProperties = null;
    String mName                = null;
    private Date mTimestamp     = null;
    private String mSessionId   = null;


    private String LOG_TAG = "";

    /**
     * Constructor to initialize Event details. The event is created with a current timestamp and an empty properties map
     * @param name  The event name. This event name can be any identifiedr that makes sense in the context of the application
     * @param analyticSessionId  the id of the analytic session that this event will be addociated with
    */
    public Event(String eventName, String sessionId) {
        this(eventName,new Date(),new HashMap<String,String>(), sessionId);
    }

    /**
     * Constructor to initialize Event details. The event is created with no associated analytic session ID
     * @param name  The event name. This event name can be any identifiedr that makes sense in the context of the application
     * @param timestamp The Date instance when this event is created
     * @param properties a Map&ltString,String&gt of key/value pairs describing custom event prioperties like "purchaseOrderId" (key) and "122232" (value)
     */
    public Event(String name, Date timestamp,  Map<String, String> properties){
        this(name,timestamp,properties,null);
    }

     /**
      * Constructor to initialize Event details.
      * @param name  The event name. This event name can be any identifiedr that makes sense in the context of the application
      * @param timestamp The Date instance when this event is created
      * @param properties a Map&ltString,String&gt of key/value pairs describing custom event prioperties like "purchaseOrderId" (key) and "122232" (value)
      * @param analyticSessionId  the id of the analytic session that this event will be addociated with
     */
    public Event(String name, Date timestamp,  Map<String, String> properties, String analyticSessionId){
        
      if(name == null) {
         throw new IllegalArgumentException("'name' cannot be null");
      }

      this.mName = name;
        
       LOG_TAG=LOG_TAG+name;
        this.mSessionId = analyticSessionId;
        this.mTimestamp = timestamp == null ? new Date() : timestamp;
        this.mProperties = properties == null ? new HashMap<String, String>() : properties;
    }

    /**
     * Analytic events are configurable through properties. This methdod returns properties defined 
     * for an analytic event.
     * @return
     */
    public Map<String, String> getProperties() {
        return mProperties;
    }

    /**
     * Returns date event was recorded.
     * @return
     */
    public Date getTimestamp() {
        return mTimestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.mTimestamp = timestamp;
    }

    /**
     * Returns name of the event.
     * @return
     */
    public String getName() {
        return mName;
    }

    /**
     * getter to return Seesion ID in which the event was recorded.
     * @return
     */
    public String getSessionId() {
        return mSessionId;
    }

    /**
     * setter for the Session ID
     */
    public void setSessionId(String sessionId) {
        mSessionId = sessionId;
    }


    /**
     * Add a property to the existing Map of event properties.
     * @param key
     * @param value
     */
    public void addProperty(String key, String value){
        mProperties.put(key, value);
    }

}
