/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2013 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/

/*jslint vars: true, node: true */
"use strict";

var SHAREDCLOUD_HOST = '';
var IMS_HOST = '';
var IMS_CLIENT_ID = 'webpagraphics';
var SHAREDCLOUD_PORT = 443;
var SHAREDCLOUD_PROTOCOL = 'https';
var SHAREDCLOUD_URI;
var GRAPHITEWORKER_NAME = "GraphiteJSONExtractionWorker";
var SERVICES_HOST = 'adobeid-na1-qa2.services.adobe.com';
var WORKER_IS_LOCAL = false;

var SPRITE_SHEET_SCALE = 1;
var SPRITE_SHEET_MIN_SHEETS = 6;
var SPRITE_SHEET_INCLUDE_DRAWABLE = false;
var SPRITE_SHEET_FORMAT = "png";
var SPRITE_SHEET_THREADS = 6;
var SPRITE_SHEET_CLIP_LAYERS = true;

var SC_ENVIRONMENTS = {
	QE1 : 'qe1',
	DEVSTABLE : 'dev-stable',
	LOCALWORKER : 'local-worker',
	CLOUDLABS : 'cloud-labs',
	STAGE : 'stage',
	PROD  : 'prod'
};

var currentEnvironment = SC_ENVIRONMENTS.DEVSTABLE;

function selectEnvironment(sWhichEnv) {

    var isLocalWorker = false;
	if (sWhichEnv === SC_ENVIRONMENTS.QE1) {
		SHAREDCLOUD_HOST = 'sc-dev-qe1.adobesc.com';
		IMS_HOST = 'ims-na1-qa1.adobelogin.com';
        SERVICES_HOST = 'adobeid-na1-qa1.services.adobe.com';
		SHAREDCLOUD_URI = 'https://sc-dev-qe1.adobesc.com';
	} else if (sWhichEnv === SC_ENVIRONMENTS.DEVSTABLE) { 
		SHAREDCLOUD_HOST = 'sc-dev-stable.adobesc.com';
		IMS_HOST = 'ims-na1-qa2.adobelogin.com';
        SERVICES_HOST = 'adobeid-na1-qa2.services.adobe.com';
		SHAREDCLOUD_URI = 'https://sc-dev-stable.adobesc.com';
	} else if (sWhichEnv === SC_ENVIRONMENTS.CLOUDLABS) {
		SHAREDCLOUD_HOST = 'sc-labs.adobesc.com';
		IMS_HOST = 'ims-na1-stg1.adobelogin.com';
        SERVICES_HOST = 'adobeid-na1-stg1.services.adobe.com';
		SHAREDCLOUD_URI = 'https://sc-labs.adobesc.com';
		GRAPHITEWORKER_NAME = "GraphiteJSONExtractionNGWorker";
	} else if (sWhichEnv === SC_ENVIRONMENTS.STAGE) {
		SHAREDCLOUD_HOST = 'cc-us1-stage.adobesc.com';
		IMS_HOST = 'ims-na1-stg1.adobelogin.com';
        SERVICES_HOST = 'adobeid-na1-stg1.services.adobe.com';
		SHAREDCLOUD_URI = 'https://cc-us1-stage.adobesc.com';
	} else if (sWhichEnv === SC_ENVIRONMENTS.PROD) {
		SHAREDCLOUD_HOST = 'cc-us1-prod.adobesc.com';
		IMS_HOST = 'ims-na1-cc1.adobelogin.com';
        SERVICES_HOST = 'adobeid-na1.services.adobe.com';
		SHAREDCLOUD_URI = 'https://cc-us1-prod.adobesc.com';
    } else if (sWhichEnv.substring(0, SC_ENVIRONMENTS.LOCALWORKER.length) === SC_ENVIRONMENTS.LOCALWORKER) {
        // anything that starts with "local-worker"
        SHAREDCLOUD_HOST = 'localhost';
        IMS_HOST = 'ims-na1-qa1.adobelogin.com';
        SERVICES_HOST = 'adobeid-na1-qa1.services.adobe.com';
        SHAREDCLOUD_PORT = 8181;
        SHAREDCLOUD_PROTOCOL = 'http';
        SHAREDCLOUD_URI = 'http://localhost:8181';
        isLocalWorker = true;

        // assume it's "local-worker:foo". It doesn't really have to be a colon; we don't look.
        var optLocalHostname = sWhichEnv.substring(SC_ENVIRONMENTS.LOCALWORKER.length+1);
        if (optLocalHostname) {
            SHAREDCLOUD_HOST = optLocalHostname;
            SHAREDCLOUD_URI = 'http://' + optLocalHostname + ':8181';
        }
	} else {
		throw 'unknown SharedCloud environment (' + sWhichEnv + ')';
	}

	if (process.env.CHARLES_SCPROXYPORT != null) {
		//we're reverse proxying through Charles
		if (null != process.env.CHARLES_SCPROXYHOST) {
			SHAREDCLOUD_HOST = process.env.CHARLES_SCPROXYHOST;
		} else {
			SHAREDCLOUD_HOST = 'localhost';
		}

		SHAREDCLOUD_PORT = parseInt(process.env.CHARLES_SCPROXYPORT);

		SHAREDCLOUD_URI = 'https://' + SHAREDCLOUD_HOST + ':' + SHAREDCLOUD_PORT;
		
	}

    WORKER_IS_LOCAL = isLocalWorker;
	currentEnvironment = sWhichEnv;
}

function getCurrentEnvironment() {
	return currentEnvironment;
}

if (process.env.GRAPHITE_ENV) {
	selectEnvironment(process.env.GRAPHITE_ENV);
} else {
    selectEnvironment(SC_ENVIRONMENTS.DEVSTABLE);
}

if (process.env.GRAPHITE_SS_SCALE && !isNaN(parseFloat(process.env.GRAPHITE_SS_SCALE))) {
    SPRITE_SHEET_SCALE = parseFloat(process.env.GRAPHITE_SS_SCALE);
}
if (process.env.GRAPHITE_SS_MIN_SHEETS && !isNaN(parseInt(process.env.GRAPHITE_SS_MIN_SHEETS, 10))) {
    SPRITE_SHEET_MIN_SHEETS = parseInt(process.env.GRAPHITE_SS_MIN_SHEETS, 10);
}
if (process.env.GRAPHITE_SS_INCLUDE_DRAWABLE) {
    SPRITE_SHEET_INCLUDE_DRAWABLE = (process.env.GRAPHITE_SS_INCLUDE_DRAWABLE !== 0);
}
if (process.env.GRAPHITE_SS_FORMAT) {
    SPRITE_SHEET_FORMAT = process.env.GRAPHITE_SS_FORMAT;
}
if (process.env.GRAPHITE_SS_THREADS && !isNaN(parseInt(process.env.GRAPHITE_SS_THREADS, 10))) {
    SPRITE_SHEET_THREADS = parseInt(process.env.GRAPHITE_SS_THREADS, 10);
}
if (process.env.GRAPHITE_SS_CLIP_LAYERS) {
    SPRITE_SHEET_CLIP_LAYERS = (process.env.GRAPHITE_SS_INCLUDE_DRAWABLE !== 0);
}

// Allows swapping in of dummy dummyuser for local worker endpoints
function getAuthorizationHeaderValue(sIMSAuth) {
	if (WORKER_IS_LOCAL) {
		return "dummy dummyuser";
	} else {
		return 'Bearer ' + sIMSAuth;
	}
}

// Allows swapping in of dummy dummyuser for local worker endpoints
function getUserHeaderValue(sIMSUser) {
	if (WORKER_IS_LOCAL) {
		return "dummy dummyuser";
	} else {
		return sIMSUser;
	}
}


module.exports = {
	SC_ENVIRONMENTS : SC_ENVIRONMENTS,
	getSharedCloudHost : function () { return SHAREDCLOUD_HOST; },
	getSharedCloudPort : function () { return SHAREDCLOUD_PORT; },
	getSharedCloudProtocol : function () { return SHAREDCLOUD_PROTOCOL; },
	getSharedCloudURI : function () { return SHAREDCLOUD_URI; },
	getGraphiteWorkerName : function () {return GRAPHITEWORKER_NAME; },
    getAuthorizationHeaderValue : getAuthorizationHeaderValue,
    getUserHeaderValue : getUserHeaderValue,
	IMS_CLIENT_ID : IMS_CLIENT_ID,
	getIMSHost : function () { return IMS_HOST; },
    getServicesHost : function () { return SERVICES_HOST; },
	selectEnvironment: selectEnvironment,
	getCurrentEnvironment: getCurrentEnvironment,
    SPRITE_SHEET_SCALE : SPRITE_SHEET_SCALE,
    SPRITE_SHEET_MIN_SHEETS : SPRITE_SHEET_MIN_SHEETS,
    SPRITE_SHEET_INCLUDE_DRAWABLE : SPRITE_SHEET_INCLUDE_DRAWABLE,
    SPRITE_SHEET_FORMAT : SPRITE_SHEET_FORMAT,
    SPRITE_SHEET_THREADS : SPRITE_SHEET_THREADS,
    SPRITE_SHEET_CLIP_LAYERS : SPRITE_SHEET_CLIP_LAYERS
};