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

import com.oracle.maf.sample.mcs.shared.mbe.MBE;
import com.oracle.maf.sample.mcs.shared.utils.DateUtil;

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

/**
 * *** INTERNAL USE ONLY  ***
 *
 * Gathers message relating to client to Mobile Cloud Service (MCS) API requests. This information
 * helps administrators to diagnose problems in the system for individual client sessions.
 *
 * @author Frank Nimphius
 * @copyright Copyright (c) 2015 Oracle. All rights reserved.
 */
public class Diagnostics {
    
    private MBE backend = null;

    //Diagnostic Header Names
    public static final String DIAGNOSTIC_SESSION_ID =  "Oracle-Mobile-DIAGNOSTIC-SESSION-ID";
    public static final String DIAGNOSTIC_DEVICE_ID =   "Oracle-Mobile-DEVICE-ID";
    public static final String DIAGNOSTIC_REQUEST_TIME ="Oracle-Mobile-CLIENT-REQUEST-TIME";

    public Diagnostics(MBE backend){
        this.backend = backend;
    }

    public Map<String, String> getHTTPHeaders() {
        Map<String,String> headerMap = new HashMap<String, String>();
        headerMap.put(DIAGNOSTIC_SESSION_ID, getSessionId());
               
        //the clientUID is generated by this utility. However, for mobile applications that 
        //have access to the device information, they can override the generated value on 
        //the MBE backend.
        headerMap.put(DIAGNOSTIC_DEVICE_ID, backend.getClientUID());
        headerMap.put(DIAGNOSTIC_REQUEST_TIME, getRequestTimestamp());
        return headerMap;
    }

    public String getRequestTimestamp() {
        return DateUtil.getISOTimeStamp(new Date());
    }

    public String getSessionId() {
        //we don't have access to the process ID of the OS. So we use a generated clientUI that is 
        //unique for each MBE instance
        return this.backend.getClientUID();        
    }
}
