/*
	Licensed Material - Property of IBM

	(C) Copyright 2015,2016 IBM Corp.

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/


'use strict';

var propertiesParser = require('properties-parser');

/*
Builds the Android mfpclient.properties file or updates it if oldData !== undefined
*/
function buildClientProperties(serverInfo, runtime, oldData){
    var server = serverInfo;
    var result = oldData === undefined ? propertiesParser.createEditor() : oldData;

    if(typeof server.path !== 'undefined' && server.path!=="/" && server.path!==""){
        var serverUri = `${server.protocol}://${server.host}:${server.port}${server.path}`;
        result.set("serverUri",serverUri);
    }
    else{
        if(result.get('serverUri') !== undefined){
            result.set("serverUri","");
        }
        if(server.protocol){
            result.set('wlServerProtocol', server.protocol);
        }
        if(server.host){
            result.set('wlServerHost', server.host);
        }
        if(server.port){
            //the module properties-parser only processes strings, so to get result.set to set the port the port (which is a Number) has to be converted to a String
            result.set('wlServerPort', ''+server.port);
        }
        if(runtime){
            result.set('wlServerContext', '/'+runtime+'/');
        }
        if(result.get('languagePreferences') === undefined) {
            result.set('languagePreferences', 'en');
        }
    }
    return result;
}

module.exports = buildClientProperties;
